Gradle + Kotlin 为什么我们不直接给变量赋值

Gradle + Kotlin why don't we assign variables directly

build.gradle.kts中有如下代码:

buildscript {
extra.apply {
    set("kotestVersion", "4.6.1")
    set("jdbi3Version", "3.21.0")
    set("resilience4jVersion", "1.7.1")
}
}
val kotestVersion: String by extra
val jdbi3Version: String by extra
val resilience4jVersion: String? by extra

为什么我们需要以这种间接的方式赋值?有没有理由我们不直接给变量赋值,比如

val kotestVersion: String = "4.6.1"

假设你的意思是为什么需要使用set方法,那是因为这些是Gradle Lazy Properties, and not primitive types like string or int. The Groovy syntax makes it possible for direct assignments, but Kotlin doesn’t. There’s an open ticket about it: https://github.com/gradle/gradle/issues/9268