# App Bridge Android example

This sample contains a local notes publisher and a separate Android client that
tests the public App Bridge v1 contract and its optional background snapshot
extension. It ships 123 synthetic notes in **Sample folder**. The owner can grant
scoped reads and can edit or open the primary note. Background checks require a
separate, default-off choice. The sample uses Android Binder and app-owned
consent on a single device; its `localOnly` boundary prohibits cloud processing.
[App Bridge v1 specification](https://smarteragent.app/app-bridge/spec.md)

## Run the example

Download and extract the example, then open its directory in a shell. Install JDK
17 or later, Android SDK Platform 35, Build Tools 35.0.0 and Platform Tools. Set
`ANDROID_HOME` to the SDK directory and make the JDK tools available on `PATH`.
The scripts use Bash and `zip`. No Gradle or library downloads are needed once
the tools are installed. [Download example and tests](https://smarteragent.app/app-bridge/android-example.zip)

Start an unlocked Android 9 or later emulator reserved for testing. Use its
serial from `adb devices` in the second command.

```sh
bash build.sh
bash test.sh emulator-5554
```

The build creates `build/publisher.apk` and `build/client.apk`. It generates two
local test signing keys in `build/keys` and pins each app to the other's public
certificate. It also builds `client-wrong-signer.apk`, which has the same client
package but uses the publisher's signing key. Keep `build/` out of source
control and use dedicated signing keys for your own app.

The test script installs the publisher, runs the negative test with the
wrong-signer client, then installs the trusted client and runs the consent and
invocation checks. It refuses to replace either existing package and removes
the packages it installed when it exits. Look for `PASS wrongSigner` and
`PASS conformance` in the instrumentation output. Use an emulator in English
for the sample's accessibility selectors.

## What the compatibility checks cover

The client resolves the provider and checks its package, UID and signer. It
checks v1 JSON envelopes, invalid inputs, unsupported versions, the request
size limit and rejection of raw provider queries. It exercises real Android
activity results, explicit grant selection, cancellation and denied access.
After consent it checks correlation IDs, a scoped read, and a publisher-owned
immutable handoff. It checks a write and status receipt, reordered JSON replay,
changed requests using the same ID, and stale revisions. It revokes the grant,
then checks denial of further reads and receipt access, and safe rejection of
a handoff minted before revocation. The wrong-signer APK tests Binder calls and
the consent activity under the expected package name with a different signer.

The optional `changes` checks verify that an existing read grant and the
`requestBackground` hint do not authorize background reads. They exercise
cancelled and saved background consent, all 123 records across multiple pages,
stable event identities, changed content revisions, rejected cursors after grant
changes, and the `localOnly` processing label on every page. The snapshots read
native storage and are not an event journal or realtime push. The example allows
only separately authorized snapshot reads while locked; ordinary read/write/open
operations still require unlock. Qualify actual locked-device storage and
scheduling behavior separately on your publisher's supported devices.

The example's three capability IDs, records `sample` through `sample-123`, the excluded ID
`private`, the title change, revision numbers and the consent screen labels
are fixture checks. The publisher has one fixed local account scope and one
paired client. For an app with account switching or additional scopes, add
account and scope transition cases to the same checks. Test persistence and
crash recovery with that app's actual storage and backend. The example writes
its synthetic data and receipts atomically to a private file excluded from
Android backup.

## Adapt the tests to your app

1. In `client/AndroidManifest.xml`, replace the publisher package and authority
   under `<queries>`. In `BridgeCompatibility.java`, update `PUBLISHER` and
   `ACCESS_ACTIVITY` to match your publisher. Keep the provider ownership and
   certificate assertions. For a different publisher, change `build.sh` to
   supply `TrustedPublisher.SHA256` from that APK's verified signing certificate.
2. Build the compatibility client once and use its certificate fingerprint to
   configure your publisher's test trust policy. Use
   `apksigner verify --print-certs build/client.apk` to read it. Retain the same
   package with a different certificate for the negative case. Install your
   publisher fixture instead of `build/publisher.apk`. Update `test.sh` to name
   that fixture package and APK so its install and cleanup checks stay exact.
3. Set `READ`, `WRITE`, `OPEN`, `RECORD` and the fixture arguments and assertions
   in `BridgeCompatibility.java` to your advertised capabilities. Replace the
   consent text selectors in `verifyConsentAndScope` and
   `verifyWriteAndRevocation` with the labels your owner uses. For a read-only
   publisher, keep the scope, handoff and revoke checks that apply. If the app
   offers writes, test its real revision, replay and receipt semantics.

Start the publisher implementation with `BridgeProvider.java` for transport,
`AgentBridgeAccessActivity.java` for consent, and `BridgeIdentity.java` for
package and signer verification. The public specification describes the
protocol and the eligibility of publishers in Smarter Agent. Running these
tests against your app establishes the tested Android behavior independently
of whether a Smarter Agent release lists your publisher.

The example source and tests are available under the MIT license included in
the download.
