我有一些在settings.gradle.kts
中定义的对象。如何从build.gradle.kts
获得它们?
使用普通的DSL,我可以将这些对象放在gradle.ext
中,但是gradle对象似乎在Kotlin DSL中并没有额外支持(使用Gradle 5.2)。
答案 0 :(得分:0)
您可以使用以下代码访问扩展对象:
(gradle as ExtensionAware).extra["myObject"]
答案 1 :(得分:0)
我意识到这个帖子有点老了,但在过去的两周里我一直在努力解决这个问题。希望它能帮助下一个寻找这个答案的可怜的开发人员。这是如何完成的示例。
在settings.gradle.kts
// // This works in settings.gradle
// gradle.ext.GLOBAL_VAR = "This is a global value"
// println("settings.gradle ::: " + gradle.GLOBAL_VAR)
// This works in settings.gradle.kts
val settingsValue = "This value was set in settings.gradle.kts"
if (gradle is ExtensionAware) {
(gradle as ExtensionAware).extra["GLOBAL_VAR"]=settingsValue
println("settings.gradle.kts ::: " + (gradle as ExtensionAware).extra.get("GLOBAL_VAR"))
}
在build.gradle.kts
// // This works in settings.gradle
// println("build.gradle ::: " + gradle.GLOBAL_VAR)
// This works in settings.gradle.kts
if (gradle is ExtensionAware) println("build.gradle.kts ::: " + (gradle as ExtensionAware).extra.get("GLOBAL_VAR"))