Home > database >  Move version number to a variable in Gradle Kotlin DSL
Move version number to a variable in Gradle Kotlin DSL

Time:04-22

My build.gradle.kts contains my dependencies like this:

dependencies {
    implementation("io.insert-koin:koin-core:3.1.6")
    implementation("io.insert-koin:koin-test:3.1.6")
    testImplementation(kotlin("test"))
}

How can I move the 3.1.6 to a local variable (?) so I can avoid duplicating it in several places.

CodePudding user response:

If you just want it local, you can add a value to your dependencies block: dependencies { val koinVersion = "3.1.6"

implementation("io.insert-koin:koin-core:$koinVersion")
implementation("io.insert-koin:koin-test:$koinVersion")
testImplementation(kotlin("test"))

}

If you want to use it multiple spots, you can add an extra value in your project's build.gradle.kts file:

val koinVersion by extra { "3.1.6" }

then in the app's build.gradle.kts file, you import it before using it:

val koinVersion: String by rootProject.extra

dependencies {
    implementation("io.insert-koin:koin-core:$koinVersion")
    implementation("io.insert-koin:koin-test:$koinVersion")
    testImplementation(kotlin("test"))
}
  • Related