首页 文章

如何在具有相互依赖性的多模块Android Gradle项目中设置依赖项?

提问于
浏览
17

问题

如何在Android Gradle项目中设置模块的依赖关系,模块之间具有相互依赖关系,这样我可以一次性部署所有模块的新快照或发布版本的构建?

项目设置

我有一个由2个模块组成的项目:lib-core和lib-help,lib-help取决于lib-core . 我想将lib-core和lib-help分别发布到Maven Central,这样lib-help的pom文件就会指定lib-core与正确版本的依赖关系 .

该项目的结构如下:

/my-project-name
|
|--/lib-core
|  |-- build.gradle
|  |-- gradle.properties
|
|--/lib-help
|  |-- build.gradle
|  |-- gradle.properties
|
|-- build.gradle
|-- gradle.properties
|-- settings.gradle
|-- gradle-mvn-push.gradle

项目配置文件如下,仅显示相关部分(根据我的判断):

settings.gradle

include ':lib-core', ':lib-help'

gradle.properties

GROUP=com.company
VERSION=5.0.0-SNAPSHOT

gradle-mvn-push.gradle

apply plugin: 'maven'

afterEvaluate { project ->
    uploadArchives {
        repositories {
            mavenDeployer {
                // GROUP is defined in <root>/gradle.properties
                pom.groupId = GROUP
                // ARTIFACT is defined in <root>/<module>/gradle.properties
                pom.artifactId = ARTIFACT
                // VERSION is defined in <root>/gradle.properties
                pom.version = VERSION
            }
        }
    }
}

// Contains a lot more to fill in other meta-data like project name,
// developer name, github url, javadoc, signing of the artifacts, etc, etc..

lib-core/gradle.properties

ARTIFACT=my-lib-core

lib-core/build.gradle

apply plugin: 'com.android.library'

// Contains more code to configure the android stuff, not relevant for this question

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

apply from: rootProject.file('gradle-mvn-push.gradle')

lib-help/gradle.properties

ARTIFACT=my-lib-help

lib-help/build.gradle

apply plugin: 'com.android.library'

// Contains more code to configure the android stuff, not relevant for this question

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':lib-core')
}

apply from: rootProject.file('gradle-mvn-push.gradle')

问题

当我运行 gradlew clean :lib-core:uploadArchives :lib-help:uploadArchives 时,一切都很好地编译,上传被推送到正确的存储库 . 在Nexus Repository Manager中,我可以看到具有正确名称,版本,签名等的两个模块的工件,但是lib-help对生成的pom.xml中的lib-core的依赖性是错误的 . 它使用项目名称作为groupId,模块名称作为artifactId和未指定的版本名称:

<dependency>
    <groupId>my-project-name</groupId>
    <artifactId>lib-core</artifactId>
    <version>unspecified</version>
    <scope>compile</scope>
</dependency>

因此,使用lib-help作为依赖项的项目无法解析lib-core依赖项 . 应该在lib-help的pom.xml中的正确值是groupId = com.company,artifactId = my-lib-core和version = 5.0.0-SNAPSHOT .

为了解决这个问题,我想我会在lib-help中使用maven依赖,而不是模块依赖,如下所示:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.company:my-lib-core:' + VERSION
}

当然,在执行lib-core的uploadArchives命令之前,该版本不存在,但gradle希望在开始执行任何给定命令之前解析所有模块的依赖关系 . 因此,它无法解决此依赖关系,并在配置gradle项目时退出并出现错误 . 现在,我可以通过暂时将lib-help的依赖关系设置为 project(':lib-core') ,然后释放lib-core,然后将lib-help的依赖关系调整为 'com.company:my-lib-core:5.0.0-SNAPSHOT' ,然后释放lib-help来解决这个问题,但这似乎不是正确的方法 . 它会引发人为错误(如果我不小心增加了uploadArchives命令之间的版本号怎么办?)它需要几个手动的,严格按顺序排列的步骤 .

问题(重复)

如何在Android Gradle项目中设置模块的依赖关系,模块之间具有相互依赖关系,这样我可以一次性部署所有模块的新快照或发布版本的构建? (例如,但不一定,使用命令 gradlew clean :lib-core:uploadArchives :lib-help:uploadArchives

1 回答

  • 11

    如果在上传之前重写POM,则可以保留本地项目依赖项( compile project(':core') ):

    afterEvaluate { project ->
      uploadArchives {
        repositories {
          mavenDeployer {
            // ... other config here ...
    
            pom.whenConfigured { pom ->
              pom.dependencies.forEach { dep ->
                if (dep.getVersion() == "unspecified") {
                  dep.setGroupId(GROUP)
                  dep.setVersion(VERSION_NAME)
                }
              }
            }
          }
        }
      }
    }
    

    以下是工作项目的示例:https://github.com/hannesstruss/WindFish

相关问题