Gradle Kotlin DSL: access objects defined in settings.gradle.kts

 ̄綄美尐妖づ 提交于 2021-01-05 13:23:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!