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

多模块Gradle构建错误:无法解析:data项目依赖

多模块Gradle依赖冲突问题解决

问题背景

我的项目采用多模块结构:

  • app模块
  • presentation模块(Android library Module)
  • domain模块(Kotlin library Module)
  • data模块(Android library Module)

期望的依赖关系为:presentation -> domain -> data

依赖配置

在Presentation模块的build.gradle中配置:

implementation(projects.domain)

在Domain模块的build.gradle中配置:

implementation(projects.data)

报错信息

同步Gradle时持续出现以下错误:

:domain:main: Could not resolve project :data.
Required by:
    project :domain

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

同时还有:

:domain:test: Could not resolve project :data.
Required by:
    project :domain

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

移除implementation(projects.data)后项目可正常构建,但我需要保留该依赖。详细错误日志如下:

* What went wrong:
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 8, 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'
              - Other compatible attributes:
                  - Doesn't say anything about how its dependencies are found (required its dependencies declared externally)
                  - Doesn't say anything about its elements (required them preferably in the form of class files)
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
          - Variant 'debugRuntimeElements' declares a library for use during runtime, 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'
              - Other compatible attributes:
                  - Doesn't say anything about how its dependencies are found (required its dependencies declared externally)
                  - Doesn't say anything about its elements (required them preferably in the form of class files)
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
          - Variant 'debugSourcesElements' declares a component for use during runtime, packaged as a jar, preferably optimized for Android, and its dependencies declared externally:
              - Incompatible because this component declares documentation, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' and the consumer needed a library, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm'
              - Other compatible attribute:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
          - Variant 'releaseApiElements' 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'
              - Other compatible attributes:
                  - Doesn't say anything about how its dependencies are found (required its dependencies declared externally)
                  - Doesn't say anything about its elements (required them preferably in the form of class files)
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
          - Variant 'releaseRuntimeElements' declares a library for use during runtime, 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'
              - Other compatible attributes:
                  - Doesn't say anything about how its dependencies are found (required its dependencies declared externally)
                  - Doesn't say anything about its elements (required them preferably in the form of class files)
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
          - Variant 'releaseSourcesElements' declares a component for use during runtime, packaged as a jar, preferably optimized for Android, and its dependencies declared externally:
              - Incompatible because this component declares documentation, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' and the consumer needed a library, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm'
              - Other compatible attribute:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)

问题根源

错误核心是domain模块是标准JVM库(纯Kotlin library),而data模块是Android库,两者的Kotlin平台类型不匹配:domain期望依赖的是jvm类型模块,但data提供的是androidJvm类型变体,导致Gradle无法找到兼容的依赖版本。

解决办法

有两种可行方案,根据你的需求选择:

方案1:将data模块改为纯Kotlin JVM库

如果data模块不需要调用Android专属API(比如Context、Android SDK类等),直接把它改成纯Kotlin库:

  1. 打开data模块的build.gradle文件
  2. 删除Android相关插件(比如com.android.library),替换为Kotlin JVM插件:
    plugins {
        id("org.jetbrains.kotlin.jvm") version "你的Kotlin版本号"
    }
    
  3. 删除所有Android专属配置块(比如android块、compileSdk、defaultConfig等)
  4. 同步Gradle即可解决问题

方案2:让domain模块兼容Android库依赖

如果data模块必须保留Android库类型(因为要用到Android API),需要修改domain模块的配置,使其接受androidJvm类型的依赖:
在domain模块的build.gradle中添加以下配置:

configurations.all {
    attributes.attribute(org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType.attribute,
        org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType.androidJvm)
}

或者更精准地针对编译和运行时配置:

configurations {
    compileClasspath {
        attributes.attribute(KotlinPlatformType.attribute, KotlinPlatformType.androidJvm)
    }
    runtimeClasspath {
        attributes.attribute(KotlinPlatformType.attribute, KotlinPlatformType.androidJvm)
    }
}

同步Gradle后即可正常解析依赖。

额外提醒

如果选择方案2,domain模块会间接引入Android相关依赖,建议确保domain模块中不要直接使用Android专属API,避免破坏领域层的跨平台特性(如果有这方面需求的话)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 23:23:10