androidandroid-studioandroid-gradle-pluginandroid-studio-arctic-fox

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?


Solution

  • 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.