Flutter编译Release APK遇background_fetch错误及依赖冲突求助
解决Flutter编译Release APK时的
android:attr/lStar及Kotlin Gradle插件版本冲突问题 问题核心
编译Release APK时触发android:attr/lStar not found资源错误,调整Kotlin Gradle插件版本后出现兼容性或依赖冲突,但App Bundle可正常编译。
解决方案步骤
1. 升级Android编译/目标SDK版本
android:attr/lStar是Android 12(API 31)新增的属性,错误源于第三方库(background_fetch)使用了高API资源,但项目编译SDK版本未匹配。修改android/app/build.gradle:
android { compileSdkVersion 33 // 至少指定为31,推荐用最新稳定API版本 defaultConfig { targetSdkVersion 33 // 保留其他原有配置 } }
2. 适配background_fetch依赖版本
- 打开
pubspec.yaml,将background_fetch更新到官方最新稳定版,确保其支持API 31+。 - 若已为最新版仍报错,在
android/app/build.gradle的android块中强制指定该库的兼容版本:configurations.all { resolutionStrategy { force 'com.transistorsoft:tsbackgroundfetch:7.1.1' // 替换为实际最新版本号 } }
3. 匹配Kotlin与Android Gradle插件版本
你的Android Gradle插件版本为7.3.1,对应的兼容Kotlin版本范围是1.7.20~1.8.20,无需降级到1.6.0。操作步骤:
- 保持
buildscript中的配置为:buildscript { ext.kotlin_version = '1.8.20' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.3.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } - 执行清理命令:
flutter clean rm -rf android/.gradle # Windows系统用 rmdir /s/q android\.gradle flutter pub get - 重新编译Release APK:
flutter build apk --release
4. 解决类重复依赖冲突
若仍出现重复类错误,运行flutter pub deps查看依赖树,定位冲突的库,通过resolutionStrategy强制统一版本:
configurations.all { resolutionStrategy { force 'com.example:conflicting-library:1.0.0' // 替换为冲突库的正确版本 } }
为什么App Bundle能正常编译?
App Bundle采用按需拆分资源的机制,会根据目标设备API级别过滤资源;而Release APK会合并所有资源,当编译SDK版本低于资源依赖的API级别时,就会触发lStar这类属性找不到的错误。
内容的提问来源于stack exchange,提问作者Matheus290
相关产品推荐
相关产品推荐

