I’m using Kotlin to build an application that’s distributed to users with older JVM versions, while still using modern language features (I don’t want to be stuck on Java 6).
By targeting JVM 1.6, I’m able to run the application on older JVMs. However, the only way I can make sure I also don’t use later JDK APIs, e.g. Path
, java.time
, etc. is by doing the compilation on JDK 1.6. Otherwise, the compilation succeeds, and then causes a runtime exception only when run on older JVMs due to missing classes.
Here’s a minimal build.gradle:
plugins {
id "org.jetbrains.kotlin.jvm" version "1.2.41"
}
repositories { jcenter() }
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib"
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
jvmTarget = "1.6"
}
}
When compiling using JDK 1.8, I can successfully compile:
import java.time.Instant
fun main(args: Array<String>) {
println(Instant.now())
}
For this use case, am I supposed to run Gradle and therefore compile the Kotlin files using JDK 1.6?