Search code examples
androidkotlinkotlin-multiplatform

How To Set Android JVM Target Compability in Kotlin Multiplatform Mobile Project


As the title of this question describes, I get a warning when building the Android app of my KMM project that hints me to set my JVM version to 11 as the following terminal output shows:

> Task :shared:compileDebugKotlinAndroid
'compileDebugJavaWithJavac' task (current target is 1.8) and 'compileDebugKotlinAndroid' task (current target is 11) jvm target compatibility should be set to the same Java version

But how can I do that? Setting it like you'd do it in a regular Android project doesn't seem to have an effect.

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> { kotlinOptions.jvmTarget = "11" }

Solution

  • You can add target compatability option in your compile task

    tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
        kotlinOptions.jvmTarget = JavaVersion.VERSION_11
    }
    

    and add compile options to your android plugin

    android {
        compileOptions {
            sourceCompatibility = JavaVersion.VERSION_11
            targetCompatibility = JavaVersion.VERSION_11
        }
    }