我有我在settings.gradle.kts
中定义的对象。我怎样才能从build.gradle.kts
那里得到它们?
使用时髦的 DSL,我可以将这些对象放在 gradle.ext
中,但 gradle 对象似乎不支持 Kotlin DSL 中的额外功能(使用 Gradle 5.2(。
可以使用以下代码访问扩展对象:
(gradle as ExtensionAware).extra["myObject"]
我意识到这是一个有点旧的帖子,但我只是在过去的两周里为此苦苦挣扎。 希望它能帮助下一个寻找这个答案的可怜的开发人员。 这是它如何完成的示例。
In 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"))
}
In 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"))
- Gradle 版本 6.6.1