问题
I have objects I define in settings.gradle.kts
. How can I get them from build.gradle.kts
?
With the groovy DSL, I could put these objects in gradle.ext
, but the gradle object doesn't seem to support extra in the Kotlin DSL (using Gradle 5.2).
回答1:
You could access extension objects using following code:
(gradle as ExtensionAware).extra["myObject"]
回答2:
I realize this is a bit old of a post, but I just struggled with this for the last 2 weeks. Hopefully it'll help the next poor developer searching for this answer. Here is an example of how its done.
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 version 6.6.1
来源:https://stackoverflow.com/questions/54603853/gradle-kotlin-dsl-access-objects-defined-in-settings-gradle-kts