解决Manifest merger failed : android:exported needs to be explicitly specified for <activity>

司马钱明
2023-12-01

出错场景

运行一个老项目,编译时报以下错误

Execution failed for task ':app:processDebugMainManifest'.
> Manifest merger failed : android:exported needs to be explicitly specified for <activity>. 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 https://developer.android.com/guide/topics/manifest/activity-element#exported for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

解决方案

根据错误提示,targetSdkVersion大于等于SDK 31(也就是Android 12)时,如果有的Activity配置了Intent-filter,必须也同时配置exported属性,否则编译失败。

所以我们有2种解决方案

  1. 降低targetSdkVersion为30.
    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 21
        //targetSdkVersion 31
        targetSdkVersion 30
    }
  1. 将Manifest中有配置Intent-filter的Activity加上android:exported属性
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:launchMode="standard"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 类似资料: