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.
In your build. gradle (:app): tasks. all { Task task -> if (task.name == "preDebugBuild") { doFirst { //for debug build } } else if (task.name == "preReleaseBuild") { doFirst { //for release build } } } dependencies { ... }
A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.
Go to Android Studio -> Preferences -> Plugins (for Mac) and File -> Settings -> Plugins (for windows) and search “Check for Dependency updates plugin”. Install it and restart android studio. You will be able to see Dependencies tab on the right which will show if any dependency has a new update available.
In Android Studio, go to File > Project Structure. Then select the "project" tab on the left. Your Gradle version will be displayed here.
You can use
if (gradle.startParameter.taskNames.contains("assembleExample")) {
// do stuff
}
That variable will be set before the buildConfig
block is evaluated
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
Add a task which depends on each assembleXxx task and property setting up after it invoked
ext {
currentConfig = ""
}
task generateConfigProperty(dependsOn: 'installDebug') {
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def taskName = "taskindicate$output.name"
task "$taskName"() << {
project.ext.set("currentConfig", "$output.name")
}
output.assemble.dependsOn "taskindicate$output.name"
}
}
}
task getConfig(dependsOn: ['installDebug', 'generateConfigProperty']) << {
println("My config is $currentConfig")
}
took idea from the answer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With