In Kotlin project, what is a proper Gradle script to make sure my classes will be compiled to byte code ver 52 (Java 8)?
For some reason my classes are compiled as v
In your build.gradle.kts
:
tasks {
val java = "11"
compileKotlin {
kotlinOptions { jvmTarget = java }
sourceCompatibility = java
}
}
Or like this:
tasks {
withType<KotlinCompile> { kotlinOptions { jvmTarget = "11" } }
}
Kotlin 1.0 always produces Java 6 class files. Kotlin 1.1 will support generating Java 8 class files by passing -jvm-target 1.8
to the compiler. See
https://blog.jetbrains.com/kotlin/2016/07/first-glimpse-of-kotlin-1-1-coroutines-type-aliases-and-more/
for a discussion of Java 7/8 support.
thx for seanf's anwser ,but if your DSL kotlin is 1.3.0:
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class.java).all {
kotlinOptions {
jvmTarget = "1.8"
}
}