skip to content

How do you guard a content provider shared with your own companion app using a custom Android permission, and why choose the signature protection level?

level: seniorimportance: should knowfreq 35%

answer

  1. define it once, in one app
  2. the provider names the permission
  3. same certificate, silent grant
  4. name it after your package

basics

~20 s

Define a <permission> with protectionLevel signature in the app that owns the provider, name it in the provider's readPermission or permission attribute, and declare it with <uses-permission> in the companion. Signature grants it silently, only to apps signed with your certificate.

solid answer

~50 s

In the app that owns the data I declare a `<permission>`, named after my package such as `com.example.social.permission.READ_POSTS`, with `android:protectionLevel="signature"` and a label and description from string resources. On the `<provider>` I set `android:readPermission` (and `android:writePermission` if needed, or one `android:permission` for both). The companion app lists the same name in `<uses-permission>`. Because both apps are signed with the same certificate, the system grants it at install without asking the user, and, without the `knownSigner` flag, an app signed with a different key can't get it at all. `normal` would hand it to any app that asks; `dangerous` would put a confusing prompt in front of users for what is really first-party plumbing. The system also refuses to install a second package declaring the same permission name with a different certificate, which is why the name carries my package.

code

xml · 22 lines
xml
<!-- Social app: defines the permission and guards its provider with it. -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <permission
        android:name="com.example.social.permission.READ_POSTS"
        android:label="@string/perm_read_posts_label"
        android:description="@string/perm_read_posts_desc"
        android:protectionLevel="signature" />

    <application android:label="@string/app_name">
        <provider
            android:name=".data.PostsProvider"
            android:authorities="com.example.social.posts"
            android:exported="true"
            android:readPermission="com.example.social.permission.READ_POSTS" />
    </application>
</manifest>

<!-- Companion app, signed with the same certificate: asks for it. -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="com.example.social.permission.READ_POSTS" />
</manifest>

go deeper

for a junior

Recall that an app can define its own permission with a <permission> element, and that another app asks for it with <uses-permission>.

for a middle

Explain the provider's permission attributes and why the signature level grants silently and only to apps signed with your certificate.

for a senior

Show the failure modes you guard against: a normal-level permission any app can claim, name collisions blocking installs, and duplicate definitions across a suite.

for a principal

Decide how first-party apps share data: one defining app, signature-level permissions, and a plan for signing-key rotation before a second key ever exists.

## The problem A social app keeps posts in a content provider, and its companion app (a widget host, a watch-face helper or a lite client from the same developer) needs to read them. Each Android app runs under its own Linux user ID, so the companion can't normally read the social app's private files; the provider is the sanctioned door. The question is how to make sure **only your own apps** can walk through it. (Exporting the provider and granting per-URI access are covered with intents and IPC; this answer is about the permission.) ## Defining the permission A custom permission is declared in the defining app's manifest with a **`<permission>`** element: - **`android:name`**: the name code and other manifests use. The convention is your package name, then `.permission.`, then the capability in upper SNAKE_CASE: `com.example.social.permission.READ_POSTS`. - **`android:protectionLevel`**: the base type plus optional flags. This is the security decision. - **`android:label`** and **`android:description`**: string resources the user sees when viewing permissions. The description must be a resource reference, not a raw string. - **`android:permissionGroup`** (optional): only affects how the permission is displayed. The provider then names the permission: | `<provider>` attribute | Guards | |---|---| | `android:permission` | Read and write for the whole provider | | `android:readPermission` | Reads; takes precedence over `android:permission` | | `android:writePermission` | Writes; takes precedence over `android:permission` | | `<path-permission>` child | One content URI path; finer scopes take precedence | The companion app declares the same name with `<uses-permission>`, exactly as it would a platform permission. ## Why signature Compare the three base levels for this job: | Level | Who gets the permission | Fit for first-party sharing | |---|---|---| | `normal` | Any app that declares it, granted at install | No: any app can claim it | | `dangerous` | Any app the user approves at runtime | Poor: a prompt for plumbing the user doesn't understand, and any app can still ask | | `signature` | Only apps signed with the same certificate as the defining app, granted silently | Yes | Android's own recommendation matches: when only apps signed by the same developer need access, as in secure IPC between two of your apps, use a signature permission. It is **transparent to the user** and avoids user-confirmed permissions, which confuse people. The `signatureOrSystem` level is an old synonym for `signature|privileged`, deprecated in API level 23; plain `signature` is enough for app-to-app sharing. ## Naming is a security property too The system **does not allow two packages to declare a permission with the same name unless they are signed with the same certificate**. Once a package has declared a permission, installing another package that declares the same name with a different certificate is refused. Two consequences: 1. Prefix names with your package to avoid collisions with someone else's permission. 2. In a suite of apps, **define each permission once**. The documentation says you must do this if the apps aren't all signed with the same certificate, and recommends it even when they are. ## Rotating or adding signers Since Android 12 (API 31), a signature permission can carry the **`knownSigner`** flag in its protection level together with a **`knownCerts`** attribute listing signing-certificate digests. The system then grants the permission to a requesting app if any signer in its signing lineage, including the current one, matches a listed digest. It lets the permission reach apps signed with other known certificates, such as a companion signed with a different key, without sharing the defining app's key. ## Alternatives worth naming - **Signature checks in code**: if only same-signature apps should call you, the receiving app can check that the caller is signed with the same certificate, with no custom permission at all. - **`sharedUserId`**: strongly discouraged. It causes non-deterministic package-manager behaviour, may be removed in a future release, and an app that uses it cannot migrate off it. ## Checklist - Signature level, unless a real third party must be let in. - Package-prefixed name, defined in exactly one app. - Label and description as string resources. - The narrowest provider attribute that fits, read-only if the companion only reads. - Keep the provider's underlying files and database private; the permission guards only the provider, not world-readable files behind it.

  • What does the knownSigner flag add to a signature permission?
    Since Android 12 (API 31), you can add `knownSigner` to `android:protectionLevel` and list certificate digests in `android:knownCerts`. The system then grants the permission to a requesting app if any signer in its signing lineage, including the current one, matches a listed digest. It lets you admit apps signed with other known certificates without sharing the defining app's key.
  • Why not give both apps the same sharedUserId instead of a custom permission?
    Android strongly discourages `sharedUserId`: it causes non-deterministic behaviour in the package manager and may be removed in a future release. Existing apps also cannot remove it, because migrating off a shared user ID is not supported. A provider guarded by a signature permission is the supported way for two of your apps to share data.

  • A custom permission should be dangerous so the user can approve the companion app.
  • A normal-level custom permission keeps other developers' apps out.
  • Each app in the suite should declare its own copy of the shared permission.
  • A short permission name like READ_POSTS is fine because it is scoped to your app.

Assumes Android 17 (API 37); the knownSigner flag with knownCerts exists since Android 12 (API 31), and signatureOrSystem has been deprecated since API 23.