Run clean task before every build automatically in Gradle
Asked Answered
H

3

8

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

How can I trigger the clean task basically before everything?

Holman answered 8/1, 2017 at 11:16 Comment(0)
E
3

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
Enos answered 8/1, 2017 at 14:32 Comment(3)
the buildscriptRapine
@Xerus Which one and where? Could not get unknown property 'assembleRelease' for project ':app' of type org.gradle.api.Project.Prize
It needs to be within the tasks { } block. Which buildscript depends on which project you want to configure.Rapine
B
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

Blip answered 13/8, 2018 at 20:8 Comment(1)
That syntax does not look sound. What is this line supposed t do?Rapine
L
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
        }
    }
}
Laevorotation answered 29/8, 2019 at 2:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.