13

My app uses several gradle.kts scripts. I want to set one variable which would be global for everyone.

object Versions{
   val kotlin_version = "1.3.60-eap-25"
}

but it is not resolved:

 classpath ("org.jetbrains.kotlin:kotlin-gradle-plugin:$Versions.kotlin_version")
Nurseyit Tursunkulov
  • 8,012
  • 12
  • 44
  • 78

3 Answers3

8

Declared them in gradle.properties file:

kotlin_version = 1.3.60-eap-25

And use it everywhere by findProperty("kotlin_version ")

elect
  • 6,765
  • 10
  • 53
  • 119
3

in project level gradle the

ext.kotlin_version = '1.3.61'

Should just be:

val kotlinVersion by rootProject.extra { "1.3.61" }

And then you can access it by doing this:

implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:${rootProject.extra.get("kotlinVersion")}")

in your app module

John
  • 1,447
  • 15
  • 16
0

To have your property available in any build section (including the plugins) you can map it as a system property using the systemProp prefix in your gradle.properties file, like so:

systemProp.kotlinVersion = 1.9.0

and access it anywhere in your build.gradle.kts script(s), for example:

plugins {
    val kotlinVersion: String by System.getProperties()

    kotlin("android").version(kotlinVersion).apply(false)
}

For regular sections (e.g. dependencies) you may omit the systemProp. prefix and access the value specified in your gradle.properties (e.g. myVersion = 1.2.3) like so:

dependencies {
    val myVersion: String by project

    implementation("some.group:artifactId:$myVersion")
}
krevelen
  • 361
  • 3
  • 11