React Native Android构建失败:react-native-navigation Kotlin编译错误
我在构建React Native Android应用时遇到了如下编译错误,本人并非React Native专家,以下是我的配置信息及已尝试的方案,求解决办法:
依赖配置
"dependencies": { "@react-native-async-storage/async-storage": "^1.15.4", "@react-native-community/blur": "https://github.com/flyingSAP/react-native-blur", "@react-native-community/push-notification-ios": "^1.10.1", "@react-native-firebase/app": "^11.5.0", "@react-native-firebase/messaging": "^11.5.0", "@sentry/react-native": "^2.4.2", "axios": "^0.21.1", "hermes-engine": "0.10.0", "jwt-decode": "^3.1.2", "oidc-client": "^1.11.5", "patch-package": "^6.4.7", "postinstall-postinstall": "^2.1.0", "prop-types": "^15.7.2", "qs": "^6.10.1", "react": "^17.0.2", "react-devtools": "^4.24.7", "react-dom": "16.13.1", "react-native": "^0.64.4", "react-native-actionsheet": "^2.4.2", "react-native-app-auth": "^6.4.3", "react-native-auth0": "^2.8.3", "react-native-check-version": "^1.0.18", "react-native-contacts": "^7.0.0", "react-native-device-info": "^10.2.1", "react-native-dotenv": "^2.5.5", "react-native-eventemitter": "^0.0.1", "react-native-gesture-handler": "~1.10.2", "react-native-i18n": "^2.0.15", "react-native-image-picker": "^5.6.1", "react-native-keyboard-aware-scroll-view": "^0.9.4", "react-native-modal-dropdown": "^1.0.0", "react-native-navigation": "7.27.0", "react-native-option-menu": "^1.1.3", "react-native-phone-number-input": "^2.1.0", "react-native-push-notification": "^8.1.1", "react-native-reanimated": "~2.1.0", "react-native-render-html": "^5.1.0", "react-native-safe-area-context": "^3.2.0", "react-native-screens": "~3.0.0", "react-native-sha256": "^1.4.7", "react-native-svg": "^12.1.1", "react-native-svg-transformer": "^0.14.3", "react-native-vector-icons": "^9.2.0", "react-native-webview": "^11.17.2", "react-redux": "^7.2.4", "redux": "^4.1.0", "redux-oidc": "^4.0.0-beta1", "redux-thunk": "^2.3.0", "shell-quote": "1.7.3" },
构建脚本
ext { kotlinVersion = "1.6.10" RNNKotlinVersion = kotlinVersion minSdkVersion = 21 compileSdkVersion = 33 targetSdkVersion = 33 // buildToolsVersion = "31.0.0" supportLibVersion = "28.0.0" ndkVersion = "20.1.5948944" }
错误输出
FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':react-native-navigation:compileReactNative63DebugKotlin'. > Compilation error. See log for more details * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 13s at makeError (/Users/sefakose/repos/irecommend-mobile/node_modules/execa/index.js:174:9) at /Users/sefakose/repos/irecommend-mobile/node_modules/execa/index.js:278:16 at processTicksAndRejections (node:internal/process/task_queues:96:5) at async runOnAllDevices (/Users/sefakose/repos/irecommend-mobile/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/runOnAllDevices.js:94:5) at async Command.handleAction (/Users/sefakose/repos/irecommend-mobile/node_modules/@react-native-community/cli/build/index.js:186:9)
已尝试的解决方案
- 将React Native升级至0.64.4
- 尝试使用Kotlin版本1.6.0和1.6.10
- 删除.gradle文件夹后重新构建
- 执行
./gradlew clean命令 - 将react-native-navigation升级至7.27.0
解决建议
查看详细编译日志
执行./gradlew assembleDebug --stacktrace或./gradlew assembleDebug --info,获取Kotlin编译的具体错误信息,比如语法错误、依赖冲突等,这是定位问题的关键。检查版本兼容性
react-native-navigation 7.x系列对RN版本有明确要求,确认7.27.0是否完全适配RN 0.64.4,可尝试调整到官方推荐的匹配版本。调整Kotlin版本
尝试将Kotlin版本降级到1.5.31,部分RN导航版本对高版本Kotlin兼容性不佳。修改构建脚本中的kotlinVersion和RNNKotlinVersion为"1.5.31",清理缓存后重新构建。排查依赖冲突
执行npx react-native info查看所有依赖状态,排查第三方库与react-native-navigation的依赖冲突,尤其是AndroidX或Kotlin相关的依赖。重新安装依赖
删除node_modules文件夹和package-lock.json/yarn.lock,执行npm install或yarn install,再执行./gradlew clean assembleDebug。确认AndroidX配置
确保android/gradle.properties中已正确配置:android.useAndroidX=true android.enableJetifier=true
内容的提问来源于stack exchange,提问作者Sefa

