升级React Native 0.70.6后Android编译Kotlin实验性API报错求助
升级React Native至0.70.6后Android编译失败问题解决
问题情况
将React Native升级到0.70.6版本后,Android端持续编译失败,报错信息如下:
> Task :react-native-gradle-plugin:compileKotlin FAILED e: /Users/hugohouyez/code/hugoh1995/Superprof/node_modules/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/BundleHermesCTask.kt: (137, 11): This declaration is experimental and its usage must be marked with '@kotlin.ExperimentalStdlibApi' or '@OptIn(kotlin.ExperimentalStdlibApi::class)'
此前因未安装依赖时提示找不到该依赖,已在package.json中手动安装了"react-native-gradle-plugin": "^0.71.10",尽管官方文档未提及此操作。
当前相关配置
项目根目录build.gradle
... dependencies { classpath("com.android.tools.build:gradle:7.3.0") classpath("com.facebook.react:react-native-gradle-plugin") classpath("de.undercouch:gradle-download-task:5.0.1") classpath("com.google.gms:google-services:4.3.10") // 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-7.4-all.zip
解决步骤
卸载不兼容的高版本插件
执行命令:npm uninstall react-native-gradle-plugin安装与RN0.70.6匹配的插件版本
RN 0.70.x系列对应的react-native-gradle-plugin版本应为0.70.x,执行:npm install react-native-gradle-plugin@0.70.6 --save-dev清理缓存并重新编译
- 进入Android目录清理gradle缓存:
cd android && ./gradlew clean - 返回项目根目录重新编译:
npm run android
- 进入Android目录清理gradle缓存:
补充说明
当前配置中的Android Gradle Plugin(7.3.0)和Gradle(7.4)版本与RN0.70.6的要求匹配,无需调整。核心问题是手动安装的高版本插件与RN版本不兼容,导致Kotlin编译时出现实验性API未标记的错误。
内容的提问来源于stack exchange,提问作者Hugo
相关产品推荐
相关产品推荐

