How to Get Current Buildtype in Android Gradle Configuration

How to get Current Build Type in Gradle

You can get the exact build type by parsing your applicationVariants:

applicationVariants.all { variant ->
buildType = variant.buildType.name // sets the current build type
}

A implementation could look like the following:

def buildType // Your variable

android {
applicationVariants.all { variant ->
buildType = variant.buildType.name // Sets the current build type
}
}

task myTask{
// Compare buildType here
}

Also you can check this and this similar answers.

Update

This answer by this question helped the questioner to settle the problem.

How to get current buildType in Android Gradle configuration

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 ->
"${buildType.name}Compile" project(path: ":${dependency.name}", configuration: buildType.name)
}
}
}
}

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:
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Library-Publication

Please note that the Android team is working on an improvement for this behaviour:
https://code.google.com/p/android/issues/detail?id=52962

How to get the buildtype in a build.gradle EARLY ON

I finally found a way (though its not a very clean one) to look through the array of gradle tasks:

def buildType = gradle.startParameter.taskNames.any{it.toLowerCase().contains("debug")}?"debug":"release"

How to check current build variant in Gradle?

You can call variant.name, or "${variant.name}" to treat as string(text).

You can also do this programmatically by calling BuildConfig.BUILD_TYPE (this is a generated constant that has the value "debug" in case of a debug build)



Related Topics



Leave a reply



Submit