Android Studio Artic Fox - Java and Kotlin JVM Target - 8 vs 11
Asked Answered
S

2

19

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?

Skolnik answered 7/8, 2021 at 8:44 Comment(1)
developer.android.com/studio/releases/…Willena
R
19

If you're using Android Studio Artic Fox 2020.3.1, the first choice is the preferred option.

compileOptions {
    sourceCompatibility JavaVersion.VERSION_11
    targetCompatibility JavaVersion.VERSION_11
}

kotlinOptions {
    jvmTarget = JavaVersion.VERSION_11
}

Now coming to the kotlin-stdlib, you can use the jdk8 version.

 implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_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.

Rubenstein answered 3/9, 2021 at 3:10 Comment(1)
When you say the preferred option, can you detail why and any sources you have that enforce that opinion please?Disinfect
Y
3

According to current documentation use java 8:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = JavaVersion.VERSION_1_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.

Yardman answered 30/10, 2021 at 21:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.