AGP: How to tell gradle to pick a specific dependency variant

Viewed 132

Assume I have an app with two build types debug and release. And there is an artifact in the maven repository that was published using the gradle variants API. When you look into the .module file you can find

    {
      "name": "anotherBuildTypeVariantCustomApiPublication",
      "attributes": {
        "com.android.build.api.attributes.BuildTypeAttr": "anotherBuildType",
        "org.gradle.category": "library",
        "org.gradle.dependency.bundling": "external",
        "org.gradle.libraryelements": "aar",
        "org.gradle.usage": "java-api"
      },
      "files": [
        {
          "name": "libraryname-0.1-anotherBuildType.aar",
          "url": "libraryname-0.1-anotherBuildType.aar",
          ....
        }
      ]
    }

How to tell gradle to pick anotherBuildType variant but not having to create a new variant of my app?

Currently gradle complains with

Variant 'anotherBuildTypeVariantCustomApiPublication' capability com.example:libraryname:0.1:
         - Incompatible because this component declares an API of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'anotherBuildType' and the consumer needed a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug'
1 Answers

In the app's gradle file:

android {
    buildTypes {
        debug {
            matchingFallbacks = ['anotherBuildType']
        }
        release {
            matchingFallbacks = ['anotherBuildType']
        }
    }
}

As you might guess from looking at this, it's not dependency-specific. For each dependency, Gradle will try the app's build type, and then each fallback build type in turn, until it finds one that matches or fails to do so. Goddess help you if dependencies A and B both define build types C and D, but you need build type C for dependency A and build type D for dependency B.

Related