Gradle: 只解析所需变体的依赖关系

Gradle: Only resolve dependencies for the desired variant

我有一个具有三种风格的测试应用程序:


dependencies {
  devCompile project(':library')
  qaCompile 'com.example:library:1.0.0-SNAPSHOT@aar'
  rcCompile 'com.example:library:1.0.0@aar'
}

我 运行 Gradle,并期望它完成构建我想要的所需的最少工作量:

./gradlew :test-app:connectedAndroidTestDevDebug

但是,构建失败了,因为它试图解决所有构建风格的依赖关系,而不仅仅是我正在构建的那个。

FAILURE: Build failed with an exception.

* What went wrong:

A problem occurred configuring project ':test-app'.

Could not resolve all dependencies for configuration ':test-app:_qaDebugCompile'.

Could not find com.example:library1.0.0-SNAPSHOT.

Searched in the following locations:
     https://repo1.maven.org/maven2/com/example/library/1.0.0-SNAPSHOT/maven-metadata.xml
     https://repo1.maven.org/maven2/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.pom
     https://repo1.maven.org/maven2/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.aar
     http://oss.sonatype.org/content/repositories/snapshots/com/example/library/1.0.0-SNAPSHOT/maven-metadata.xml
     http://oss.sonatype.org/content/repositories/snapshots/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.pom
     http://oss.sonatype.org/content/repositories/snapshots/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.aar
     file:/opt/android-sdk-macosx/extras/android/m2repository/com/example/library/1.0.0-SNAPSHOT/maven-metadata.xml
     file:/opt/android-sdk-macosx/extras/android/m2repository/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.pom
     file:/opt/android-sdk-macosx/extras/android/m2repository/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.aar
     file:/opt/android-sdk-macosx/extras/google/m2repository/com/example/library/1.0.0-SNAPSHOT/maven-metadata.xml
     file:/opt/android-sdk-macosx/extras/google/m2repository/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.pom
     file:/opt/android-sdk-macosx/extras/google/m2repository/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.aar
 Required by:
     project-name:test-app:unspecified

qa flavor 试图解析的 SNAPSHOT 尚不存在,这应该没问题,因为我不是在尝试构建 qa flavor。如果存在该 SNAPSHOT 构建,则一切正常。

问题:

  1. 为什么所有构建风格都解决了它们的依赖关系?
  2. 我怎样才能只构建一种口味而不遇到这个问题?
  3. 有没有更好的方法来做到这一点"Gradley"?

如果您手动排除任务,我假设构建工作正常?

gradle connectedAndroidTestDevDebug -x _qaDebugCompile

看起来任务 connectedAndroidTestDevDebug 依赖于任务 _qaDebugCompile,这会导致您的问题。我不知道你的任务是如何定义的,但你可以使用 Gradle 的内置任务 gradle dependenciesgradle dependencyInsight 来研究你的依赖关系。也许这会给你一个正确方向的提示:

gradle dependencyInsight --dependency com.example:library:1.0.0-SNAPSHOT

您可以在 the gradle User's guide 中阅读有关任务依赖性的更多信息。

我最终通过检查任务列表解决了这个问题,如果有所需构建变体的任务,则添加依赖项。

dependencies {
  gradle.startParameter.taskRequests.each { taskRequest ->
    taskRequest.args.each { taskName ->
      String flavorName = "qa";
      if (taskName.toLowerCase().endsWith(flavorName+"debug") ||
          taskName.toLowerCase().endsWith(flavorName+"release")) {
        qaCompile 'com.example:my-library:1.0.0-SNAPSHOT@aar'
      }
    }
  }
  devCompile project(':localLibrary')
//qaCompile 'com.example:my-library:1.0.0-SNAPSHOT@aar' // What I used to do.
  rcCompile 'com.example:my-library:1.0.0@aar'
}

请记住,任务列表将不包含相关任务名称,因此如果您不调用包含您的构建风格名称的任务,它将不起作用。这只是我的工作。