[英]How to parametrize Kotlin version in the plugins block of a build.gradle.kts script?
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.变量kotlinVersion
已正确打印。 Nevertheless it is not recognized in the plugins
block and the following error is risen:尽管如此,它在plugins
块中无法识别,并且出现以下错误:
e: /home/achadde/sources/kotlin/minichain/build.gradle.kts:6:27: Unresolved reference: kotlinVersion
How can I fix this?我怎样才能解决这个问题?
There is currenlty no way of accessing anything of the outer scope from inside the lambda passed to plugins
.当前无法从传递给plugins
的 lambda 内部访问外部作用域的任何内容。
In case you use IntelliJ it will show you a bit more information:如果您使用 IntelliJ,它会向您显示更多信息:
'val kotlinVersion: String' can't be called in this context by implicit receiver. 'val kotlinVersion: String' 不能在此上下文中被隐式接收器调用。 Use the explicit one if necessary.如有必要,请使用显式的。
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.您定义kotlinVersion
的外部作用域 ( this@Build_gradle
) 在this@plugins
作用域中不可用,因此您必须在 plugins lambda 中定义kotlinVersion
。
Since the extra
delegate isn't available there either you can't use it:由于extra
委托在那里不可用,您也不能使用它:
plugins {
val kotlinVersion = "1.3.61"
// ...
}
Unfortunately using a label does not work:不幸的是,使用标签不起作用:
val kotlinVersion by extra ("1.3.61")
plugins {
// ... Unresolved reference: kotlinVersion
kotlin("jvm") version this@Build_gradle.kotlinVersion
}
For whatever reason, the plugins
block can't read the outer scope, so what I did was added the following entry to my settings.gradle.kts
instead:无论出于何种原因, plugins
块无法读取外部作用域,所以我所做的是将以下条目添加到我的settings.gradle.kts
:
pluginManagement.resolutionStrategy.eachPlugin {
if (requested.id.id.startsWith("org.jetbrains.kotlin.")) {
useVersion("1.4.10")
}
}
With my build.gradle.kts
now as follows:现在我的build.gradle.kts
如下:
plugins {
kotlin("jvm")
kotlin("kapt")
}
println(kotlin.coreLibrariesVersion)
Note how the version can now be omitted.请注意现在如何省略版本。 The following print statement will print out the version we specified in our settings.gradle.kts
, if we ever want to access our Kotlin version further down in our build script.如果我们想在构建脚本中进一步访问 Kotlin 版本,以下打印语句将打印出我们在settings.gradle.kts
指定的版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.