0

I have a doNecessaryJob task that depends on preProcessing task. If a certain condition in preProcessing is true, I want to stop the sync right away. How do I do that?

Part of my gradle is here:

task preProcessing() {
    Gradle gradle = getGradle()
    String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
    Pattern pattern
    if (tskReqStr.contains("assemble"))
        pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
    else
        pattern = Pattern.compile("generate(\\w+)(Release|Debug)")
    Matcher matcher = pattern.matcher(tskReqStr)
    if (matcher.find()) {
        // do processing here
    } else {
        println "Error, stopping the sync."
        // !STOP THE GRADLE SYNC HERE DUE TO ERROR!
    }
}
task doNecessaryJob() {
    dependsOn preProcessing

    // do necessary processing here that depends on variables from preProcessing task
}

I checked other solutions here in SO but all seem to point to the command line solution such as How to stop Gradle task execution in Android Studio?.

user1506104
  • 6,554
  • 4
  • 71
  • 89

1 Answers1

0

I found it. I used GradleException like so:

task preProcessing() {
    Gradle gradle = getGradle()
    String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
    Pattern pattern
    if (tskReqStr.contains("assemble"))
        pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
    else
        pattern = Pattern.compile("generate(\\w+)(Release|Debug)")
    Matcher matcher = pattern.matcher(tskReqStr)
    if (matcher.find()) {
        // do processing here
    } else {
        throw new GradleException("Error, stopping the sync.")  // <--- THE FIX
    }
}
user1506104
  • 6,554
  • 4
  • 71
  • 89