GitHub Actions Gradle cache that cut our Android CI from 11 minutes
Our Android CI was taking 11 minutes for a build that felt like it should take three. Most of it was Gradle downloading the same dependencies over and over because the workflow had no caching at all, and someone had pinned java-version: '21' in setup-java while the wrapper was still on Gradle 7.6.4. Here's the workflow that got us to about four minutes.
## The failure that started this
Every PR build died the same way:
* What went wrong:
Could not determine java version from '21.0.2'.
Gradle 7.6.4 predates Java 21 support. We had upgraded the runner image, which picked up a newer default JDK, and the explicit setup-java step just made it permanent. Two ways out: bump the wrapper to Gradle 8.7, or pin Java 17. We pinned 17 because AGP 8.4 was the version we were on and I didn't want to be debugging two upgrades at once.
## The workflow
name: android-ci
on: [push, pull_request]jobs:
build:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '17'
- uses: gradle/gradle-build-action@v3
with:
gradle-version: wrapper
cache-read-only: ${{ github.ref != 'refs/heads/main' }}
- run: ./gradlew :app:assembleDebug --stacktrace
- uses: actions/upload-artifact@v4
with:
name: debug-apk
path: app/build/outputs/apk/debug/*.apk
gradle/gradle-build-action@v3 is the important bit. It caches the Gradle distribution, the dependency cache under ~/.gradle/caches, and the build cache, with separate keys for each. cache-read-only stops feature branches from overwriting the main branch's cache, that bit me once when a branch added a dependency and poisoned the shared cache.
## What the numbers actually were
Before: 11m 02s median, mostly dependency downloads. After: 4m 18s with a warm cache, 6m 40s when the cache key changed. The key is based on the wrapper checksum and the hash of Gradle files, so a build.gradle edit invalidates it, which is correct, because that's when dependencies usually change.
We also turned off the Gradle daemon in CI with org.gradle.daemon=false in gradle.properties. The action sets this itself, but we had it in the repo already from an old debugging session. I'd rather have a slightly slower but predictable build than a daemon that outlives the step and holds a lock on the cache directory.
One more small win: I changed the build task to :app:assembleDemoDebug so CI produces the exact variant I install on my phone. That caught a flavor-specific resource error two weeks later which would otherwise have turned up in a test build.
## Two things I'd still fix
The on: [push, pull_request] trigger means some branches build twice. We should narrow it to main for push. And the action still downloads the Gradle distribution on the first run of a new wrapper version, which is unavoidable unless we vendor it into the repo, which I'm not doing.
If you're on a self-hosted runner, set GRADLE_USER_HOME to a path on the big disk. We filled up a 40 GB runner volume in a week before I noticed.