You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

同步React Native项目时Kotlin版本不兼容报错的解决求助

解决React Native编译Kotlin版本不兼容问题

执行任务:react-native-gradle-plugin:compileKotlin失败,模块使用不兼容的Kotlin版本编译。
该类从D:/Gradle/wrapper/dists/gradle-8.3-all/6en3ugtfdg5xnpx44z4qbwgas/gradle-8.3/lib/kotlin-stdlib-1.9.0.jar!/kotlin/collections/CollectionsKt___CollectionsKt.class加载。错误信息:D:\ReactNative\project_name\node_modules\react-native-gradle-plugin\src\main\kotlin\com\facebook\react\utils\TaskUtils.kt: (25, 7): 类'kotlin.collections.CollectionsKt__CollectionsKt'使用不兼容的Kotlin版本编译。其元数据的二进制版本为1.9.0,预期版本为1.6.0。

此错误出现在项目同步阶段,当前正在将SDK版本从33升级到34。


环境版本信息

android/build.gradle

buildscript {
    ext {
        buildToolsVersion = "34.0.0"
        minSdkVersion = 27
        compileSdkVersion = 34
        targetSdkVersion = 34
        kotlin_version = '1.9.0'
        if (System.properties['os.arch'] == "aarch64") {
            // For M1 Users we need to use the NDK 24 which added support for aarch64
            ndkVersion = "24.0.8215888"
        } else {
            // Otherwise we default to the side-by-side NDK version from AGP.
            ndkVersion = "21.4.7075529"
        }
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:8.1.1")
        classpath("com.facebook.react:react-native-gradle-plugin")
        classpath("de.undercouch:gradle-download-task:5.0.1")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

package.json

"dependencies": {
    "react":"18.2.0",
    "react-native":"0.73.0"
}

解决步骤

  1. 统一Kotlin适配版本
    React Native 0.73.0官方推荐搭配Kotlin 1.8.20,与当前使用的AGP 8.1.1、Gradle版本兼容性更好。修改android/build.gradle中的kotlin_version:
kotlin_version = '1.8.20'
  1. 强制统一Kotlin依赖版本
    在android/build.gradle的allprojects块中添加版本强制规则,避免依赖树中出现多版本冲突:
allprojects {
    repositories {
        google()
        mavenCentral()
    }
    configurations.all {
        resolutionStrategy.force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.20"
        resolutionStrategy.force "org.jetbrains.kotlin:kotlin-stdlib:1.8.20"
        resolutionStrategy.force "org.jetbrains.kotlin:kotlin-stdlib-common:1.8.20"
    }
}
  1. 清理缓存并重新构建
  • 清理Gradle缓存:
    ./gradlew cleanBuildCache
    
  • 删除本地依赖文件后重新安装:
    rm -rf node_modules package-lock.json
    npm install
    
  • 重新同步或编译项目:
    ./gradlew assembleDebug
    
  1. 修正Gradle版本兼容性
    AGP 8.1.1对应的Gradle版本范围是8.0-8.2,当前使用的Gradle 8.3超出兼容范围,修改gradle-wrapper.properties中的下载链接:
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-all.zip

内容的提问来源于stack exchange,提问作者KINJAL VIRANI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 22:22:05