当前位置:   article > 正文

解决Android 12及以上版本Manifest合并失败:android:exported属性必须显式声明_android:exported needs to be explicitly specified

android:exported needs to be explicitly specified for element

解决Android 12及以上版本Manifest合并失败:android:exported属性必须显式声明

引言

从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.
  • 1

这意味着 AndroidManifest.xml 文件中的某些组件缺少 android:exported 属性。

解决步骤

步骤1: 打开 AndroidManifest.xml

首先,定位到你的项目中的 AndroidManifest.xml 文件。

步骤2: 定位到相关的 <activity> 标签

AndroidManifest.xml 中找到错误提示中指定的 <activity> 标签,如 <activity#com.example.btconnectchart.main.MainActivity>

步骤3: 检查 <intent-filter>

确认这个 <activity> 标签内是否包含 <intent-filter>。如果有,那么需要显式声明 android:exported 属性。

步骤4: 添加 android:exported 属性

<activity> 标签中添加 android:exported 属性,并根据该 Activity 的需求设置为 truefalse

示例代码:

<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
步骤5: 保存并重新编译

保存更改后的 AndroidManifest.xml 文件,并重新编译你的项目。

结语

通过以上步骤,你应该能够解决目标为Android 12及以上版本的应用中因 android:exported 属性缺失导致的编译错误。希望这篇文章能帮助到遇到相同问题的开发者。


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/824104
推荐阅读
相关标签