How can I disable a task in build.gradle

AndroidGradlebuild.gradle

Android Problem Overview


I want to skip some tasks when I run gradle build. I know that it can be done from command line with -x:

gradle build -x unwantedTask 

My question is how can the same result be achieved in the build.gradle?

Android Solutions


Solution 1 - Android

You can try e.g.:

unwantedTask.enabled = false

Solution 2 - Android

Because I need to disable a bunch of tasks, so I use the following codes before apply plugin: in my build.gradle file:

tasks.whenTaskAdded {task ->
    if(task.name.contains("unwantedTask")) {
        task.enabled = false
    }
}

Solution 3 - Android

For a bit more generic approach, you can:

unwantedTask.onlyIf { <expression> }

For instance:

compileJava.onlyIf { false }

Advanced IDEs, like IDEA, through code completion, will give you a lots of what you can do on any given object in the build.gradle - it's just a Groovy script, after all.

Solution 4 - Android

As hinted to by @LukasKörfer in a comment, to really remove a task from the build, instead of just skipping it, one solution is to add this to your build script:

project.gradle.startParameter.excludedTaskNames.add('yourTaskName')

However this seems to remove the task for all subprojects.

Solution 5 - Android

Examples for Kotlin DSL (build.gradle.kts):

tasks.clean {
    isEnabled = false
}

tasks.getByName("MyTaskName") {
    onlyIf { System.getenv()["CI"] == "true" }
}

Solution 6 - Android

project.gradle.taskGraph.whenReady { graph ->
  project.tasks.findAll().forEach { task ->
    if (task.name.contains("<your-text>")) {
      task.enabled = false
    }
  }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNO127View Question on Stackoverflow
Solution 1 - AndroidOpalView Answer on Stackoverflow
Solution 2 - AndroidNO127View Answer on Stackoverflow
Solution 3 - AndroidOndra ŽižkaView Answer on Stackoverflow
Solution 4 - AndroidVic SeedoubleyewView Answer on Stackoverflow
Solution 5 - AndroidMahozadView Answer on Stackoverflow
Solution 6 - AndroidkolobokView Answer on Stackoverflow