As an Android lead, how do you decide what your app exports to other apps, and how do you guard each exported activity, receiver, provider and bound service?
answer
- every exported component is public API
- delegate instead of exporting
- signature-level for your own apps
- Binder has no manifest guard
- validate everything that arrives
basics
~20 sExport only what another app must start: the launcher activity, link handlers, receivers that must hear other apps. Mark everything else android:exported="false", prefer PendingIntents and URI grants to exporting, and guard the rest with permissions, caller checks and input validation.
solid answer
~50 sI treat every exported component as a public API that any installed app can call. First an inventory of the merged manifest: each activity, receiver, provider and service gets an explicit `android:exported` value and a reason. Most needs are met without exporting: a callback is an immutable, explicit `PendingIntent`, a shared file is a `content://` URI with a grant flag, an app-internal event is a `RECEIVER_NOT_EXPORTED` receiver with `setPackage()`. What must stay exported gets a guard: a signature-level permission between our own apps, `checkCallingPermission()` inside a Binder method because a Binder object carries no manifest permission of its own, parameterized provider queries, and validation of every extra. For bound services I pick the narrowest interface: a local `Binder` in-process, a `Messenger` when calls can run one at a time, AIDL only when other apps need concurrent calls, since it must be thread-safe and backward compatible from its first release.
go deeper
Recall what android:exported does for each component type, and that a component with an intent filter must declare it explicitly.
Explain the safer alternatives to exporting: immutable PendingIntents, URI grants, not-exported receivers, and signature-level permissions between your own apps.
Show you can audit a merged manifest and harden what stays exported: caller permission checks in Binder methods, input validation, and no relaunching of nested intents.
Weigh integration reach against attack surface: every exported entry point is an API to keep compatible and secure, so set the review rule and the default of exporting nothing.
## The IPC surface is the attack surface Every component with `android:exported="true"` can be started, bound or queried by any app on the device that knows its class name, whatever intent filters say, unless a permission guards it. The platform's guidance on `android:exported` lists what an accidental export leads to: denial of service, other apps changing the app's internal behaviour, leaked data and code running in the app's context. So the lead's decision is not "is this component secure?" but "**which entry points do we offer at all, and to whom?**" ## Start from an inventory 1. Read the **merged manifest**, because libraries add components too (how merging works is covered with the manifest). 2. Give every activity, service, receiver and provider an explicit `android:exported` value. A component with an intent filter and no value cannot even be installed on Android 12 or higher, and older defaults differed by component type and release. 3. Write down, per exported component, who calls it and why. Components without a caller become unexported. Usually only a few survive: the launcher activity, activities that handle App Links or share targets, receivers that must hear other apps or privileged system broadcasts, and services or providers that a companion app really uses. ## Prefer delegation to exporting | Need | Instead of exporting | Use | |---|---|---| | another app calls you back later | an exported activity or receiver | an immutable `PendingIntent` around an explicit intent | | another app reads one file or row | an exported provider | a `content://` URI with `FLAG_GRANT_READ_URI_PERMISSION` | | parts of your app signal each other | an exported receiver | `RECEIVER_NOT_EXPORTED` plus `setPackage()` on the broadcast | | your own apps share data | an open provider or service | a permission at signature protection level | Each row replaces a door anyone can knock on with a capability handed to one caller. ## Bound services: choose the narrowest Binder A bound service returns an `IBinder` from `onBind()`, and the platform offers three ways to build it: - **Extending `Binder`** for a service private to the app and in the same process: no IPC, no export. - **A `Messenger`**, which queues all requests on one thread, so the service need not be thread-safe; the simplest cross-process choice. - **AIDL**, only when other apps make **concurrent** calls: calls from other processes arrive on a Binder thread pool, so the implementation must be completely thread-safe, and any interface change after the first release must stay backward compatible, because clients copy the `.aidl` file. Binder and Messenger objects are not declared in the manifest, so no declarative permission applies to an individual call. Guard the service with `android:permission` on `<service>` for binding, and call `checkCallingPermission()` inside each sensitive method. Clients trap `DeadObjectException` when the connection breaks. Always bind with an explicit intent: `bindService()` throws on implicit intents since Android 5.0 (API level 21). ## Treat what arrives as untrusted - **Validate every extra and URI** an exported component receives; intent filters are not a security feature. - **Never relaunch a nested intent** taken from another intent's extras; Android 12 added the StrictMode check `detectUnsafeIntentLaunch()` to catch it, and a `PendingIntent` is the safe alternative. - **Use `selectionArgs`** in provider queries instead of concatenating input into SQL. - On **Android 17 (API level 37)**, background activity launch restrictions extend to `IntentSender`; apps should move from `MODE_BACKGROUND_ACTIVITY_START_ALLOWED` to the narrower `MODE_BACKGROUND_ACTIVITY_START_ALLOW_IF_VISIBLE`. ## A worked inventory For the ride-hailing app, the review ends with a short list: - **Exported:** the launcher activity; the activity whose verified filter handles `https` ride links; a receiver for `ACTION_LOCALE_CHANGED`. - **Not exported:** the trips provider (the home-screen widget reads it in-process), the `FileProvider` behind receipt sharing, the trip-updated receiver, and the driver-chat service, which returns a local `Binder`. - **Handed out instead:** immutable notification tokens, one-shot payment callbacks and read grants on receipt URIs. ## Defaults the team enforces - An explicit `android:exported` on every component, checked in review against the merged manifest. - New exported components need a named caller and a guard. - `<queries>` lists only the packages or intents the app really looks up; `QUERY_ALL_PACKAGES` is a Google Play policy review, not a convenience. The trade-off is reach against risk: each exported entry point makes an integration easy and adds a surface the team must keep validating for as long as the app ships.
- Your app's bound service is called by a partner app. How does the service know the caller may perform a sensitive call?Binder methods have no manifest permission of their own, so check inside the method: `checkCallingPermission()` for a permission the caller must hold, typically a signature-level one if the partner is your own app. Put `android:permission` on `<service>` too, so apps without it cannot bind at all.
- When is AIDL worth its cost over a Messenger for a bound service?When other apps need concurrent calls or a typed, multi-method interface. A `Messenger` handles one message at a time on one thread, which is simpler and safe without locking. AIDL calls arrive on a Binder thread pool, so the service must be thread-safe, and the interface must stay backward compatible after release.
- Intent filters are enough to stop other apps from starting a component
- A Binder object can declare its own manifest permission, just like an activity
- AIDL is the default choice for any bound service, even in-process
- Exporting a provider is fine because the data is only read
- Leaving android:exported unset is safe because the default is always false
Assumes Android 17 (API 37), which extends background activity launch restrictions to IntentSender; the explicit android:exported requirement dates from Android 12 and detectUnsafeIntentLaunch() from Android 12 (API 31).