Gradle Kotlin DSL:访问 settings.gradle.kts 中定义的对象
Gradle Kotlin DSL: access objects defined in settings.gradle.kts
我在 settings.gradle.kts
中定义了对象。如何从 build.gradle.kts
获取它们?
使用 groovy DSL,我可以将这些对象放入 gradle.ext
,但 gradle 对象似乎不支持 Kotlin DSL 中的额外内容(使用 Gradle 5.2).
您可以使用以下代码访问扩展对象:
(gradle as ExtensionAware).extra["myObject"]
我意识到这有点旧 post,但在过去的 2 周里我一直在努力解决这个问题。希望它能帮助下一个寻找这个答案的可怜的开发人员。这是一个如何完成的示例。
在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"))
- Gradle 版本 6.6.1
我在 settings.gradle.kts
中定义了对象。如何从 build.gradle.kts
获取它们?
使用 groovy DSL,我可以将这些对象放入 gradle.ext
,但 gradle 对象似乎不支持 Kotlin DSL 中的额外内容(使用 Gradle 5.2).
您可以使用以下代码访问扩展对象:
(gradle as ExtensionAware).extra["myObject"]
我意识到这有点旧 post,但在过去的 2 周里我一直在努力解决这个问题。希望它能帮助下一个寻找这个答案的可怜的开发人员。这是一个如何完成的示例。
在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"))
- Gradle 版本 6.6.1