React Native项目构建失败求助:依赖与资源合并报错
React Native Android构建失败问题修复方案
我负责的React Native项目2023年1月可正常构建,如今执行构建时失败,先后出现两类错误:
初始版本配置
ext { buildToolsVersion = "31.0.0" minSdkVersion = 21 compileSdkVersion = 31 targetSdkVersion = 31 ndkVersion = "21.4.7075529" playServicesVersion = "17.0.0" // or find latest version androidMapsUtilsVersion = "2.2.4" kotlin_version = "1.6.0" }
错误1
* What went wrong: Execution failed for task ':app:checkDebugAarMetadata'. > A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction > The minCompileSdk (33) 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.appcompat:appcompat-resources:1.6.1. AAR metadata file: /home/xyz/.gradle/caches/transforms-3/d7be3d1450d870047b15ae8f44694206/transformed/jetified-appcompat-resources-1.6.1/META-INF/com/android/build/gradle/aar-metadata.properties.
将compileSdkVersion升级至33后,出现以下错误:
升级后版本配置
buildToolsVersion = "31.0.0" minSdkVersion = 21 compileSdkVersion = 33 targetSdkVersion = 33 ndkVersion = "21.4.7075529" playServicesVersion = "17.0.0" // or find latest version androidMapsUtilsVersion = "2.2.4" kotlin_version = "1.6.0"
错误2
* What went wrong: Execution failed for task ':app:mergeDebugResources'. > /home/xyz/.gradle/caches/transforms-3/1ab3f973c5a5d473246b969002b3ea52/transformed/material-1.8.0/res/values/values.xml: Error: Can't determine type for tag '<macro name="m3_comp_assist_chip_container_shape">?attr/shapeAppearanceCornerSmall</macro>'
我尝试过修改版本、调整Gradle版本、添加第三方库等多种解决方案,但均无效。以下是项目相关配置文件:
Android/build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext { buildToolsVersion = "33.0.2" minSdkVersion = 21 compileSdkVersion = 33 targetSdkVersion = 33 ndkVersion = "21.4.7075529" playServicesVersion = "17.0.0" // or find latest version androidMapsUtilsVersion = "2.2.4" kotlin_version = "1.6.0" } repositories { google() mavenCentral() } dependencies { classpath("com.android.tools.build:gradle:4.2.2") // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files classpath 'com.google.gms:google-services:4.3.10' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { exclusiveContent { // We get React Native's Android binaries exclusively through npm, // from a local Maven repo inside node_modules/react-native/. // (The use of exclusiveContent prevents looking elsewhere like Maven Central // and potentially getting a wrong version.) filter { includeGroup "com.facebook.react" } forRepository { maven { url "$rootDir/../node_modules/react-native/android" } } } mavenCentral() 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() maven { url 'https://www.jitpack.io' } } }
gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-6.9-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists
有效修复方案
1. 升级Gradle插件与Gradle版本
旧版Gradle插件(4.2.2)不支持Android 13(API 33)引入的<macro>资源语法,必须升级:
- 修改
Android/build.gradle中com.android.tools.build:gradle版本为7.4.2:classpath("com.android.tools.build:gradle:7.4.2") - 修改
gradle-wrapper.properties中的distributionUrl为对应版本的Gradle:distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
2. 升级Kotlin版本
Gradle插件7.x需要Kotlin版本≥1.7.x,修改Android/build.gradle中的kotlin_version:
kotlin_version = "1.7.20"
3. (可选)锁定Material库版本
若升级后仍有冲突,可强制使用兼容的Material版本,在app/build.gradle中添加:
configurations.all { resolutionStrategy { force 'com.google.android.material:material:1.7.0' } }
4. 清理缓存并重新构建
执行以下命令清除旧缓存:
./gradlew clean rm -rf ~/.gradle/caches npm run android
内容的提问来源于stack exchange,提问作者Bilal Yaqoob
相关产品推荐
相关产品推荐

