技術學習記錄

Android 12 Manifest merger error

今天在更新APP專案的API Level時,發生了下面的錯誤訊息:

Manifest merger failed: 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.

趁著下班時還記憶猶新,趕緊將查到的解法記錄起來。

Google在Android API Level 31中加入了exported參數的強制要求。
也就是說當你的Activity是APP的入口、或是Activity有註冊intent filter時,必須要在該Activity的設定中加入exported參數,否則編譯會失敗。
至於exported參數的作用是什麼,官方文件有提到:

官方文件連結

This element sets whether the activity can be launched by components of other applications — “true” if it can be, and “false” if not. If “false“, the activity can be launched only by components of the same application or applications with the same user ID. If you are using intent filters, you should not set this element “false“. If you do so, and an app tries to call the activity, system throws an ActivityNotFoundException. Instead, you should prevent other apps from calling the activity by not setting intent filters for it.
If you do not have intent filters, the default value for this element is “false“. If you set the element “true“, the activity is accessible to any app that knows its exact class name, but does not resolve when the system tries to match an implicit intent.
This attribute is not the only way to limit an activity’s exposure to other applications. You can also use a permission to limit the external entities that can invoke the activity (see the permission attribute).

上面講的實際上是說:exported參數定義了Activity是否可以被其他APP啟動。true表示為可以、false則表示不能。

如果有使用到intent filter,必須要將此參數設定為true,不然會拋出ActivityNotFoundException例外。

因此我們必須要將exported參數給補上,如下:

<activity
	android:name=".MainActivity"
	android:exported="true"
	android:theme="@style/Theme.MyApplication.NoActionBar">
	<intent-filter>
		<action android:name="android.intent.action.MAIN" />
		<category android:name="android.intent.category.LAUNCHER" />
	</intent-filter>
</activity>

參考來源:
https://developer.android.com/guide/topics/manifest/activity-element#exported

https://stackoverflow.com/questions/67654506/manifest-merger-failed-targeting-android-12

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *