为何Kotlin库模块无法依赖Android库模块?构建报错求助
解决JVM模块依赖Android库模块的变体不匹配问题
问题背景
尝试构建依赖链:presentation(Android模块) → domain(Kotlin纯JVM模块) → data(Android库模块),但在domain模块添加implementation(projects.data)后出现构建错误:
Could not determine the dependencies of task ':domain:compileJava'. > Could not resolve all task dependencies for configuration ':domain:compileClasspath'. > Could not resolve project :data. Required by: project :domain > No matching variant of project :data was found. The consumer was configured to find a library for use during compile-time, compatible with Java 21, preferably in the form of class files, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' but: - Variant 'debugApiElements' declares a library for use during compile-time, 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 'jvm' ...(其余错误内容省略)
domain模块原配置:
plugins { alias(libs.plugins.jetbrains.kotlin.jvm) } java { sourceCompatibility = JavaVersion.VERSION_21 targetCompatibility = JavaVersion.VERSION_21 } tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach { kotlinOptions { jvmTarget = "21" } } dependencies { implementation(projects.data) }
data模块原配置:
plugins { alias(libs.plugins.android.library) alias(libs.plugins.jetbrains.kotlin.android) } android { namespace = "com.your.mom" compileSdk = 34 defaultConfig { minSdk = 28 testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles("consumer-rules.pro") } buildTypes { release { isMinifyEnabled = false proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" ) } } compileOptions { sourceCompatibility = JavaVersion.VERSION_21 targetCompatibility = JavaVersion.VERSION_21 } kotlinOptions { jvmTarget = "21" } } dependencies { implementation(libs.androidx.core.ktx) implementation(libs.androidx.appcompat) implementation(libs.material) testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.espresso.core) }
核心原因
纯JVM模块(domain)的Gradle变体属性为org.jetbrains.kotlin.platform.type = jvm,而Android库模块(data)的所有变体属性都是androidJvm。两者平台类型不兼容,导致Gradle无法找到匹配的依赖变体。
解决方案
方案1:将domain改为Android库模块
直接把domain模块从纯JVM转为Android库,统一平台类型,这是最简便的解决方式:
修改domain的build.gradle:
plugins { alias(libs.plugins.android.library) alias(libs.plugins.jetbrains.kotlin.android) } android { namespace = "你的domain模块命名空间" // 需补充命名空间 compileSdk = 34 // 与data模块保持一致 defaultConfig { minSdk = 28 // 与data模块保持一致 consumerProguardFiles("consumer-rules.pro") // 可选,按需配置 } compileOptions { sourceCompatibility = JavaVersion.VERSION_21 targetCompatibility = JavaVersion.VERSION_21 } kotlinOptions { jvmTarget = "21" } } dependencies { implementation(projects.data) }
方案2:拆分data模块为JVM核心层+Android适配层
如果需要保持domain的纯JVM特性(比如要跨平台复用),可以拆分data模块:
- 创建
data-core纯JVM模块,迁移data中不依赖Android API的逻辑(如数据解析、业务逻辑、网络请求封装等) - 修改原data模块依赖
data-core:implementation(projects.dataCore) - 修改domain模块依赖
data-core:implementation(projects.dataCore)
这样domain只依赖纯JVM的data-core,避免跨平台冲突。
总结
- 方案1适合大多数Android单平台项目,实现简单,无额外模块维护成本
- 方案2适合需要跨平台复用domain层的场景(如同时开发Android和桌面端应用)
内容的提问来源于stack exchange,提问作者No_Name
相关产品推荐
相关产品推荐

