lukstei lukstei - 19 days ago 7
Android Question

Add dependency to specific productFlavor and buildType in gradle

I'm wondering how to add dependency to specific productFlavor and buildType in gradle.
For example I have productFlavor

free
and build type
release
, how can I add a dependency on the
assembleFreeRelease
task?

I've tried many variants but neither works.

For example I tried:

task('release', dependsOn: assembleProductionRelease) {
}
// error: Could not find property 'assembleProductionRelease' on root project 'app'.


Or:

task('release', dependsOn: 'assembleProductionRelease') {
}


Here there is no error but the task is executed for every flavor and build type, very confusing.

Answer

These task are generated dynamically based on your Android plugin configuration. At the time of configuration they are not available to you yet. You can defer the creation of your task in two ways:

Wait until the project is evaluated.

afterEvaluate {
    task yourTask(dependsOn: assembleFreeRelease) {
        println "Your task"
    }
}

Lazily declaring the task dependency as String.

task yourTask(dependsOn: 'assembleFreeRelease') {
    println "Your task"
}
Comments