按照 Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 的一些回答我有一个包含
的Kotlin Gradle DSL脚本tasks.compileKotlin {
sourceCompatibility = JavaVersion.VERSION_11.toString()
targetCompatibility = JavaVersion.VERSION_11.toString()
kotlinOptions {
jvmTarget = "11"
}
}
但是在升级到 Kotlin 1.7.0 之后我得到了异常
Unresolved reference: sourceCompatibility
Unresolved reference: targetCompatibility
我很清楚他们显然已经删除了这些,因为我发现它列在 https://kotlinlang.org/docs/whatsnew17.html#changes-in-compile-tasks
我的问题是,我应该用什么来代替它?如何保证兼容性?
最佳答案
设置 JVM 目标以及 Kotlin API 和语言版本可以通过配置所有 KotlinCompile
任务来完成。
// build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.7.0"
}
// configure all Kotlin compilation tasks,
// using the Gradle configuration avoidance API
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = "11"
apiVersion = "1.6"
languageVersion = "1.6"
}
// you can also add additional compiler args,
// like opting in to experimental features
kotlinOptions.freeCompilerArgs += listOf(
"-opt-in=kotlin.RequiresOptIn",
)
}
apiVersion
将声明的使用限制在指定版本的捆绑库中languageVersion
表示编译后的代码将兼容指定的 Kotlin 版本
All the compiler options are documented here .还有其他构建工具的附加文档,例如 Maven 和 Ant。
您可以使用新的工具链功能来设置将用于编译项目的 Java 版本。
// build.gradle.kts
kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of("11"))
}
}
关于kotlin - 将 Gradle 构建升级到 Kotlin 1.7.0 后为 "Unresolved reference: sourceCompatibility",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72670084/