How to resolve compile error: “Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported`"
If your app targets Android12, the document says:
If your app targets Android 12 and contains activities, services, or broadcast receivers that use intent filters, you must explicitly declare the
android:exported
attribute for these app components
So I explicitly declared android:exported
attribute for all components which use intent filters in AndroidManifest.xml, but still failed with compile error:
Manifest merger failed with multiple errors, see log Error: 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
What happened ??
You need to check “Merged” Manifest
This document says that:
Your APK file can contain just one
AndroidManifest.xml
file, but your Android Studio project may contain several—provided by the main source set, build variants, and imported libraries. So when building your app, the Gradle build merges all manifest files into a single manifest file that's packaged into your APK.
and
Tip: Use the Merged Manifest view to preview the results of your merged manifest and find conflict errors.
So you also need to check imported libraries as well, and to preview merged manifest you can use Merged Manifest tab at the bottom of the editor.
You would need to down targetSdkVersion to 30 temporarily at this time to compile.
In my project, I found that these components shown below, from external libraries, did not specify an explicit value for `android:exported` even if these components declare intent filters.
When I clicked these components in Merged Manifest view, I found these components are from androidx.test:core:1.3.0.
However, my project did explicitly implement this library, so I had to find other libraries that depend on “androidx.test:core:1.3.0”.
Tips to replace libraries
So I had to find and replace libraries that depend on androidx.test:core:1.3.0.
First, I searched which version of “androidx.test:core” properly handle this exported attribute issue.
For this, I used Android Code Search and found that from “androidx.test:core:1.3.1-alpha01” this library properly handles exported attributes in the manifest file.
Second, I had to find libraries that depend on “androidx.test:core”, and also had to find the version of those libraries to depend on above “androidx.test:core:1.3.1-alpha01”.
To do this, I used AndroidXTech to see dependency, and searched libraries that are explicitly implemented in my build.gradle file which looks like relate to “androidx.test:core”, one by one. (Terrible…Please teach me if you have a better idea)
And finally, I found:
- androidx.test.ext:junit:1.1.1 depends on androidx.test:core:1.2.1
- androidx.test.espresso:espresso-intents:3.1.1 depends on androidx.test:core:1.1.0
- androidx.fragment:fragment-testing:1.3.3 depends on androidx.test:core:1.3.0
and upgraded to versions that depend on the above “androidx.test:core:1.3.1-alpha01”.
Besides, “com.squareup.leakcanary:leakcanary-android:1.5.1” also has to be upgraded.
Conclusion
If you failed to compile even if you specified exported attribute, the cause may be your external libraries. To find out this, you can check with Merged Manifest View. To find a proper version of libraries, you can use Android Code Search, AndroidXTech, etc.