7

I want to run a project "clean" before the assembleRelease task in Gradle.

How can I trigger the clean task basically before everything?

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Marian Klühspies
  • 15,824
  • 16
  • 93
  • 136

3 Answers3

2

In gradle you can use the dependsOn method.

B.dependsOn A

In this way:

  • task B depends on task A
  • gradle executes A task everytime before the B task execution.

In your case:

assembleRelease.dependsOn clean
Graham
  • 7,431
  • 18
  • 59
  • 84
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

Adding to this, what I needed to do was have this in the

    android {
    afterEvaluate { 
       assemble(*your task here*)debug clean
}

and now it works great

reidisaki
  • 1,525
  • 16
  • 29
0

Use following code to execute the clean task first per each build variant

project.afterEvaluate {
    android.applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def capitalizedVariant = variant.name.capitalize()
            def assembleVariantTask = project.tasks."assemble${capitalizedVariant}"
            assembleVariantTask.dependsOn clean
        }
    }
}
shizhen
  • 12,251
  • 9
  • 52
  • 88