kenny
kenny

Reputation: 1105

Gradle android: Where should I put my custom task?

I want to add a custom task to my Gradle build file.

Should this go inside the android configuration closure or not?

I tried both and they seem to work fine and I couldn't find any explicit mention in the docs.

Upvotes: 0

Views: 81

Answers (2)

Rene Groeschke
Rene Groeschke

Reputation: 28653

the task definition should usually not go into the android blog and on root level as zeventh suggests. though it sometimes useful to do this in the android block. especially when you create these tasks dynamically.

In the following example I create one task for each build variant:

android{
    applicationVariants.all{ variant ->
       def variantTask = task("${variant.name}CustomTask", type:CustomTask){
           ...
       }
       check.dependsOn variantTask
    }
}  

Upvotes: 1

Jeffrey Mixon
Jeffrey Mixon

Reputation: 13616

You should just put the task at the root level of the file, outside the android closure.

task yourTask << {
    println "Hello world"
}

Upvotes: 1

Related Questions