React Native run-android构建失败:android:attr/lStar资源未找到
解决React Native
resource android:attr/lStar not found 构建报错 问题描述
使用react-native run-android运行React Native应用(版本0.64.2)时,构建流程失败,核心报错为resource android:attr/lStar not found,完整构建日志如下:
> Task :app:processDebugResources FAILED 227 actionable tasks: 227 executed Note: /home/leo/.dev/id_motorista/node_modules/@react-native-async-storage/async-storage/android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncStorageModule.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: /home/leo/.dev/id_motorista/node_modules/@react-native-async-storage/async-storage/android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncStoragePackage.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Note: /home/leo/.dev/id_motorista/node_modules/react-native-background-fetch/android/src/main/java/com/transistorsoft/rnbackgroundfetch/HeadlessTask.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. FAILURE: Build failed with an exception.
* What went wrong: Execution failed for task ':app:processDebugResources'. > A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade > Android resource linking failed /home/leo/.gradle/caches/transforms-2/files-2.1/3e18a838e60dae2918cb4d785933a26e/core-1.7.0/res/values/values.xml:105:5-114:25: AAPT: error: resource android:attr/lStar not found.
已尝试以下方法但未解决问题:
- 清理gradle缓存
- 清理React缓存
- 清理Metro缓存
- 清理Yarn缓存
- 删除并重新安装node_modules
- 修改
build.gradle中compileSdkVersion和targetSdkVersion为31
解决方案
1. 强制约束AndroidX Core版本到1.6.0
android:attr/lStar是AndroidX Core 1.7.0新增的属性,但React Native 0.64.2依赖的AndroidX版本不兼容该属性。需在项目根目录的build.gradle中添加强制依赖约束:
在allprojects块的repositories下方插入:
configurations.all { resolutionStrategy { force 'androidx.core:core-ktx:1.6.0' force 'androidx.core:core:1.6.0' } }
2. 检查并降级冲突的第三方依赖
部分第三方库(如react-native-background-fetch)可能自动引入高版本AndroidX Core。可通过以下命令查看依赖树定位冲突库:
./gradlew app:dependencies
找到引入androidx.core:core:1.7.0的库,尝试将其降级至兼容React Native 0.64.2的版本。
3. 匹配gradle插件与wrapper版本
React Native 0.64.2对应的gradle插件版本应为4.2.2左右,检查项目根目录build.gradle中的classpath:
classpath("com.android.tools.build:gradle:4.2.2")
同时确保gradle wrapper版本(gradle/wrapper/gradle-wrapper.properties)为6.7.1:
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
4. 重新构建项目
完成上述修改后,执行以下命令清理并重新构建:
cd android && ./gradlew clean cd .. && react-native run-android
内容的提问来源于stack exchange,提问作者leonardogbxv
相关产品推荐
相关产品推荐

