You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin多平台Compose模块类无法在非Compose模块使用的发布问题

Kotlin多平台项目maven-publish跨平台发布异常问题

问题场景

在Kotlin多平台项目中,当非Compose模块依赖Compose模块的接口或类后,执行maven-publish任务时出现跨平台发布异常:

具体表现

  • Windows端:应用运行正常,但发布时仅依赖Compose模块的非Compose模块发布失败,未依赖Compose的模块及Compose模块本身可正常发布
  • Mac端:所有模块发布失败,核心报错日志如下:
Could not resolve all task dependencies for configuration ':shared:iosArm64CompileKlibraries'.
Could not resolve project :shared-ui-compose.
Required by:
project :shared
> No matching variant of project :shared-ui-compose was found. The consumer was configured to find a usage of 'kotlin-api' of a library, preferably optimized for non-jvm, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native', attribute 'org.jetbrains.kotlin.native.target' with value 'ios_arm64' but:
- Variant 'debugApiElements' capability Expressus:shared-ui-compose:unspecified declares an API of a library, preferably optimized for Android:
- Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
- Other compatible attribute:
- Doesn't say anything about org.jetbrains.kotlin.native.target (required 'ios_arm64')
- Variant 'debugRuntimeElements' capability Expressus:shared-ui-compose:unspecified declares a runtime of a library, preferably optimized for Android:
- Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
- Other compatible attribute:
- Doesn't say anything about org.jetbrains.kotlin.native.target (required 'ios_arm64')
- Variant 'jvmApiElements' capability Expressus:shared-ui-compose:unspecified declares an API of a library, preferably optimized for standard JVMs:
- Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
- Other compatible attribute:
- Doesn't say anything about org.jetbrains.kotlin.native.target (required 'ios_arm64')
- Variant 'jvmRuntimeElements' capability Expressus:shared-ui-compose:unspecified declares a runtime of a library, preferably optimized for standard JVMs:
- Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
- Other compatible attribute:
- Doesn't say anything about org.jetbrains.kotlin.native.target (required 'ios_arm64')
- Variant 'metadataApiElements' capability Expressus:shared-ui-compose:unspecified declares a library, preferably optimized for non-jvm:
- Incompatible because this component declares a usage of 'kotlin-metadata' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'common' and the consumer needed a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
- Other compatible attribute:
- Doesn't say anything about org.jetbrains.kotlin.native.target (required 'ios_arm64')
- Variant 'releaseApiElements' capability Expressus:shared-ui-compose:unspecified declares an API of a library, preferably optimized for Android:
- Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
- Other compatible attribute:
- Doesn't say anything about org.jetbrains.kotlin.native.target (required 'ios_arm64')
- Variant 'releaseRuntimeElements' capability Expressus:shared-ui-compose:unspecified declares a runtime of a library, preferably optimized for Android:
- Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
- Other compatible attribute:
- Doesn't say anything about org.jetbrains.kotlin.native.target (required 'ios_arm64')

问题原因

报错核心是:shared模块的iOS平台变体尝试解析shared-ui-compose模块,但shared-ui-compose仅配置了Android/JVM/Common平台变体,没有提供iOS Native对应的变体,导致Gradle无法找到匹配的依赖变体,最终发布失败。Windows端运行正常是因为仅涉及JVM/Android平台,而发布任务会处理全平台元数据,因此依赖该模块的非Compose模块发布失败。

解决方案

方案1:抽离通用接口到独立Common模块

将Compose模块中被非Compose模块依赖的接口、数据类等与Compose无关的代码,抽离到纯Common模块(如shared-ui-api):

  1. 新建shared-ui-api模块,仅存放通用代码,不引入Compose依赖
  2. shared-ui-compose模块依赖shared-ui-api,实现其中的接口
  3. 非Compose模块(如shared)仅依赖shared-ui-api,而非直接依赖shared-ui-compose

方案2:为Compose模块添加iOS平台支持

如果确实需要在iOS端使用Compose模块的代码,需为shared-ui-compose配置iOS目标:
在shared-ui-compose的build.gradle.kts中添加:

kotlin {
    // 已有目标(如android、jvm)
    iosArm64()
    iosX64()
    iosSimulatorArm64()
    
    sourceSets {
        val iosMain by getting {
            dependencies {
                // 添加Compose Multiplatform iOS依赖,确保版本兼容
                implementation(compose.ui)
                implementation(compose.foundation)
                implementation(compose.material3)
            }
        }
    }
}

注意:需确保项目已配置Compose Multiplatform插件及对应版本的依赖。

方案3:按平台过滤依赖

如果非Compose模块仅在Android/JVM平台需要依赖Compose模块,可在依赖时指定平台范围:

kotlin {
    sourceSets {
        val androidMain by getting {
            dependencies {
                implementation(project(":shared-ui-compose"))
            }
        }
        val jvmMain by getting {
            dependencies {
                implementation(project(":shared-ui-compose"))
            }
        }
        // iOS等其他平台的sourceSet不添加该依赖
    }
}

内容的提问来源于stack exchange,提问作者YLS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 15:30:50