适配Android 14:AndroidManifest.xml中android:exported属性添加位置咨询
适配Android 14时android:exported属性的正确添加位置
修改旧应用适配Android 14时,需在AndroidManifest.xml中添加android:exported="true",但错误将其放在
原始Manifest代码
<?xml version="1.0" encoding="utf-8" standalone="no"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sp.protector.helper"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <receiver android:name="ProtectorHelperReceiver"> <intent-filter> <action android:name="android.intent.action.PACKAGE_RESTARTED"/> <action android:name="android.intent.action.PACKAGE_REPLACED"/> <data android:scheme="package"/> </intent-filter> </receiver> <receiver android:name="HelperWakeupReceiver"> <intent-filter> <action android:name="com.sp.protector.action.WAKEUP_HELPER"/> </intent-filter> </receiver> </application> </manifest>
错误修改后的代码
<?xml version="1.0" encoding="utf-8" standalone="no"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sp.protector.helper"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <receiver android:name="ProtectorHelperReceiver"> <intent-filter> <action android:name="android.intent.action.PACKAGE_RESTARTED"/> <action android:exported="true"/> <action android:name="android.intent.action.PACKAGE_REPLACED"/> <data android:scheme="package"/> </intent-filter> </receiver> <receiver android:name="HelperWakeupReceiver"> <intent-filter> <action android:name="com.sp.protector.action.WAKEUP_HELPER"/> </intent-filter> </receiver> </application> </manifest>
报错信息
brut.androlib.exceptions.AndrolibException: brut.common.BrutException: could not exec (exit code = 1): [C:\Users\Manij\AppData\Local\Temp\brut_util_Jar_69206966786179757076419828386311953032.tmp, link, -o, C:\Users\Manij\AppData\Local\Temp\APKTOOL1611484024447185108.tmp, --package-id, 127, --min-sdk-version, 31, --target-sdk-version, 33, --version-code, 3, --version-name, 2.0, --no-auto-version, --no-version-vectors, --no-version-transitions, --no-resource-deduping, --allow-reserved-package-id, --no-compile-sdk-metadata, --warn-manifest-validation, -e, C:\Users\Manij\AppData\Local\Temp\APKTOOL4006206227314927488.tmp, -0, arsc, -I, C:\Users\Manij\AppData\Local\apk-editor-studio\frameworks\1.apk, --manifest, C:\Users\Manij\AppData\Local\Temp\apk-editor-studio\apk\{14b6c1a8-808c-433c-bc1f-32f964756b55}\AndroidManifest.xml, C:\Users\Manij\AppData\Local\Temp\apk-editor-studio\apk\{14b6c1a8-808c-433c-bc1f-32f964756b55}\build\resources.zip]
正确修改方案
android:exported是组件级属性,必须添加在<receiver>标签本身,而非<intent-filter>或<action>内部。所有带有<intent-filter>的四大组件(Activity、Service、Receiver)在Android 12及以上版本中都必须显式声明该属性。
修改后的完整代码:
<?xml version="1.0" encoding="utf-8" standalone="no"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sp.protector.helper"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <receiver android:name="ProtectorHelperReceiver" android:exported="true"> <intent-filter> <action android:name="android.intent.action.PACKAGE_RESTARTED"/> <action android:name="android.intent.action.PACKAGE_REPLACED"/> <data android:scheme="package"/> </intent-filter> </receiver> <receiver android:name="HelperWakeupReceiver" android:exported="true"> <intent-filter> <action android:name="com.sp.protector.action.WAKEUP_HELPER"/> </intent-filter> </receiver> </application> </manifest>
补充说明
- 如果组件仅接收应用内部的Intent(无外部调用需求),可设置
android:exported="false",但只要存在<intent-filter>,就必须显式声明该属性,不能省略。 - 此要求从Android 12(API 31)开始强制执行,适配Android 14时必须严格遵守。
内容的提问来源于stack exchange,提问作者ManiJ
相关产品推荐
相关产品推荐

