Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Java and Kotlin compile to different JVM targets in an Android module

ErrorInconsistent JVM-target compatibility detected for tasks 'compileDebugJavaWithJavac' (1.8) and 'kaptGenerateStubsDebugKotlin' (17)

Short answer

Java and Kotlin tasks in the same module are compiling for different JVM targets, for example 1.8 and 17. Set one toolchain version, or set compileOptions and Kotlin jvmTarget to the same value, so both tasks agree.

  • Applies to Android Gradle plugin 8.x, Kotlin Gradle plugin 2.x, JDK 17
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print where the module sets its Java and Kotlin targets.

    grep -rn "jvmTarget\|jvmToolchain\|sourceCompatibility\|targetCompatibility\|toolchain" --include=*.gradle --include=*.gradle.kts .
  2. 2Match what you see, then jump to its fix

    1. 1The build file has no jvmToolchain or java toolchain block.No toolchain is set, so the two tasks pick different defaultsFix
    2. 2The build file sets compileOptions to one version and jvmTarget to another, or sets only one of them.compileOptions and Kotlin jvmTarget disagreeFix

What does the error look like?

  • Inconsistent JVM-target compatibility detected for tasks 'compileDebugJavaWithJavac' (1.8) and 'kaptGenerateStubsDebugKotlin' (17)

Cause 1 of 2

No toolchain is set, so the two tasks pick different defaults

1Check

The Kotlin docs say that with no explicit value, jvmTarget resolves to 1.8, while the Java target follows the JDK that runs Gradle. With JDK 17 the two disagree.

./gradlew --version

2Fix

Declare one toolchain in the module. The Android docs recommend always doing so; it supplies defaults for sourceCompatibility and targetCompatibility, and Kotlin follows it.

// app/build.gradle.kts
kotlin {
    jvmToolchain(17)
}

3Verify

Compile the debug variant.

./gradlew :app:compileDebugKotlin :app:compileDebugJavaWithJavac

Confirmed by kotlinlang.org

Did this fix it?

Cause 2 of 2

compileOptions and Kotlin jvmTarget disagree

1Check

The Android JDK docs say sourceCompatibility and targetCompatibility should generally have the same value, and that with Kotlin below 2.2 you must also set kotlinOptions jvmTarget.

grep -n -A4 "compileOptions\|kotlinOptions" app/build.gradle*

2Fix

Set all three to the same Java version.

android {
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }
}

3Verify

Run the debug build.

./gradlew :app:assembleDebug

Confirmed by developer.android.com

Did this fix it?

How do I stop it coming back?

  • Set the toolchain once in a shared build convention so every module gets the same JDK.
  • Use the same JDK version for Android Studio, the terminal and CI.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.