skip to content

After adding an SDK, your Android release's merged manifest declares READ_PHONE_STATE that your code never uses; how do you find its source and remove it?

level: seniorimportance: should knowfreq 45%

answer

  1. one manifest ships, many are merged
  2. libraries have the lowest priority
  3. the merger report names the source
  4. a marker in the tools namespace

basics

~20 s

The build merges library manifests into yours, so check the Merged Manifest view or the manifest-merger report to see which library contributed READ_PHONE_STATE. Then add a <uses-permission> for it with tools:node="remove" to the app manifest, and test the SDK without it.

solid answer

~40 s

An APK or bundle carries one `AndroidManifest.xml`, merged at build time from the variant, build-type and flavor manifests, the main manifest and every library's manifest, libraries lowest. So a permission I never wrote can arrive from a dependency. I open Android Studio's Merged Manifest tab, or read `build/outputs/logs/manifest-merger-<variant>-report.txt`, to see which file contributed it. There's a second cause to rule out: if a library declares a very old `targetSdkVersion`, the merger adds implicit system permissions such as `READ_PHONE_STATE` itself. To drop it, I declare the same `<uses-permission>` in the app manifest with `tools:node="remove"`, optionally scoped with `tools:selector` to that library. Then I test the SDK path, because code that really calls a protected API will fail without the permission; upgrading or replacing the SDK may be the real fix.

code

xml · 12 lines
xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <!-- Drop the permission an SDK contributes to the merged manifest. -->
    <uses-permission
        android:name="android.permission.READ_PHONE_STATE"
        tools:node="remove" />

    <application android:label="@string/app_name">
        <!-- ... -->
    </application>
</manifest>

go deeper

for a junior

Recall that the shipped manifest is merged from your manifest and every library's, so a dependency can add permissions you never wrote.

for a middle

Explain the merge priorities, how elements are matched by android:name, and what tools:node and tools:replace do.

for a senior

Trace an unexpected permission to its source with the merger report, rule out implicit system permissions, and test the SDK before shipping the removal.

for a principal

Make the merged manifest a reviewed artefact: a CI diff on permissions per release and a dependency policy that weighs what each SDK costs in prompts.

## One shipped manifest, many sources An APK or Android App Bundle contains exactly **one** `AndroidManifest.xml`. The project, however, has several: the main manifest in `src/main/`, optional manifests for build variants, build types and product flavors, and one inside **every library** you depend on. The Android Gradle plugin's **manifest merger** combines them at build time (the behaviour described here is the plugin's, current as of AGP 9.4.1). Priorities run from highest to lowest: 1. The build variant's manifest (for example `src/demoDebug/`), then the build type's (`src/debug/`), then the product flavor's (`src/demo/`); with flavor dimensions, the first listed dimension wins. 2. The app module's main manifest. 3. Library manifests, in the order the libraries appear in the `dependencies` block. Lower-priority manifests are merged into higher-priority ones. An element from a library that matches nothing in your manifest is simply **added**. For `<uses-permission>` the match key is `android:name`, so a library's `READ_PHONE_STATE` lands in your release even though no line of your code mentions it, and users see it as your app's permission. ## Finding where it came from Two tools answer "who added this?": - **The Merged Manifest tab** in Android Studio, at the bottom of the `AndroidManifest.xml` editor. Elements merged from lower-priority files are colour-coded by source; clicking one shows its origin in the merging log. - **The merger report**, written to the module's `build/outputs/logs/` directory as `manifest-merger-<buildVariant>-report.txt`, with the full decision tree. It is the version to read in CI. Rule out the second cause, too: **implicit system permissions**. Some APIs that were once open became protected in later releases, and an app targeting an old enough release keeps access without declaring the permission. If a library's manifest has a lower `targetSdkVersion` that would grant such an implicit permission, the merger **explicitly adds** that permission to the merged manifest. A library with `targetSdkVersion` of 3 or lower brings `WRITE_EXTERNAL_STORAGE` and `READ_PHONE_STATE`; one at 15 or lower that uses `READ_CONTACTS` brings `READ_CALL_LOG`. ## Removing it with merge rule markers Markers live in the **`tools` namespace** (`xmlns:tools="http://schemas.android.com/tools"`) and are read from the higher-priority manifest. The ones that matter here: | Marker | Effect | |---|---| | `tools:node="remove"` | Removes the matching element from the merged manifest | | `tools:node="removeAll"` | Removes every element of that type under the same parent | | `tools:node="replace"` | Replaces the lower-priority element completely | | `tools:replace="attr,..."` | Keeps this manifest's values for the named attributes | | `tools:remove="attr,..."` | Drops the named attributes from the merged element | | `tools:selector="<library package>"` | Applies the marker only to that library's contribution | Declaring `READ_PHONE_STATE` with `tools:node="remove"` in the main manifest (see the code block) removes it from the result. In an app module the markers are stripped after merging; in a **library module** they are kept and can affect merges downstream, which matters if you publish libraries. ## Before you ship the removal Removing the declaration does not remove the code that wanted it. If the SDK really calls an API that `READ_PHONE_STATE` protects, that call now fails at runtime, typically with a `SecurityException`, on whatever path the SDK takes. So: - Read the SDK's documentation for which features need the permission, and whether a newer version dropped it. - Exercise the SDK's features on a device with the permission absent. - Prefer upgrading or replacing an SDK that targets an ancient release over patching its manifest forever. ## Conflicts are the other half of merging Permissions are additive, but attributes conflict. When the same element has the same attribute with **different values** in two manifests, the build fails with a merge error until a marker such as `tools:replace` decides it. A few elements behave differently: attributes on `<manifest>` are never merged (the highest priority wins), `android:required` on `<uses-feature>` is OR-merged, and `<intent-filter>` elements are never matched, only added. And because merging fills in attributes you left unset, don't rely on a default value: a library's explicit value replaces your implicit default. ## Keeping it from coming back - Diff the merged manifest of each release candidate against the last release in CI, and fail on new permissions. - Note in code review which dependency brings which permission, so an SDK upgrade that adds one is a visible decision.

  • How do you remove a permission from only one library's contribution?
    Add `tools:selector` with that library's package name next to the marker, for example `tools:node="remove"` plus `tools:selector="com.example.lib1"`. The merger then applies the rule only when the lower-priority manifest comes from that library; the same element from any other source merges normally.
  • Your build fails because the main manifest and a library set different values for android:theme on one activity. What do you do?
    That is a merge conflict: the same attribute with two values on a matched element fails the build. In the higher-priority manifest, keep your value and add `tools:replace="android:theme"` to that element. The merging errors in the Merged Manifest view suggest the marker for each conflict.

  • A permission missing from your own manifest file can't appear in the shipped app.
  • The main manifest always overrides a library's conflicting attribute without any marker.
  • Removing the permission with tools:node="remove" also removes the SDK code that uses it.
  • Libraries have the highest merge priority because they are merged last.