Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 633
user924 Avatar asked Aug 07 '21 08:08

user924


People also ask

Does Kotlin support Java 11?

 Yes. Kotlin is supported as a first-class language on Android.

Which version of Java is used in Android Studio?

A copy of the latest OpenJDK comes bundled with Android Studio 2.2 and higher, and this is the JDK version we recommend you use for your Android projects.

What is new in Android Studio Bumblebee?

Android Studio 2021.1 “Bumblebee” has a new Device Manager and improved Apple Silicon support. Android Studio is the main integrated development environment (IDE) for creating Android applications, with a feature-packed code editor, debugging tools, emulators, and much more.

What is the latest Android studio version?

Android Studio Chipmunk | 2021. With Android Studio Chipmunk Patch 2 and Android Gradle 7.2. 2, you can compile against Android 13 APIs by setting compileSdk=33 . The highest supported minimum SDK version is 32.


2 Answers

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.

like image 95
Bharath Vishal Avatar answered Sep 23 '22 18:09

Bharath Vishal


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.

like image 45
arberg Avatar answered Sep 23 '22 18:09

arberg