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

React Native升级至0.76.2后Android构建遇actionBarSize重复资源错误

React Native 0.74.3 升级至0.76.2 Android构建actionBarSize资源重复错误解决指南

问题详情

升级React Native从0.74.3到0.76.2后,Android构建时抛出资源重复错误,核心报错信息如下:

/Users/xxx/.gradle/caches/8.10.2/transforms/0fd0166c4f0b7f2b3a94f0420432a518/transformed/material-1.6.1/res/values/values.xml:7497:4: Duplicate value for resource 'attr/actionBarSize' with config 'DEFAULT' and product ''. Resource was previously defined here: com.xxx.xxx.app-mergeDebugResources-79:/values/values.xml:8497: .

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeDebugResources'.
> A failure occurred while executing com.android.build.gradle.internal.res.ResourceCompilerRunnable
   > Resource compilation failed (Failed to compile values resource file /Users/xxx/Developer/xxx/app-native/android/app/build/intermediates/incremental/debug/mergeDebugResources/merged.dir/values/values.xml. Cause: java.lang.IllegalStateException: Can not add resource (com.android.aaptcompiler.ParsedResource@329c2b83) to table.). Check logs for more details.

排查后确认actionBarSize仅存在于构建生成的中间文件中,未在项目代码或node_modules中找到具体定义,涉及的中间文件包括:

  • ./android/app/build/intermediates/incremental/release/mergeReleaseResources/merged.dir/values-v21/values-v21.xml
  • ./android/app/build/intermediates/incremental/release/mergeReleaseResources/merged.dir/values/values.xml
  • ./android/app/build/intermediates/incremental/release/mergeReleaseResources/merger.xml
  • ./android/app/build/intermediates/incremental/debug/mergeDebugResources/merged.dir/values-v21/values-v21.xml
  • ./android/app/build/intermediates/incremental/debug/mergeDebugResources/merged.dir/values/values.xml
  • ./android/app/build/intermediates/incremental/debug/mergeDebugResources/merger.xml

已尝试过删除node_modules重装、清理本地.gradle缓存、删除app/build目录、执行./gradlew clean等操作,但问题仍未解决。

解决方案

1. 定位依赖冲突根源

进入Android项目目录,执行依赖树分析命令,找出重复引入actionBarSize的依赖:

cd android
./gradlew app:dependencies

重点查看com.google.android.material:material相关依赖项,确认是否存在多版本引入,或是其他第三方依赖间接引入了该库。

2. 强制统一Material库版本

在android/build.gradle的allprojects块中添加依赖强制配置,确保所有模块使用与RN 0.76.2兼容的Material库版本(建议使用RN官方模板中的对应版本,比如1.11.0):

allprojects {
    repositories {
        // 保留原有仓库配置
    }
    configurations.all {
        resolutionStrategy {
            force 'com.google.android.material:material:1.11.0'
        }
    }
}

3. 检查项目本地资源文件

再次检查android/app/src/main/res/values目录下的attrs.xml、styles.xml等文件,确认是否存在手动定义actionBarSize的情况,若有则删除或注释该定义。

4. 深度清理构建缓存

除已执行的操作外,补充以下清理步骤:

  • 删除整个android/build目录(非仅app/build)
  • 执行./gradlew cleanBuildCache清理gradle构建缓存
  • 重启开发IDE(如Android Studio),避免IDE缓存干扰

5. 校验RN升级配置完整性

确认升级过程中Android相关配置是否同步更新:

  • android/build.gradle中的compileSdkVersion、targetSdkVersion是否符合RN 0.76.2的要求(通常为34)
  • android/gradle/wrapper/gradle-wrapper.properties中的gradle版本是否正确(RN 0.76.2适配gradle 8.7及以上)
  • android/app/build.gradle中com.facebook.react:react-native依赖版本是否准确为0.76.2

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 03:23:18