This Android example build file包含代码段
buildTypes {
release {
minifyEnabled true
}
但是由于Gradle总是执行构建脚本中的所有configure语句,因此即使对于调试构建,这也不会始终将minifyEnabled设置为true吗?
我希望Gradle让我在一个任务中将变量设置为true,而在另一任务中将变量设置为false,并以不同的方式配置事物,但是我能够做到的唯一方法是等待taskGraph.whenReady。通常是这样的:
def myBool = false
task runs {
myBool = true
}
task doesNotRun {
myBool = false
}
task whoWins(dependsOn: runs) {
doLast { println "myBool is ${myBool}" }
}
gradle whoWins
:runs
:whoWins
myBool is false
最佳答案
配置步骤是为了帮助Gradle构建任务执行图(将实际运行的内容),这就是为什么要执行所有配置代码的原因。您所描述的内容听起来像是循环任务依赖性,其中任务取决于任务设置的变量,而该变量又取决于第一个任务。
Task A -> depends on Task B -> depends on variable from Task A
您可能正在寻找的是基于执行图配置任务的功能。请参阅Gradle用户指南,第6.13节Configure by DAG。使用它可以打破循环依赖。
Task A -> depends on Task B -> depends on gradle.taskGraph.whenReady
或者,您也可以创建一个新任务Task C,该任务根据任务图处理设置变量。
def myBool = false
task runs {}
task doesNotRun {}
task whoWins(dependsOn: runs) {
doLast { println "myBool is ${myBool}" }
}
task taskC << {
if (gradle.taskGraph.hasTask(runs)) {
myBool = true
} else if (gradle.taskGraph.hasTask(doesNotRun)) {
myBool = false
}
}
runs.dependsOn(taskC)
doesNotRun.dependsOn(taskC)
结果:
$ gradle whois
:taskC
:runs
:whoWins
myBool is true
关于gradle - 如何在配置步骤中将Gradle属性设置为不同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33108488/