React Native从0.64.1升级至最新版本后执行gradlew clean出现react-native-gradle-plugin:compileKotlin失败问题
The error you're hitting happens because your Android build has conflicting JVM target versions: compileJava is set to 1.8 while compileKotlin uses 11. Newer React Native versions require Java 11+ for builds, so we need to align all components to use Java 11 consistently. Here's how to fix it step by step:
Step 1: Update App-Level Build Configuration
Open android/app/build.gradle and modify the android block to enforce Java 11 for both Java and Kotlin compilation:
android { // ... keep your existing config here compileOptions { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 } kotlinOptions { jvmTarget = "11" } }
Step 2: Align Project-Level Build Settings
Open android/build.gradle and add these lines at the top level (outside of buildscript or allprojects blocks) to ensure all modules use the same JVM target:
tasks.withType(JavaCompile) { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach { kotlinOptions { jvmTarget = "11" } }
Step 3: Configure Gradle Properties
Open android/gradle.properties and add/modify these lines to point to your JDK 11 installation and set Kotlin's target explicitly:
org.gradle.java.home=C:\\Program Files\\Java\\jdk-11.0.14 kotlin.jvm.target=11 org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
Step 4: Update Gradle Wrapper (If Needed)
Newer React Native versions require a compatible Gradle version. Check android/gradle/wrapper/gradle-wrapper.properties and ensure the distribution URL matches the version recommended by your React Native release (for latest RN versions, this is typically 7.5+):
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
Step 5: Clean and Rebuild
Now run the clean command again, followed by a build to verify the fix:
cd android && ./gradlew clean cd .. && npx react-native run-android
This should resolve the JVM target mismatch error, as all compilation tasks will now use Java 11 consistently.
内容的提问来源于stack exchange,提问作者CHANDUKA SAMARASINGHE.

