简体   繁体   中英

Run clean task before every build automatically in Gradle

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

How can I trigger the clean task basically before everything ?

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

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

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
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM