TExcelTAD

The Manifest Merger

The AndroidManifest.xml inside an APK is never the file you wrote. AGP's manifest merger builds it from several inputs, and most "why does my app ask for this permission?" questions are answered by its report.

The inputs, by priority

text
1. src/<variant>/AndroidManifest.xml      e.g. src/freeDebug/      highest
2. src/<buildType>/AndroidManifest.xml    e.g. src/debug/
3. src/<flavor>/AndroidManifest.xml       e.g. src/free/
4. src/main/AndroidManifest.xml           the main manifest
5. manifests of library dependencies      AARs, :library modules     lowest

On top of that, the build script injects values: applicationId becomes the package, versionCode/versionName and minSdk/targetSdk become attributes, and manifestPlaceholders fill ${…} slots.

Higher-priority files are merged into lower ones element by element (matched by tag and android:name). New elements are added, identical attributes are fine, and different values for the same attribute are a conflict, unless a tools: marker says how to resolve it.

Two errors and their fixes

The session builds a project with a main manifest, a debug overlay and a free overlay. The first two builds fail on purpose:

bash · try it yourself
mkdir -p hello/app/src/main hello/app/src/debug hello/app/src/free hello/app/src/paid && cd hello
cat > settings.gradle.kts <<'KTS'
pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } }
dependencyResolutionManagement { repositories { google(); mavenCentral() } }
rootProject.name = "hello"
include(":app")
KTS
echo 'plugins { id("com.android.application") version "9.4.1" apply false }' > build.gradle.kts
cat > app/build.gradle.kts <<'KTS'
plugins { id("com.android.application") }
android {
    namespace = "com.example.hello"
    compileSdk = 36
    defaultConfig { applicationId = "com.example.hello"; minSdk = 24; targetSdk = 36; versionCode = 3; versionName = "1.1" }
    flavorDimensions += "tier"
    productFlavors { create("free") { dimension = "tier" }; create("paid") { dimension = "tier" } }
}
KTS
cat > app/src/main/AndroidManifest.xml <<'XML'
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <application android:label="Hello" android:allowBackup="false">
        <meta-data android:name="ads.unit" android:value="${adsUnit}" />
    </application>
</manifest>
XML
cat > app/src/debug/AndroidManifest.xml <<'XML'
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:allowBackup="true" android:usesCleartextTraffic="true" />
</manifest>
XML
cat > app/src/free/AndroidManifest.xml <<'XML'
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="com.google.android.gms.permission.AD_ID" />
</manifest>
XML
gradle wrapper > /dev/null
echo "=== 1: a placeholder without a value"
./gradlew -q :app:processFreeDebugManifest 2>&1 | grep -A1 ' Error:' | head -2
sed -i 's/defaultConfig {/defaultConfig { manifestPlaceholders["adsUnit"] = "none";/' app/build.gradle.kts
echo "=== 2: two values for one attribute"
./gradlew -q :app:processFreeDebugManifest 2>&1 | grep -A3 ' Error:' | head -4
=== 1: a placeholder without a value
/home/learner/hello/app/src/main/AndroidManifest.xml:5:44-70 Error:
	Attribute meta-data#ads.unit@value at AndroidManifest.xml:5:44-70 requires a placeholder substitution but no value for <adsUnit> is provided.
=== 2: two values for one attribute
/home/learner/hello/app/src/debug/AndroidManifest.xml:4:40-67 Error:
	Attribute application@allowBackup value=(true) from AndroidManifest.xml:4:40-67
	is also present at AndroidManifest.xml:4:40-67 value=(false).
	Suggestion: add 'tools:replace="android:allowBackup"' to <application> element at AndroidManifest.xml:2:5-83 to override.

The second message even tells you the fix. tools:replace says "my value wins for these attributes". While we're at it, the paid flavor has no ads, so it removes the location permission the main manifest asks for, with tools:node="remove":

bash · try it yourself
mkdir -p hello/app/src/main hello/app/src/debug hello/app/src/free hello/app/src/paid && cd hello
cat > settings.gradle.kts <<'KTS'
pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } }
dependencyResolutionManagement { repositories { google(); mavenCentral() } }
rootProject.name = "hello"
include(":app")
KTS
echo 'plugins { id("com.android.application") version "9.4.1" apply false }' > build.gradle.kts
cat > app/build.gradle.kts <<'KTS'
plugins { id("com.android.application") }
android {
    namespace = "com.example.hello"
    compileSdk = 36
    defaultConfig {
        applicationId = "com.example.hello"; minSdk = 24; targetSdk = 36; versionCode = 3; versionName = "1.1"
        manifestPlaceholders["adsUnit"] = "none"
    }
    flavorDimensions += "tier"
    productFlavors {
        create("free") { dimension = "tier"; manifestPlaceholders["adsUnit"] = "ca-app-pub-123" }
        create("paid") { dimension = "tier" }
    }
}
KTS
cat > app/src/main/AndroidManifest.xml <<'XML'
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <application android:label="Hello" android:allowBackup="false">
        <meta-data android:name="ads.unit" android:value="${adsUnit}" />
    </application>
