Skip to content

Cache Gradle downloads in the Android build - #7

Merged
dwd merged 1 commit into
dwd:mainfrom
Fishbowler:android-gradle-cache
Nov 20, 2026
Merged

dwd merged 1 commit into
dwd:mainfrom
Fishbowler:android-gradle-cache

Conversation

@Fishbowler

Copy link
Copy Markdown
Contributor

Problem

The build-android job has no Gradle cache. The Flutter action caches the Flutter SDK, not ~/.gradle. So every run downloads all of Gradle's dependencies again, including the plugins for Flutter's own Gradle plugin loader.

That makes the job slower, and it makes it fail whenever those servers have a hiccup. On my fork, one run failed while resolving Flutter's plugin loader because the Gradle Plugin Portal answered "not found" for an artifact that exists:

Could not find org.jetbrains.kotlin:kotlin-stdlib:2.2.21.
  Searched in: https://plugins.gradle.org/m2/org/jetbrains/kotlin/kotlin-stdlib/2.2.21/kotlin-stdlib-2.2.21.pom

A re-run of the same commit passed. Gradle's own download retries don't cover this kind of failure, because a "not found" response counts as a definite answer and isn't retried.

Change

Add gradle/actions/setup-gradle@v6 to build-android, just before the first Gradle build. It installs init scripts that apply to every Gradle run in the job, including the ones flutter build apk starts. It caches Gradle's home directory between runs: dependencies, wrapper distributions and generated jars.

This is the only job that runs Gradle. maestro-e2e uses the APK that build-android produced.

Behaviour worth knowing:

  • Only runs on the default branch write the cache. Branch and pull-request runs restore main's cache but don't save their own. So the benefit starts after the first main run with this change.
  • It restores the closest match. The cache key changes with each commit, so the closest earlier cache is restored and only what's missing is downloaded. Unused entries are cleaned up after successful runs.
  • Defaults otherwise. It also adds a Gradle build summary to the job page, validates Gradle wrapper jars, and prints a notice about the proprietary caching provider in the log. It also writes the runner's JDK locations to ~/.m2/toolchains.xml, which the Flutter build doesn't use.

Licensing: the default enhanced cache provider uses Gradle's proprietary gradle-actions-caching library under the Gradle Technologies Terms of Use. It is free for public repositories (DISTRIBUTION.md). If you'd rather keep CI fully open source, cache-provider: basic is an MIT-licensed alternative built on actions/cache. The trade-off: it only matches the cache exactly on a hash of the repo's Gradle files, with no partial matching or cleanup, so the first run after any Gradle file change starts from an empty cache. It's a one-line change either way.

I considered actions/setup-java with cache: gradle and decided against it. It would also pin the JDK version, and its cache only matches exactly on the repo's Gradle files.

Testing

  • The workflow YAML parses, and the new step sits in build-android immediately before Build Android (release).
  • The change merges cleanly with Fix CI build versioning: real pre-release versions, build numbers, Apple and MSIX versions #5 and Make pre-release versions sort after the release they follow #6.
  • flutter analyze, flutter test and vendor/xmpp_stone dart test pass. The app code is unchanged.
  • CI on my fork, run 44543986332: build-android passed, with the release and debug builds both green under the action. The action behaved as documented for a branch run. It used the enhanced provider with cache-read-only: true. It logged "Gradle User Home cache not found. Will initialize empty." on restore, and "Cache is read-only: will not save state" in the post step. It also produced the job summary (Gradle 9.3.1, assembleRelease and assembleDebug successful). maestro-e2e passed against the APK built this way. Every other job passed, apart from build-macos and build-ios, which GitHub never started because of macOS runner capacity; this change doesn't touch them. Cache hits can only show up on main, after the first run there has saved the cache.

🤖 Generated with Claude Code

build-android had no Gradle cache (the Flutter action caches the
Flutter SDK, not ~/.gradle), so every run downloaded all of Gradle's
dependencies, including the plugins for Flutter's own Gradle plugin
loader. That is slow, and makes the job fail whenever those
repositories hiccup: a fork run failed with the Gradle Plugin Portal
answering "Could not find org.jetbrains.kotlin:kotlin-stdlib:2.2.21"
for an artifact that exists, and passed on re-run. Gradle's own
download retries don't help there, as a not-found response is treated
as definite.

Add gradle/actions/setup-gradle@v6 to build-android, before the first
Gradle build. Its init scripts apply to every Gradle run in the job,
including those started by `flutter build apk`, and it caches the
Gradle User Home (dependencies, wrapper distributions, generated jars)
between runs. Only default-branch runs write the cache; branch and
pull-request runs restore it. build-android is the only job that runs
Gradle; maestro-e2e uses the APK it uploads.

It uses the default (enhanced) cache provider: proprietary, under the
Gradle Technologies Terms of Use, free for public repositories, with
restore-key matching and cleanup. `cache-provider: basic` is the
MIT-licensed alternative.

No retry around the build step: with a warm cache the repositories are
rarely contacted, and a retry would mostly delay genuine failures.

Testing:
- Workflow YAML parses; the step is in build-android immediately
  before "Build Android (release)".
- Merges cleanly with the version-fixes and prerelease ordering
  branches (dwd#5, dwd#6).
- flutter analyze: no issues. flutter test: all 1044 passed.
  vendor/xmpp_stone dart test: all 190 passed.
- Maestro flows not run locally: CI-only change; the CI maestro-e2e
  job covers them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dwd
dwd merged commit 0c799f9 into dwd:main Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants