我在gradle.properties
中有一个属性:
answer = 42
build.gradle
中使用了哪个:
println "answer is ${answer}"
现在我想用Kotlin脚本替换Groovy以进行Gradle配置。如何以正确的方式访问属性?我现在在build.gradle.kts
做的是:
fun property(name: String) = properties[name] as String
println("answer is " + property("answer"))
这似乎不太方便。谢谢!
答案 0 :(得分:1)
尝试delegated property,如下所示:
val answer by project
println("answer is $answer")
在gradle脚本kotlin repo中有例子 https://github.com/gradle/gradle-script-kotlin/tree/master/samples/project-properties