赞
踩
从Android 12(API 级别 31)开始,Android要求所有的 manifest 文件中包含 intent filter 的组件(如 Activity
, Service
, Broadcast receiver
等)必须显式声明 android:exported
属性。这一变更常导致应用升级后编译失败,本文将详细介绍如何解决此问题。
开发者在编译针对Android 12或更高版本的应用时,可能会遇到以下编译错误信息:
Caused by: java.lang.RuntimeException: Manifest merger failed : android:exported needs to be explicitly specified for element <activity#com.example.btconnectchart.main.MainActivity>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined.
这意味着 AndroidManifest.xml
文件中的某些组件缺少 android:exported
属性。
AndroidManifest.xml
首先,定位到你的项目中的 AndroidManifest.xml
文件。
<activity>
标签在 AndroidManifest.xml
中找到错误提示中指定的 <activity>
标签,如 <activity#com.example.btconnectchart.main.MainActivity>
。
<intent-filter>
确认这个 <activity>
标签内是否包含 <intent-filter>
。如果有,那么需要显式声明 android:exported
属性。
android:exported
属性在 <activity>
标签中添加 android:exported
属性,并根据该 Activity
的需求设置为 true
或 false
。
示例代码:
<activity
android:name=".main.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
保存更改后的 AndroidManifest.xml
文件,并重新编译你的项目。
通过以上步骤,你应该能够解决目标为Android 12及以上版本的应用中因 android:exported
属性缺失导致的编译错误。希望这篇文章能帮助到遇到相同问题的开发者。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。