React Native Android构建报错::app:processDebugResources失败,android:attr/lStar缺失
解决React Native构建时android:attr/lStar资源缺失问题
几天前还能正常运行React Native应用,现在执行yarn run android时构建失败,核心报错为未找到资源android:attr/lStar,尝试修改build.gradle、重新克隆项目均无效。
报错详情
本次构建使用了已弃用的Gradle特性,与Gradle 7.0不兼容。 使用'--warning-mode all'查看具体弃用警告。 请参阅https://docs.gradle.org/6.2/userguide/command_line_interface.html#sec:command_line_warnings 212个可执行任务:3个已执行,209个已是最新状态 警告:将新命名空间http://schemas.android.com/repository/android/common/02映射到旧命名空间http://schemas.android.com/repository/android/common/01 警告:将新命名空间http://schemas.android.com/repository/android/generic/02映射到旧命名空间http://schemas.android.com/repository/android/generic/01 警告:将新命名空间http://schemas.android.com/sdk/android/repo/addon2/02映射到旧命名空间http://schemas.android.com/sdk/android/repo/addon2/01 警告:将新命名空间http://schemas.android.com/sdk/android/repo/repository2/02映射到旧命名空间http://schemas.android.com/sdk/android/repo/repository2/01 警告:将新命名空间http://schemas.android.com/sdk/android/repo/sys-img2/02映射到旧命名空间http://schemas.android.com/sdk/android/repo/sys-img2/01 构建失败:出现异常。 * 错误原因: 任务':app:processDebugResources'执行失败。 > 执行com.android.build.gradle.internal.tasks.Workers$ActionFacade时出现失败 > Android资源链接失败 C:\Users\username\.gradle\caches\transforms-2\files-2.1\eb9f77e0d5be7277e1d87bb123b4de4c\core-1.7.0\res\values\values.xml:105:5-114:25: AAPT: 错误:未找到资源android:attr/lStar。
当前使用的build.gradle配置
项目根目录/android/build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext.kotlin_version = '1.3.71' ext { buildToolsVersion = "30.0.2" minSdkVersion = 21 compileSdkVersion = 30 targetSdkVersion = 30 } repositories { google() mavenCentral() jcenter() } dependencies { classpath("com.android.tools.build:gradle:3.5.3") classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms:google-services:4.3.8' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() 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") } maven { url 'https://www.jitpack.io' } jcenter() flatDir { dirs 'libs' } } }
项目根目录/android/app/build.gradle
apply plugin: "com.android.application" apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'com.google.gms.google-services' import com.android.build.OutputFile project.ext.react = [ enableHermes: false, // clean and rebuild if changing ] apply from: "../../node_modules/react-native/react.gradle" def enableHermes = project.ext.react.get("enableHermes", false); android { compileSdkVersion rootProject.ext.compileSdkVersion compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } defaultConfig { applicationId "com.rectnativetemplate" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion versionCode 4 versionName "${System.env.VERSION}" ?: "1.0" missingDimensionStrategy 'react-native-camera', 'general' multiDexEnabled true } splits { abi { reset() enable enableSeparateBuildPerCPUArchitecture universalApk false // If true, also generate a universal APK include "armeabi-v7a", "x86", "arm64-v8a", "x86_64" } } signingConfigs { debug { storeFile file('debug.keystore') storePassword 'android' keyAlias 'androiddebugkey' keyPassword 'android' } release { if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) { storeFile file(MYAPP_RELEASE_STORE_FILE) storePassword MYAPP_RELEASE_STORE_PASSWORD keyAlias MYAPP_RELEASE_KEY_ALIAS keyPassword MYAPP_RELEASE_KEY_PASSWORD } } } buildTypes { debug { signingConfig signingConfigs.debug } release { // Caution! In production, you need to generate your own keystore file. // see https://reactnative.dev/docs/signed-apk-android. signingConfig signingConfigs.release minifyEnabled enableProguardInReleaseBuilds proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro" } } packagingOptions { pickFirst "lib/armeabi-v7a/libc++_shared.so" pickFirst "lib/arm64-v8a/libc++_shared.so" pickFirst "lib/x86/libc++_shared.so" pickFirst "lib/x86_64/libc++_shared.so" } // applicationVariants are e.g. debug, release applicationVariants.all { variant -> variant.outputs.each { output -> // For each separate APK per architecture, set a unique version code as described here: // https://developer.android.com/studio/build/configure-apk-splits.html def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4] def abi = output.getFilter(OutputFile.ABI) if (abi != null) { // null for the universal-debug, universal-release variants output.versionCodeOverride = versionCodes.get(abi) * 1048576 + defaultConfig.versionCode } } } } dependencies { implementation platform('com.google.firebase:firebase-bom:28.3.0') implementation 'com.google.firebase:firebase-analytics' implementation fileTree(dir: "libs", include: ["*.jar"]) //noinspection GradleDynamicVersion implementation "com.facebook.react:react-native:+" // From node_modules implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0" debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") { exclude group:'com.facebook.fbjni' } debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") { exclude group:'com.facebook.flipper' exclude group:'com.squareup.okhttp3', module:'okhttp' } debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") { exclude group:'com.facebook.flipper' } if (enableHermes) { def hermesPath = "../../node_modules/hermes-engine/android/"; debugImplementation files(hermesPath + "hermes-debug.aar") releaseImplementation files(hermesPath + "hermes-release.aar") } else { implementation jscFlavor } implementation(name: 'amazon-chime-sdk', ext: 'aar') implementation(name: 'amazon-chime-sdk-media', ext: 'aar') implementation 'com.google.code.gson:gson:2.8.6' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3' implementation 'androidx.core:core-ktx:1.1.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" } // Run this once to be able to run the application with BUCK // puts all compile dependencies into folder libs for BUCK to use task copyDownloadableDepsToLibs(type: Copy) { from configurations.compile into 'libs' } apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project) //Vector Icons apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"
解决方案
原因说明
android:attr/lStar是Android 12(API 31)新增的属性,当项目依赖的androidx.core:core-ktx自动升级到1.7.0及以上版本时,会引用该属性,但当前项目的compileSdkVersion为30,导致资源找不到。
步骤1:升级编译和目标SDK版本
修改项目根目录/android/build.gradle中的ext配置:
ext { buildToolsVersion = "31.0.0" minSdkVersion = 21 compileSdkVersion = 31 targetSdkVersion = 31 }
步骤2:升级Android Gradle插件版本
修改根目录/android/build.gradle中buildscript的dependencies部分,将Gradle插件版本升级到与compileSdk 31兼容的版本,比如:
classpath("com.android.tools.build:gradle:7.0.4")
步骤3:限制androidx.core版本(可选,若不想升级SDK)
如果暂时不想升级到API 31,可在android/app/build.gradle的dependencies中强制指定androidx.core:core-ktx为1.6.0版本(该版本未引入lStar属性):
implementation("androidx.core:core-ktx:1.6.0") { force = true }
替换原有的implementation 'androidx.core:core-ktx:1.1.0'。
步骤4:清理缓存并重新构建
执行以下命令清理缓存后重新构建:
./gradlew clean yarn cache clean rm -rf node_modules && yarn install yarn run android
内容的提问来源于stack exchange,提问作者alexgarciaalcuadrado
相关产品推荐
相关产品推荐