</manifest>
XML
cat > app/src/debug/AndroidManifest.xml <<'XML'
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <application android:allowBackup="true" android:usesCleartextTraffic="true"
        tools:replace="android:allowBackup" />
</manifest>
XML
cat > app/src/free/AndroidManifest.xml <<'XML'
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="com.google.android.gms.permission.AD_ID" />
</manifest>
XML
cat > app/src/paid/AndroidManifest.xml <<'XML'
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" tools:node="remove" />
</manifest>
XML
gradle wrapper > /dev/null
./gradlew -q :app:processFreeDebugManifest :app:processPaidReleaseManifest
cat app/build/intermediates/merged_manifests/freeDebug/processFreeDebugManifest/AndroidManifest.xml; echo
echo "=== paidRelease"
grep -E 'permission|allowBackup|value=' app/build/intermediates/merged_manifests/paidRelease/processPaidReleaseManifest/AndroidManifest.xml
echo "=== why is allowBackup true in freeDebug?"
grep -A3 '^	android:allowBackup' app/build/outputs/logs/manifest-merger-free-debug-report.txt
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hello"
    android:versionCode="3"
    android:versionName="1.1" >

    <uses-sdk
        android:minSdkVersion="24"
        android:targetSdkVersion="36" />

    <uses-permission android:name="com.google.android.gms.permission.AD_ID" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        android:allowBackup="true"
        android:debuggable="true"
        android:extractNativeLibs="false"
        android:label="Hello"
        android:usesCleartextTraffic="true" >
        <meta-data
            android:name="ads.unit"
            android:value="ca-app-pub-123" />
    </application>

</manifest>
=== paidRelease
    <uses-permission android:name="android.permission.INTERNET" />
        android:allowBackup="false"
            android:value="none" />
=== why is allowBackup true in freeDebug?
	android:allowBackup
		ADDED from /home/learner/hello/app/src/main/AndroidManifest.xml:4:40-67
		REJECTED from /home/learner/hello/app/src/main/AndroidManifest.xml:4:40-67
	tools:replace

Everything you set up is visible: the version and SDK attributes were injected from defaultConfig, android:debuggable="true" was added because this is a debug build, the free flavor's permission and placeholder value are in, and the debug overlay's allowBackup and cleartext setting won. In paidRelease the location permission is gone and the main manifest's values are back. The merger report answers the why: main's allowBackup was ADDED and then REJECTED, because the debug overlay declared tools:replace.

The tools: markers

Marker Effect
tools:replace="android:a,android:b" this file's values win for these attributes
tools:remove="android:a" drop the attribute from the merged result
tools:node="remove" drop this whole element (match it by android:name)
tools:node="removeAll" drop every element of this type
tools:node="replace" replace the lower-priority element entirely
tools:node="merge" the default
tools:overrideLibrary="com.lib" on <uses-sdk>: allow a library whose minSdk is higher than yours (you promise to guard calls)

Libraries: the usual surprise

Libraries are the lowest-priority input, and they add things: an analytics SDK brings INTERNET and ACCESS_NETWORK_STATE, a push library brings a <service> and a <receiver>, an old library may bring READ_PHONE_STATE. Nothing in your manifest mentions them, and Google Play's permission declaration form will still ask about them. Two habits help:

  • Read the merger report (app/build/outputs/logs/manifest-merger-<variant>-report.txt, or the Merged Manifest tab in Android Studio), which says which file ADDED each element.
  • Check the merged permissions in CI and fail when the list changes unexpectedly; a diff in review beats an email from Play. The exercise in Mobile Build Exercises builds such a check.
xml
<!-- remove a permission that a library adds -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" tools:node="remove" />

Test yourself

src/main sets android:allowBackup="false" and src/debug sets it to true. What happens?

  1. debug wins silently
  2. main wins silently
  3. The merge fails until the higher-priority file adds tools:replace="android:allowBackup"
  4. The attribute is dropped

Which file tells you which library added a permission to your app?

  1. output-metadata.json
  2. the manifest merger report in build/outputs/logs
  3. mapping.txt
  4. gradle.properties

How does the paid flavor drop a permission declared in src/main?

  1. Delete the permission from main
  2. Declare the same uses-permission with tools:node="remove" in src/paid
  3. Use tools:replace
  4. Set minSdk higher