> Task :app:processDebugMainManifest FAILED android:exported needs to be explicitly specified for

报错提示如下

Task :app:processDebugMainManifest FAILED
E:\ITshop2025\6.16jianshen\app\src\main\AndroidManifest.xml:11:9-17:20 Error:
	android:exported needs to be explicitly specified for element <activity#com.example.jianshen.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. See 

错误原因:

这个错误是由于你的 AndroidManifest.xml 文件中未明确指定 android:exported 属性。从 Android 12(API 级别 31) 开始,任何包含intent-filter的组件(如 activity、service、receiver 等)都必须显式声明 android:exported 属性,以明确该组件是否可以被其他应用程序访问。

解决方法

  1. 修改 AndroidManifest.xml 文件
    打开 app/src/main/AndroidManifest.xml 文件,找到 标签,并添加 android:exported 属性。例如:

修改前:

<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

修改后:

<activity
    android:name=".MainActivity"
    android:exported="true"> <!-- 添加 android:exported 属性 -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
  1. android:exported 属性的含义
    android:exported=“true”:表示该组件可以被其他应用程序访问。

android:exported=“false”:表示该组件只能被当前应用程序访问。

对于主活动(MainActivity),通常需要设置为 true,因为它需要被系统启动。

  1. 检查其他组件
    如果你的 AndroidManifest.xml 文件中还有其他组件(如 service、receiver等),并且它们包含 intent-filter,也需要为它们添加 android:exported 属性。

例如:

<service
    android:name=".MyService"
    android:exported="false"> <!-- 根据需求设置 true 或 false -->
    <intent-filter>
        <action android:name="com.example.jianshen.MY_ACTION" />
    </intent-filter>
</service>
  1. 清理和重建项目
    完成上述修改后,清理项目并重新构建:

点击 Build > Clean Project。

点击 Build > Rebuild Project。

  1. 检查目标 SDK 版本
    确保你的 targetSdkVersion 设置为 31 或更高版本。在模块的 build.gradle 文件中检查:
android {
    compileSdk 34

    defaultConfig {
        applicationId "com.example.jianshen"
        minSdk 28
        targetSdk 34 // 确保目标 SDK 版本为 31 或更高
        versionCode 1
        versionName "1.0"
    }
}
  1. 完整示例
    以下是修改后的 AndroidManifest.xml 文件示例:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jianshen">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Jianshen">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

总结
在 AndroidManifest.xml 文件中为包含 的组件添加 android:exported 属性。
对于主活动(MainActivity),通常设置为 android:exported=“true”。
清理和重建项目。
确保 targetSdkVersion 设置为 31 或更高版本。

好的,关于这个问题我们今天就先分享到这里,希望能帮助到屏幕前为代码发愁的您。如果觉得有帮助,希望能在Taobao搜索“鹿溪IT工作室”买一个Android小项目来练手,友友们给个好评,支持一下创作者不易
关注鹿溪IT工作室,后续我们会不定时分享新的bug修改意见,有时候不一定全对,欢迎大家留言批评指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值