# Android builds

Android apps build on Groundbox with the same actions as on GitHub's runners. Copy this workflow, or change `runs-on` in yours:

.github/workflows/android.yml

```
name: Android
on: [push, pull_request]

jobs:
  build:
    runs-on: groundbox
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 21
      - uses: android-actions/setup-android@v3
      - uses: gradle/actions/setup-gradle@v4
      - run: ./gradlew assembleDebug lint testDebugUnitTest
```

- **Java:** Temurin 17 and 21 are on the runner already, so `setup-java` finds them without a download.
- **Android SDK:** it is not on the runner. Keep `android-actions/setup-android`: it installs the command-line tools and accepts the licenses in the job's VM. The Android Gradle plugin downloads the platform and build tools your project asks for.
- **Gradle:** `gradle/actions/setup-gradle` keeps Gradle and its dependencies in the Actions cache, which is on the same machine as the job.
- **Size:** `runs-on: groundbox` gives a Gradle build more cores or memory when its runs need them. See [auto size](https://groundbox.dev/docs/ci/auto-size).

## Reuse tasks between builds

Turn on Gradle's build cache. On Groundbox runners it already points at your team's [build cache](https://groundbox.dev/docs/build-caches), so a task one build ran is not run again by the next:

gradle.properties

```
org.gradle.caching=true
org.gradle.parallel=true
```

## Sign a release build

Keep the keystore in a repository secret as base64 (`base64 < release.jks`), with its passwords and alias in secrets of their own. Write it to a file in the job, then build:

.github/workflows/android.yml

```
      - name: Keystore
        run: echo "$KEYSTORE_BASE64" | base64 -d > "$RUNNER_TEMP/release.jks"
        env:
          KEYSTORE_BASE64: ${{ secrets.KEYSTORE_BASE64 }}
      - run: ./gradlew bundleRelease
        env:
          KEYSTORE_PATH: ${{ runner.temp }}/release.jks
          KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}
          KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
          KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
```

app/build.gradle.kts

```
android {
    signingConfigs {
        create("release") {
            storeFile = System.getenv("KEYSTORE_PATH")?.let { file(it) }
            storePassword = System.getenv("KEYSTORE_PASSWORD")
            keyAlias = System.getenv("KEY_ALIAS")
            keyPassword = System.getenv("KEY_PASSWORD")
        }
    }
    buildTypes {
        release {
            signingConfig = signingConfigs.getByName("release")
        }
    }
}
```

The file is in `RUNNER_TEMP`, and the VM is destroyed when the job ends. Upload the AAB from `app/build/outputs/bundle/release/` with `actions/upload-artifact`.

## Emulator tests

The runners have KVM, so the Android emulator runs with hardware acceleration. Run instrumented tests (`connectedAndroidTest`) with `reactivecircus/android-emulator-runner`:

.github/workflows/emulator.yml

```
name: Android emulator tests
on: [push, pull_request]

jobs:
  instrumented:
    runs-on: groundbox-8vcpu
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-java@v6
        with:
          distribution: temurin
          java-version: 21
      - uses: android-actions/setup-android@v4
      - uses: gradle/actions/setup-gradle@v6
      - uses: reactivecircus/android-emulator-runner@v2
        with:
          api-level: 34
          arch: x86_64
          target: google_apis
          emulator-options: -no-window -gpu swiftshader_indirect -no-snapshot -noaudio -no-boot-anim
          disable-animations: true
          script: ./gradlew connectedDebugAndroidTest
```

- **Size:** Gradle and the emulator both want cores, so use `runs-on: groundbox-8vcpu`.
- **Headless:** keep `-no-window` and `-noaudio`. The runner has no display or sound.
