Flutter旧项目运行报错:需为LocationService指定android:exported
问题描述
编译旧Flutter项目时出现如下错误:
\android\app\src\debug\AndroidManifest.xml:35:9-41:19 Error:
android:exported needs to be explicitly specified for element <service#com.adpdigital.push.location.LocationService>. Apps targeting Android 12 and higher are required to specify an explicit value forandroid:exportedwhen the corresponding component has an intent filter defined.
已更新所有依赖(依赖列表如下),且在自身AndroidManifest.xml中添加了android:exported="true",但错误仍存在:
http: ^0.13.5 provider: ^6.0.5 photo_view: ^0.14.0 intl: ^0.18.1 flutter_rating_bar: ^4.0.1 shared_preferences: ^2.1.0 uni_links: ^0.5.1 encrypt: ^5.0.1 url_launcher: ^6.1.10 auto_size_text: ^3.0.0 flutter_datetime_picker: ^1.5.1 package_info_plus: ^3.1.2 video_player: ^2.6.1 cached_network_image: ^3.2.3 location_picker_web: path: ./location_picker_web pointer_interceptor: ^0.9.3+4 location: ^4.4.0
注意:报错的com.adpdigital.push.location.LocationService并非自身AndroidManifest.xml中定义的组件,而是Chabok推送依赖库自动注入的服务。
解决方案
方法一:升级Chabok Flutter插件
查找项目中使用的Chabok相关依赖(可能属于间接依赖),将其升级到支持Android 12的版本。新版本插件会自动为该服务添加android:exported属性,无需手动修改。
方法二:手动在Manifest中合并属性
如果无法升级依赖,可在自身项目的android/app/src/main/AndroidManifest.xml的<application>标签内添加如下代码,显式为该服务指定android:exported值:
<!-- 手动添加Chabok定位服务的exported属性 --> <service android:name="com.adpdigital.push.location.LocationService" android:exported="false" /> <!-- 该服务无需外部调用,设为false即可 -->
额外检查步骤
- 确认
android/build.gradle中compileSdkVersion和targetSdkVersion已设置为31或更高(对应Android 12的API级别)。 - 执行
flutter clean和flutter pub get清除缓存后,重新编译项目。
内容的提问来源于stack exchange,提问作者moein

