In the script below:

val kotlinVersion by extra ("1.3.61")

println("Version "+kotlinVersion)

plugins {
    kotlin("jvm") version kotlinVersion
}

The variable kotlinVersion is correctly printed. Nevertheless it is not recognized in the plugins block and the following error is risen:

e: /home/achadde/sources/kotlin/minichain/build.gradle.kts:6:27: Unresolved reference: kotlinVersion

我怎么才能解决这个问题呢?

推荐答案

简短回答:

目前无法从传递给plugins的lambda内部访问外部作用域的任何内容.

长长的答案:

In case you use IntelliJ it will show you a bit more information:

隐式接收方在此上下文中无法调用"val kotlinVersion:String".如有必要,使用显式.

The outer scope (this@Build_gradle) where you define kotlinVersion is not avaiable in the this@plugins scope so you have to define kotlinVersion inside the plugins lambda.

由于extra代表在那里不可用,您也不能使用它:

plugins {
    val kotlinVersion = "1.3.61"
    // ...
}

不幸的是,使用标签不起作用:

val kotlinVersion by extra ("1.3.61")

plugins {
    // ... Unresolved reference: kotlinVersion 
    kotlin("jvm") version this@Build_gradle.kotlinVersion
}

Kotlin相关问答推荐

如何在 Big Data 中使用Inc过滤器?

Gradle Jooq配置自定义生成器

"Kotlin中的表达式

为何Kotlin标准库中的AND和OR函数不能像&&和||一样进行短路运算?

创建包含 3 个相同项目的列表/使用返回类型重复

在协程上下文中重新抛出异常

Kotlin Path.useLines { } - 如何不获取 IOException("Stream closed")?

为什么 Kotlin 中的 Double 和 Long 类型不推荐直接转换为 Char?

如何在 jOOQ 中两次加入同一张表?

匹配在单词边界上包含特殊字符的变量字符串的正则表达式

如何在 Hibernate Panache 中进行部分搜索

JavaFX - 你如何在 Kotlin 中使用 MapValueFactory?

DatabaseManager_Impl 不是抽象的,不会覆盖 RoomDatabase 中的抽象方法 clearAllTables()

如何从 Firestore 查询中排除元素?

Kotlin DataBinding 将静态函数传递到布局 xml

从 Spring WebFlux 返回 Flux 返回一个字符串而不是 JSON 中的字符串数组

Jacoco在Gradle 7.0.2和Kotlin 1.5.10上失败

Kotlin:子构造函数如何使用其父构造函数的辅助构造函数?

大小写敏感性 Kotlin / ignoreCase

Kotlin:获取文件的扩展名,例如.txt