如果满足特定条件,如何停止Gradle同步

时间:2019-12-23 07:34:19

标签: android gradle

我有一个doNecessaryJob任务,该任务取决于preProcessing任务。如果preProcessing中的某个条件为true,我想立即停止同步。我该怎么办?

我的部分礼物在这里:

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
}

我在SO中检查了其他解决方案,但似乎所有解决方案都指向诸如How to stop Gradle task execution in Android Studio?之类的命令行解决方案。

2 个答案:

答案 0 :(得分:0)

我找到了。我像这样使用GradleException

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

答案 1 :(得分:0)

您可以在终端中调用./gradlew --stop,它将杀死所有gradle进程