How can I disable a task in build.gradle

You can try e.g.:

unwantedTask.enabled = false

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

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.


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.