The manifest and app components
Why it matters
- The system reads the merged manifest inside your APK. If a manifest-only component is missing or declared wrongly, the system cannot find or start it.
android:exportedis a security boundary. Setting it totrueon an internal service exposes it to every app on the device.- Manifest merging is invisible in the editor. Library components appear in your final APK without you writing a single line, and merge conflicts cause confusing build failures.
- Interviewers probe component declaration, merge priority, and the permission model because mistakes here produce real crashes and security incidents in production.
The simple explanation
- Think of the manifest as a building's front-desk register.
- Every visitor (an Intent) is matched against a list of registered departments (components).
- The register also holds security rules (permissions) and which doors are open to the public (exported).
- When you add a new wing (a library), its departments are merged into the master list automatically.
- If a department is not on the register, no one can find it, no matter how well it is built inside.
AndroidManifest.xml declares the app's components, the permissions it requests, and the hardware or software features it uses. Activities, services and content providers must be declared there or the system will not start them. Broadcast receivers are the one exception: they can also be registered at runtime with registerReceiver.
The four components. Activity (a UI entry point), Service (work without a UI, started or bound), BroadcastReceiver (reacts to broadcasts), ContentProvider (shares structured data, also across apps). Each is a child of <application> with an android:name pointing to the class. A leading dot (.ui.MainActivity) is resolved against the module's namespace.
Manifest merging. Your APK has one manifest, built from several. From highest to lowest priority: the build variant source sets (such as src/debug/), the app module's main manifest, then library manifests in the order of your dependencies block. Lower-priority manifests are merged into higher-priority ones. If two manifests give the same attribute different values, that is a merge conflict and the build fails until you add a rule such as tools:replace="android:exported". You can also use tools:node="remove" to drop a library element or tools:node="replace" to replace it completely. Any tools: attribute needs xmlns:tools on the root <manifest> tag.
Permissions and features. <uses-permission> requests a permission. Normal permissions (such as INTERNET) are granted at install time. Dangerous permissions (location, camera, contacts and so on) are not granted at install on Android 6.0+; you must also request them at runtime, for example with registerForActivityResult(ActivityResultContracts.RequestPermission()). <uses-feature android:required="false"> means the app can be installed on devices without that hardware, and your code must check for it before use.
android:exported (target API 31+). When your app targets Android 12 (API 31) or higher, any activity, service or receiver that has an <intent-filter> must declare android:exported="true" or "false". Leaving it out fails the build at the manifest-merge step, and Android 12+ devices refuse to install such an APK. Before this rule, a component with an intent filter was exported by default, which exposed many components by accident. A component without an intent filter defaults to exported="false".
Intent filters. An <intent-filter> inside a component declares which implicit intents it handles. The system matches on action, category, and data (scheme, host, path, MIME type). A launcher activity needs MAIN + LAUNCHER. A link handler needs VIEW + DEFAULT + BROWSABLE plus a <data> element.
| Element | Purpose | Key attribute |
|---|---|---|
<activity> | UI entry point | android:exported |
<service> | Work without a UI | android:exported |
<receiver> | Reacts to broadcasts | android:exported |
<provider> | Shares data | android:authorities |
<uses-permission> | Requests a permission | android:name |
<uses-feature> | Hardware / software feature | android:required |
Notes
- The system reads the merged manifest packaged in the APK. In Android Studio, open the Merged Manifest tab at the bottom of the manifest editor to see it and which file contributed each line.
- Priority, highest first: build variant manifests, the app's main manifest, then libraries in
dependenciesorder. Higher priority does not silently win a conflicting attribute: the build fails until you addtools:replace. tools:node="merge"is the default.tools:node="remove"deletes the element from the merged result;tools:node="replace"replaces the whole element;tools:replace="attr"overrides one conflicting attribute.xmlns:tools="http://schemas.android.com/tools"must be on the root<manifest>tag for anytools:attribute to resolve.- When targeting API 31+, every activity, service and receiver with an
<intent-filter>must declareandroid:exported. Omitting it is a build error, not a warning. android:exported="true"means any app on the device can start or bind to the component, unless you also protect it withandroid:permission. Use"false"for internal services and receivers.- Normal permissions are granted at install. Dangerous permissions also need a runtime request, and the user can deny them.
<uses-feature android:required="false">lets the app install on devices without the hardware; check for it at runtime withPackageManager.hasSystemFeature.- A
ContentProviderauthority must be unique on the device. Installing a second app with the same authority fails withINSTALL_FAILED_CONFLICTING_PROVIDER. - The merger log is written to
build/outputs/logs/manifest-merger-<variant>-report.txt. It records where each element came from and how conflicts were resolved.
Code example
This manifest shows a typical app module: two permissions, one optional hardware feature, a custom permission, and four components with different export settings. Every component with an intent filter explicitly declares android:exported.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<!-- Only apps signed with the same key can hold this permission -->
<permission
android:name="com.example.app.permission.READ_CONTACTS"
android:protectionLevel="signature" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.MyApp"
tools:targetApi="34">
<!-- Launcher activity: exported so the launcher can start it -->
<activity android:name=".ui.MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Internal service: not exported, only this app can start it -->
<service android:name=".sync.SyncService" android:exported="false" />
<!-- Boot receiver: has an intent filter, so exported must be declared -->
<receiver android:name=".sync.BootReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<!-- Provider: exported, but gated by the custom permission above -->
<provider
android:name=".data.ContactProvider"
android:authorities="com.example.app.contacts"
android:exported="true"
android:permission="com.example.app.permission.READ_CONTACTS" />
</application>
</manifest>- The launcher activity and the boot receiver have intent filters, so both declare
android:exported, as required when targeting API 31+. BOOT_COMPLETEDis only delivered if the app holdsRECEIVE_BOOT_COMPLETED.SyncServicehas no intent filter and is markedexported="false", so only this app can start it.- The
ContentProvideris exported but gated by asignaturepermission that this manifest defines, so only apps signed with the same key can query it. tools:targetApi="34"tells lint that this element's attributes are only used on API 34 and higher, which suppresses "attribute is only used in API level X" warnings. It does not change runtime behaviour.
Run the idea in plain Kotlin
This model merges a library's components into the app's list and then checks the Android 12 rule. exported = null means the attribute was not written.
data class Component(
val name: String,
val type: String,
val exported: Boolean?, // null = attribute not declared
val actions: List<String>
)
fun main() {
// Components declared in the app module
val appComponents = listOf(
Component("MainActivity", "activity", true, listOf("MAIN")),
Component("SyncService", "service", false, emptyList()),
Component("BootReceiver", "receiver", true, listOf("BOOT_COMPLETED"))
)
// Components contributed by an old library (merged in at build time)
val libComponents = listOf(
Component("AnalyticsReceiver", "receiver", null, listOf("ANALYTICS_EVENT")),
Component("AnalyticsProvider", "provider", false, emptyList())
)
val merged = appComponents + libComponents
// Can another app start SyncService?
val sync = merged.find { it.name == "SyncService" }
println("External app can start SyncService: " + (sync?.exported == true))
// Which components handle BOOT_COMPLETED?
val handlers = merged.filter { "BOOT_COMPLETED" in it.actions }
println("BOOT_COMPLETED handlers: " + handlers.joinToString(", ") { it.name })
// Android 12+ rule: a component with an intent filter must declare exported
val violations = merged.filter { it.actions.isNotEmpty() && it.exported == null }
println("Missing exported: " + violations.joinToString(", ") { it.name })
// Every component that other apps can reach
val exported = merged.filter { it.exported == true }
println("Exported components: " + exported.joinToString(", ") { it.name })
}Expected output: false, then BootReceiver, then AnalyticsReceiver as the one violation (the real build would fail here), then MainActivity, BootReceiver.
In production
- After upgrading targetSdk to 31, the CI build stopped producing an APK.
- The manifest merger reported that a BroadcastReceiver in a third-party analytics library had an intent filter but no
android:exportedattribute. - No users were affected; the failure was purely at build time.
- The team updated the analytics library to a version that declared
android:exportedon its receiver. - For a second library that could not be updated, they redeclared its receiver in the app manifest with the same
android:nameandandroid:exported="false"(the defaulttools:node="merge"adds the attribute). They first checked that the receiver only received broadcasts from inside the app. - They reviewed the Merged Manifest view after every dependency update to see newly added exported components.
- The app had corrupted local data and unexplained battery drain on some devices.
- A background Service was declared with a custom intent filter but no
android:exportedattribute. - The app still targeted API 30, so a component with an intent filter defaulted to
exported="true". Another app on the device could start the sync service and trigger concurrent writes.
- The team removed the intent filter, started the service with an explicit intent, and set
android:exported="false". - Where a component truly had to be exported, they protected it with a
signature-level permission. - They wrote an instrumented test that tries to start the service from a second test app and asserts a
SecurityException.
Common mistakes
- Omitting android:exported on a component with an intent filter. When targeting API 31+ this is a build error, not a warning. Always set it explicitly on every component that contains an intent filter.
- Setting android:exported="true" on an internal service. Any app on the device can then start or bind to that service. Use "false" unless the component genuinely needs cross-app access, and then gate it with a permission.
- Removing a library component with tools:node="remove" while the library code still uses it. The build succeeds, but at runtime the library tries to start a component that no longer exists and crashes. Check the library's documentation before removing a merged component.
- Declaring a dangerous permission in the manifest but never requesting it at runtime. On Android 6.0+ it is not granted at install, so
checkSelfPermissionreturns DENIED and the protected API throws aSecurityException. Request it with the Activity Result API first. - Forgetting xmlns:tools on the root manifest tag. Any tools: attribute (tools:node, tools:targetApi, tools:ignore) has an unbound prefix and the build fails. Add xmlns:tools="http://schemas.android.com/tools" to the <manifest> element.
- Protecting a component with a custom permission that nobody defines. No app can be granted an undefined permission, so every caller is rejected with a
SecurityException, and another app that defines it first could take it over. Define it with a <permission> element, usually with protectionLevel signature.
Interview questions
Answer out loud first. Then reveal the answer and compare it at each depth.
“What are the four app components and how are they declared in the manifest?”
“How does manifest merging work between app and library modules?”
“What is android:exported and why was it made mandatory on Android 12?”
“How do intent filters route intents to components?”
Quiz
What happens if an activity with an intent filter omits android:exported and targetSdk is 31 or higher?
Which tools:node value removes a component that was declared in a library manifest?
A BroadcastReceiver must run after the device boots. Which setup is correct when targeting API 31+?
Which manifest has the highest priority during manifest merging?
0 of 4 answered
Mini task
15-minute exercise: audit and extend your manifest
- Open
app/src/main/AndroidManifest.xmlin your project. - Add a new
<activity>named.ui.DebugActivitywithandroid:exported="false"and no intent filter. - Add a new
<receiver>named.sync.RefreshReceiverwithandroid:exported="false"and an<intent-filter>containing<action android:name="com.example.app.ACTION_REFRESH" />(use your own package name). Create the matchingBroadcastReceiverclass. - Add
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />before the<application>tag. - Build the project, then open the Merged Manifest tab of the manifest editor and confirm your three new entries appear.
- In the same view, find at least one component or permission that came from a library, not from your app module.
- Remove
android:exportedfromRefreshReceiverand rebuild. Confirm the build fails with a message about a missingandroid:exported(this needs targetSdk 31 or higher). - Re-add
android:exported="false"and confirm the build passes.
Done condition: The merged manifest contains your three new declarations, you identified at least one library-contributed entry, and you reproduced the build failure caused by a missing android:exported on a component with an intent filter.