添加background_fetch后Android资源链接失败问题求助
解决background_fetch依赖导致Android Release构建时android:attr/lStar资源缺失问题
问题描述
添加background_fetch: ^1.1.1依赖后,执行Android Release版本构建时出现资源链接失败,具体报错为找不到android:attr/lStar资源,任务:background_fetch:verifyReleaseResources执行失败。已将Gradle从7.1.0升级至7.3.0,尝试相关修复方案后问题仍存在。
报错详情
FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':background_fetch:verifyReleaseResources'. > A failure occurred while executing com.android.build.gradle.tasks.VerifyLibraryResourcesTask$Action Android resource linking failed ERROR:/Users/musamaashraf/dev/projects/BuddyPark/build/background_fetch/intermediates/merged_res/release/values/values.xml:2520: AAPT: error: resource android:attr/lStar not found. * 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. * Get more help at https://help.gradle.org BUILD FAILED in 1m 41s Running Gradle task 'assembleRelease'... 102.3s Gradle task assembleRelease failed with exit code 1
当前配置文件
android/build.gradle
buildscript { ext.kotlin_version = '1.6.10' repositories { google() jcenter() mavenCentral() } dependencies { classpath 'com.google.gms:google-services:4.3.13' classpath 'com.android.tools.build:gradle:7.3.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { google() mavenCentral() maven { // [required] background_fetch url "${project(':background_fetch').projectDir}/libs" } } } rootProject.buildDir = '../build' subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" } subprojects { project.evaluationDependsOn(':app') } task clean(type: Delete) { delete rootProject.buildDir }
gradle.properties
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 android.useAndroidX=true android.enableJetifier=true android.jetifier.ignorelist=bcprov-jdk15on
修复方案
1. 统一所有子项目的SDK版本
在android/build.gradle中修改subprojects块,强制所有子项目使用与主App一致的compileSdkVersion和targetSdkVersion(至少为31,因为lStar是Android 12/API 31新增属性):
rootProject.buildDir = '../build' subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" // 添加以下配置 project.ext { compileSdkVersion = 33 // 可根据主App实际版本调整,最低31 targetSdkVersion = 33 } afterEvaluate { if (project.hasProperty('android')) { android { compileSdkVersion project.ext.compileSdkVersion targetSdkVersion project.ext.targetSdkVersion } } } } subprojects { project.evaluationDependsOn(':app') }
2. 确认主App的SDK版本配置
打开android/app/build.gradle,确保compileSdkVersion和targetSdkVersion不低于31:
android { compileSdkVersion 33 defaultConfig { targetSdkVersion 33 // 其他原有配置... } // 其他原有配置... }
3. 清理缓存并重新构建
执行以下命令清理构建缓存,然后重新生成Release版本:
flutter clean flutter build apk --release
原因说明
android:attr/lStar是Android 12(API 31)引入的资源属性,background_fetch库的依赖可能引用了该属性,但项目中部分子模块的compileSdkVersion低于31,导致AAPT无法识别该资源,从而触发链接失败。通过统一所有模块的SDK版本,确保构建环境支持该属性即可解决问题。
内容的提问来源于stack exchange,提问作者Muhammad Usama Ashraf
相关产品推荐
相关产品推荐

