alex137
alex137

Reputation: 178

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

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).

Upvotes: 2

Views: 1105

Answers (2)

JDMcMillian
JDMcMillian

Reputation: 229

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

Upvotes: 2

tapchicoma
tapchicoma

Reputation: 407

You could access extension objects using following code:

(gradle as ExtensionAware).extra["myObject"]

Upvotes: 3

Related Questions