Kotlin "Inconsistent JVM-target compatibility" between compileJava and compileKotlin in CI
Gradle checks that the Java and Kotlin compile tasks in a module target the same bytecode. When compileJava and compileKotlin disagree (for example 1.8 vs 17), Gradle fails with "Inconsistent JVM-target compatibility". The fix is to set one target for both, ideally via a JDK toolchain.
What this error means
The build fails with "Inconsistent JVM-target compatibility detected for tasks 'compileJava' (1.8) and 'compileKotlin' (17)".
> Task :app:compileKotlin FAILED
'compileJava' task (current target is 1.8) and 'compileKotlin' task (current target is 17)
jvm target compatibility should be set to the same Java version.Common causes
Java and Kotlin targets set independently
Java sourceCompatibility is 1.8 while kotlinOptions.jvmTarget/compilerOptions.jvmTarget is 17 (or vice versa), so the two tasks disagree.
The runner JDK differs from the configured target
A different JDK provided by setup-java changes one task target while the other stays at a hardcoded value.
How to fix it
Use one JDK toolchain for both
Set a Kotlin/Java toolchain so both compile tasks target the same bytecode version.
kotlin {
jvmToolchain(17)
}
java {
toolchain { languageVersion.set(JavaLanguageVersion.of(17)) }
}Provision the matching JDK in CI
Give the runner the JDK the toolchain expects with setup-java so provisioning is deterministic.
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'How to prevent it
- Set a single
jvmToolchain(...)instead of separate Java and Kotlin targets. - Pin the CI JDK with setup-java to match the toolchain.
- Keep the same JDK version across all modules of the build.