Arthur Dent Arthur Dent - 26 days ago 42x
Android Question

How to get current buildType in Android Gradle configuration

I want to dynamically add a dependency in an Android Gradle project based on the current buildType. I know I can specify the buildType in the dependency:

compile project(path: ':lib1', configuration: 'debug')

But how can I use the current buildType to specify which variant of the library I want to import, so that a debug or release build automatically imports the debug or release variant of the library? What I want is something like this (where currentBuildType is a variable containing the name of the currently used buildType):

compile project(path: ':lib1', configuration: currentBuildType)

The library project I want to import has set
publishNonDefault true
, so all buildTypes are published.


I could not find a clean way to get the current build type during the configuration phase of Gradle. Instead I define the dependency for each build type separately like that:

debugCompile project(path: ':lib1', configuration: 'debug')
releaseCompile project(path: ':lib1', configuration: 'release')

If you have many build types and many project dependencies this can get very verbose, but it is possible to add a function to make the dependency a one-liner. You need to add this to your main Gradle build file:

subprojects {
    android {
        dependencies.metaClass.allCompile { dependency ->
            buildTypes.each { buildType ->
                "${}Compile" project(path: ":${}", configuration:

Then you can add project dependencies in your Gradle modules like this:

allCompile project(':lib1')

If you also use build flavors you would have to adapt the solution. See this link for a documentation of the feature:

Please note that the Android team is working on an improvement for this behaviour: