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

How to setup dependencies in a multi module Android Gradle project with interdependencies?

问题

如何在 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 中,我可以看到具有正确名称、版本、签名等的两个模块的工件,但是生成的 pom.xml 中 lib-help 对 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来解决这个问题,但这似乎不是正确的方法。它会引发人为错误(如果我不小心增加了 to uploadArchives 命令之间的版本号怎么办?)并且它需要几个手动的、严格有序的步骤。

问题(重复)

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

如果在上传前重写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