Android Gradle custom task per variant Android Gradle custom task per variant android android

Android Gradle custom task per variant


It happens because the logic is executed at configuration time. Try adding an action (<<):

android.applicationVariants.all { variant ->    task ("myCustomTask${variant.name.capitalize()}") << {        println "*** TEST ***"        println variant.name.capitalize()    }}


Following Opal's answer and the deprecation of << operator since Gradle 3.2, the correct answer should be:

android.applicationVariants.all { variant ->    task ("myCustomTask${variant.name.capitalize()}") {        // This code runs at configuration time        // You can for instance set the description of the defined task        description = "Custom task for variant ${variant.name}"        // This will set the `doLast` action for the task..        doLast {            // ..and so this code will run at task execution time            println "*** TEST ***"            println variant.name.capitalize()        }    }}