React Native 0.66.3执行yarn android编译失败,急求解决方案
问题背景
基于React Native 0.66.3的项目,11月4日前可正常构建,执行yarn android时突然报错,尝试多种方案后仍无法解决,已停滞4天且临近交付期限。
初始错误信息
- What went wrong:
Execution failed for task ':app:checkDebugAarMetadata'.A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction
The minCompileSdk (31) specified in a
dependency's AAR metadata (META-INF/com/android/build/gradle/aar-metadata.properties)
is greater than this module's compileSdkVersion (android-30).
Dependency: androidx.activity:activity-compose:1.4.0.
AAR metadata file: /Users/dev/.gradle/caches/transforms-2/files-2.1/c35837abe789834d04f87678613d3ab9/jetified-activity-compose-1.4.0/META-INF/com/android/build/gradle/aar-metadata.properties.
- 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.
升级compileSdkVersion到31后的新错误
Execution failed for task ':react-native-incall-manager:compileDebugJavaWithJavac'.
Compilation failed; see the compiler error output for details.
当前根build.gradle配置
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext { buildToolsVersion = "30.0.2" minSdkVersion = 21 compileSdkVersion = 31 targetSdkVersion = 31 androidXCore = "1.7.0" } repositories { google() jcenter() } dependencies { classpath("com.android.tools.build:gradle:4.2.1") // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { mavenLocal() maven { // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm url("$rootDir/../node_modules/react-native/android") } maven { // Android JSC is installed from npm url("$rootDir/../node_modules/jsc-android/dist") } google() jcenter() maven { url 'https://www.jitpack.io' } } }
解决方案
方案1:锁定依赖版本,无需升级compileSdk
在根android/build.gradle的allprojects块中添加依赖强制约束,将androidx.activity:activity-compose锁定到兼容compileSdk 30的版本:
allprojects { repositories { // 保留原有仓库配置 } // 添加以下内容 configurations.all { resolutionStrategy { force 'androidx.activity:activity-compose:1.3.1' } } }
执行./gradlew clean清理构建缓存,再运行yarn android重新构建。
方案2:修复react-native-incall-manager编译问题(适配compileSdk 31)
- 升级buildToolsVersion到31.0.0,修改根build.gradle的ext配置:
buildToolsVersion = "31.0.0"
- 处理
react-native-incall-manager模块:- 优先升级该依赖到兼容RN 0.66和compileSdk 31的最新版本
- 若升级无效,手动修改
node_modules/react-native-incall-manager/android/build.gradle,将compileSdkVersion和targetSdkVersion改为31 - 执行
./gradlew compileDebugJavaWithJavac --stacktrace查看编译详情,修复Java语法或API适配问题(比如Android 12的权限兼容)
- 执行
./gradlew clean后重新构建。
方案3:清理缓存并重装依赖
- 清理包管理器缓存:
yarn cache clean
- 删除本地依赖和gradle缓存:
rm -rf node_modules android/.gradle
- 重新安装依赖:
yarn install
- 重新构建项目:
yarn android
内容的提问来源于stack exchange,提问作者Ravis Farooq

