将版本号移至 Gradle Kotlin DSL 中的变量

joh*_*ohn 4 gradle kotlin gradle-kotlin-dsl

我的build.gradle.kts包含我的依赖项,如下所示:

dependencies {
    implementation("io.insert-koin:koin-core:3.1.6")
    implementation("io.insert-koin:koin-test:3.1.6")
    testImplementation(kotlin("test"))
}
Run Code Online (Sandbox Code Playgroud)

如何将 移动3.1.6到局部变量(?),这样我就可以避免在多个地方重复它。

MFa*_*o23 6

如果您只想将其本地化,您可以向您的dependencies块添加一个值:

dependencies {
    val koinVersion = "3.1.6"

    implementation("io.insert-koin:koin-core:$koinVersion")
    implementation("io.insert-koin:koin-test:$koinVersion")
    testImplementation(kotlin("test"))
}
Run Code Online (Sandbox Code Playgroud)

如果您想在多个位置使用它,您可以extra在项目build.gradle.kts文件中添加一个值:

val koinVersion by extra { "3.1.6" }
Run Code Online (Sandbox Code Playgroud)

然后在应用程序的build.gradle.kts文件中,在使用之前导入它:

val koinVersion: String by rootProject.extra

dependencies {
    implementation("io.insert-koin:koin-core:$koinVersion")
    implementation("io.insert-koin:koin-test:$koinVersion")
    testImplementation(kotlin("test"))
}
Run Code Online (Sandbox Code Playgroud)