Gradle: allow dependencies only for the desired option

I have a test application with three options:

  • dev: uses a local copy of the library at design time
  • qa: uses snapshot during QA
  • rc: Uses a pre-build to test release candidates.

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

I run Gradle and expect it to do the minimum amount of work needed to build exactly what I want:

 ./gradlew :test-app:connectedAndroidTestDevDebug 

However, the assembly fails because it tries to resolve dependencies for all assembly assemblies, and not just from what I create.

FAILURE: assembly failure with exception.

* Something went wrong:

There was a problem setting up the project: test-app.

Failed to 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 

SNAPSHOT, which is trying to use qa , does not exist yet, and this should be good because I am not trying to create a qa flavor. If this SNAPSHOT assembly is present, then everything works fine.

Questions:

  • Why are all assembly layouts with their own dependencies allowed?
  • How can I create only one fragrance without encountering this problem?
  • Is there a better way to do this to be more "Gradley"?
+8
android maven gradle
source share
2 answers

I finally solved this by checking the list of tasks and, if there is a task for the desired build option, adding a dependency.

 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' } 

Keep in mind that the task list will not contain dependent task names, so if you do not invoke a task containing the assembly style name, this will not work. This is just what I got.

+1
source share

I assume the assembly works if you manually exclude the task?

 gradle connectedAndroidTestDevDebug -x _qaDebugCompile 

It looks like the connectedAndroidTestDevDebug task has a dependency on the _qaDebugCompile task that causes your problem. I don’t know how your tasks are defined, but you can examine your dependencies using the Gradle built-in tasks gradle dependencies and gradle dependencyInsight . Perhaps this will give you a hint in the right direction:

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

You can learn more about task dependencies in the Gradle User Guide .

0
source share

All Articles