Android Studio built-in JRE is 11 version.
And Artic Fox allows to use Java 11 for compiling projects:
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
But we also have Kotlin options
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
}
What JVM target version should we set now?
jvmTarget = JavaVersion.VERSION_1_8
or jvmTarget = JavaVersion.VERSION_11
Kotlin library uses JDK 8:
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
kotlin-stdlib-jdk11
doesn’t exist yet
All next configurations works with Artic Fox:
#1
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_11
}
#2
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
}
#3
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
}
But what should we choose?
2
Answers
If you’re using Android Studio Artic Fox 2020.3.1, the first choice is the preferred option.
Now coming to the kotlin-stdlib, you can use the jdk8 version.
The kotlin-stdlib-jdk8 library is fully compatible with the JDK 11 SDK.
Alternatively, for Kotlin only projects you can also ignore the kotlin-stdlib-jdk8 dependency as the Gradle plugin will automatically add the necessary library sources during compilation.
According to current documentation use java 8:
Android Developer docs (Java8 Support) is silent wrt. regarding jvmTarget and Java 11.
Kotlin Docs says that 1.8 is the default jvmTarget as of Kotlin 1.5.
Also note that when creating a new Kotlin-based project in the current Android Studio Artic Fox 2021.3.1 Patch 3, it creates a build.gradle with jvmTarget 1.8.