Maaz Zindani

Loading the index…

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

Kotlin and Java compile to different JVM targets in a React Native Android build

ErrorTask :react-native-gradle-plugin:compileKotlin FAILED

Short answer

Gradle refuses to build when the Java task and the Kotlin task target different JVM versions. Kotlin falls back to 1.8 when you set nothing, while Java follows the JDK running Gradle. Set one JDK toolchain so both tasks use the same version, and run Gradle on the JDK React Native recommends, which is 17.

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

Fix it fast

  1. 1Find out which cause you have

    Find out which JDK Gradle itself is running on. The mismatch always starts from that number.

    cd android
    ./gradlew --version
    echo $JAVA_HOME
  2. 2Match what you see, then jump to its fix

    1. 1One task reports target 1.8 and the other reports your JDK versionNothing is set, so Kotlin defaults to 1.8 and Java follows the JDKFix
    2. 2Both numbers are explicit in your gradle files, for example Java 8 and Kotlin 11You set different values for Java and KotlinFix
    3. 3./gradlew --version shows a JDK newer than 17Gradle runs on a JDK newer than React Native supportsFix

What does the error look like?

  • Task :react-native-gradle-plugin:compileKotlin FAILED
  • 'compileJava' task (current target is 1.8) and 'compileKotlin' task (current target is 11)

Cause 1 of 3

Nothing is set, so Kotlin defaults to 1.8 and Java follows the JDK

1Check

With no jvmTarget in the build script, Kotlin uses 1.8. The Java task uses the version of the JDK Gradle runs on. On any JDK other than 8 the two differ, and since Gradle 8.0 the plugin fails the build instead of warning.

./gradlew --version
grep -rn "jvmTarget\|sourceCompatibility\|targetCompatibility" build.gradle app/build.gradle

2Fix

Declare one toolchain in the module that fails. It sets the Kotlin jvmTarget to the toolchain version when you have not set it yourself.

// build.gradle.kts of the failing module
kotlin {
    jvmToolchain(17)
}

3Verify

Run the build again.

./gradlew assembleDebug

Confirmed by kotlinlang.org

Did this fix it?

Cause 2 of 3

You set different values for Java and Kotlin

1Check

The check pairs compileKotlin with compileJava. If jvmTarget and targetCompatibility are set to different values anywhere in the build, it fails.

grep -rn -E "jvmTarget|JavaVersion|targetCompatibility" --include=*.gradle --include=*.kts .

2Fix

Remove the separate values and use the single toolchain from the previous cause. Gradle 8.0.2 and later may also need the foojay toolchain resolver plugin so it can download the JDK.

// settings.gradle.kts
plugins {
    id("org.gradle.toolchains.foojay-resolver-convention") version("1.0.0")
}

3Verify

Run the build again.

./gradlew assembleDebug

Confirmed by kotlinlang.org

Did this fix it?

Cause 3 of 3

Gradle runs on a JDK newer than React Native supports

1Check

React Native documents JDK 17 and warns that higher versions may cause problems. See which JDK your shell and Android Studio use.

java -version
/usr/libexec/java_home -V

2Fix

Point JAVA_HOME at JDK 17 (the React Native docs install Zulu 17 on macOS) and stop the old Gradle daemons.

export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home
cd android && ./gradlew --stop

3Verify

Confirm Gradle now reports JDK 17.

./gradlew --version

Confirmed by reactnative.dev

Did this fix it?

How do I stop it coming back?

  • Pin one JDK per project (a toolchain) instead of relying on whatever JAVA_HOME the machine has.
  • Do not set kotlin.jvm.target.validation.mode to warning or ignore to silence this; the Kotlin docs say that only hides the mismatch.
  • Run ./gradlew --version in CI so the JDK is printed in every log.

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.