<!-- KeepSnappy docs for keepsnappy CLI 0.1.0-alpha.35 -->

# Android

> Measure an Android app on an emulator or on a phone on a cable.

This page sets up Android, on an emulator in your CI or on a phone on a cable. For real phones on BrowserStack, iOS or Android, see [BrowserStack](https://keepsnappy.com/docs/browserstack.md).

## Android emulator

Start with `keepsnappy init --detect`. Init can copy a recognized Android emulator job, for example one that uses `reactivecircus/android-emulator-runner`. If it cannot use your job, add KeepSnappy by hand with the steps below. This manual setup is new and hasn't been tested much in CI. In your first session's log, check that every run is valid, such as `5/5 runs valid` for one journey.

The job needs these:

- A Release APK with the SDK on ([Make a Release build](https://keepsnappy.com/docs/sdk.md#make-a-release-build)). On a Linux KVM runner, build it for `x86_64`.
- A link scheme that your app registers, such as `expo.scheme` in `app.json`. Before each run, KeepSnappy opens a link in that scheme (such as `myapp://…`) to switch measuring on: the [activation link](https://keepsnappy.com/docs/glossary.md#activation). Name the scheme in `app.scheme` in `keepsnappy.yml`, with `://`.
- The SDK key and the API address in the build. On Android, the app sends its own measurements, so the emulator must reach that address. The build reads the address as `EXPO_PUBLIC_KEEPSNAPPY_INGEST_URL`; the job below fills it from the repository variable `KEEPSNAPPY_API`.
- `@keepsnappy/agent` as a dev dependency ([Install](https://keepsnappy.com/docs/install.md#the-command-line-tool)).

Copy the SDK key and the API address from **Setup**, under **Builds that send their own measurements**. Store them in GitHub as a secret and a variable:

```sh
gh secret set EXPO_PUBLIC_KEEPSNAPPY_KEY
gh variable set KEEPSNAPPY_API --body "<the API address from Setup>"
```

Paste the SDK key when `gh` asks for the secret. It is not your CI key. Then change your job in three places: the build gets the SDK on with both values, the checkout gets the pull request's head with its whole history, and your test command runs through `keepsnappy run`.

```yaml
jobs:
  e2e-android:
    env:
      KEEPSNAPPY: '1'
      EXPO_PUBLIC_KEEPSNAPPY_KEY: ${{ secrets.EXPO_PUBLIC_KEEPSNAPPY_KEY }}
      EXPO_PUBLIC_KEEPSNAPPY_INGEST_URL: ${{ vars.KEEPSNAPPY_API }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          ref: ${{ github.event.pull_request.head.sha || github.sha }}
      # Your steps that install packages and build the Release APK stay as they are.
      - uses: reactivecircus/android-emulator-runner@v2
        env:
          KEEPSNAPPY_KEY: ${{ secrets.KEEPSNAPPY_KEY }}
        with:
          # Your emulator settings stay as they are.
          script: npx --no-install keepsnappy run --platform android-emulator --upload --wait --commit ${{ github.event.pull_request.head.sha || github.sha }} -- <your test command>
```

`--platform android-emulator` lets the job share `keepsnappy.yml` with your iOS job. If your Android package is not the iOS bundle id, write a second file with its own `app.bundle_id`, and pass it with `--config`.

### Your tests on the emulator

Your test command must start the app fresh, without clearing its data or reinstalling it. Before each run, KeepSnappy opens the activation link, and the app keeps that switch in its data. A run whose app was cleared or reinstalled does not count.

Init does not set up Detox, but a Detox command works as [the `command` setting](https://keepsnappy.com/docs/glossary.md#the-command-setting). Pass `--reuse`, as in this example:

```yaml
app:
  bundle_id: com.example.app
  scheme: myapp://
platform: android-emulator
repetitions: 5
flows:
  runner: command
  command: npx detox test --configuration android.emu.release --reuse
```

To try it on your own machine first, with an emulator already running:

```sh
npx @keepsnappy/agent run --app ./app-release.apk --upload --wait --branch main
```

`--app` installs the APK once (adb's install, replacing the installed app); omit it for an already installed app. The `keepsnappy` command selects the only online `emulator-<port>` serial. With several, name one with `--device`, for example `emulator-5554`. It never boots an emulator, and with `platform: android-emulator` it never uses a phone: for a USB phone, see [Android USB phone](#android-usb-phone).

If your tests send [step markers](https://keepsnappy.com/docs/glossary.md#step-marker), they find where to send them in the two variables the test command receives, `KEEPSNAPPY_MARKER_URL` and `KEEPSNAPPY_RUN_ID`. A failed test command, or a run whose upload the app never confirms, makes that run invalid. The other runs still count. With no data from any run, the session reads **Session failed**.

## Android USB phone

Run on a real Android phone over a cable. The setup is the one of the emulator above with one change: set `platform` in `keepsnappy.yml` to `android-device` instead of `android-emulator`. You also need `adb` on PATH, a Release APK carrying the SDK, `app.scheme`, and a test command that starts the app fresh without clearing its data (see [Android emulator](#android-emulator)). On the phone, turn on USB debugging and allow the computer when the phone asks.

```sh
adb devices
npx --no-install keepsnappy run --app ./app-release.apk --upload --wait --branch main
```

The command needs `@keepsnappy/agent` as a dev dependency ([Install](https://keepsnappy.com/docs/install.md#the-command-line-tool)). `adb devices` must list the phone as `device`.

The `keepsnappy` command uses the only phone attached to adb. With `android-device`, it ignores emulators; with `android-emulator`, it ignores phones. A run never moves to a real phone because an emulator is missing. A phone on adb over Wi-Fi counts as a phone.

With several phones, or with one that is not in the state `device`, the command stops before it opens a session. It lists what it sees. Name the phone with the `--device` flag or the `device` setting in `keepsnappy.yml`. Use the serial from the first column of `adb devices`, for example `RFCW10FJQ6D`.

A phone listed as `unauthorized` is waiting for you to allow USB debugging on its screen. `offline` means reconnect the cable. The session is stored with the phone's model, read from the phone (for example `SM-S911B`).

The `keepsnappy` command sets `ANDROID_SERIAL` to the chosen device's serial for your test command, on both emulators and phones. Test tools that honour it (adb, Detox, Appium's UiAutomator) drive the device that got the activation link. A test tool that picks its device another way must be told the serial.

A phone is shared: nothing else should drive it while the session runs, and the `keepsnappy` command does not lock it for you.

### What the run records about the phone

Before each local Android run, KeepSnappy reads the app's ART compile state before warm-up. Just before the test command starts, it records thermal status, battery temperature, CPU frequency caps and power saver state while the app is stopped.

The readings are evidence only. KeepSnappy starts the test command after it takes the readings without waiting for the phone to cool. The Run page shows them in the rows "Phone at launch" and "App compile state". Emulators show "not supported" for launch heat and can still report the ART compile state.

All KeepSnappy docs for agents: https://keepsnappy.com/llms.txt
