Gradle Performance on CI
Android builds are among the slowest in everyday software: AGP creates dozens of tasks per variant and configures them every time. On a laptop a warm daemon hides most of it; on CI every job starts cold. Four settings decide how much of that cost you pay again on every run.
| Setting | What it saves | Where |
|---|---|---|
| Configuration cache | the configuration phase (evaluating build scripts, creating the task graph) | org.gradle.configuration-cache=true |
| Build cache | task execution: outputs reused by cache key, even after clean or on another machine |
org.gradle.caching=true |
| JVM arguments | GC thrashing and out-of-memory kills in small CI containers | org.gradle.jvmargs=… |
| Workers | parallelism inside and across projects | org.gradle.workers.max, org.gradle.parallel=true |
Where properties come from
Gradle reads gradle.properties from several places, and the Gradle user home wins over the project:
command line -P / -D highest
~/.gradle/gradle.properties (GRADLE_USER_HOME) per machine: CI runner, developer laptop
<project>/gradle.properties committed defaults for everyoneThe container for this course has a user-home file (org.gradle.workers.max=1, a plain console, and its own JVM arguments), so watch what happens when the project asks for more memory:
cat ~/.gradle/gradle.properties | grep -v '^#'
mkdir -p hello && cd hello
echo 'rootProject.name = "hello"' > settings.gradle.kts
printf 'org.gradle.jvmargs=-Xmx2g -XX:+UseParallelGC\norg.gradle.caching=true\n' > gradle.properties
gradle -q properties | grep -E '^org.gradle'org.gradle.console=plain
org.gradle.welcome=never
org.gradle.workers.max=1
org.gradle.jvmargs=-Xmx768m -XX:+UseSerialGC
org.gradle.caching: true
org.gradle.console: plain
org.gradle.jvmargs: -Xmx768m -XX:+UseSerialGC
org.gradle.welcome: never
org.gradle.workers.max: 1
The project's org.gradle.caching=true applied, but its jvmargs lost to the machine's. That's useful (a CI image can cap memory for every project it builds) and confusing (a developer's old ~/.gradle/gradle.properties silently overrides the team's settings). When numbers don't add up, run gradle -q properties | grep org.gradle first.
Sizing the JVM for CI
The Gradle daemon runs AGP, D8/R8 and aapt2 orchestration in one JVM; R8 on a large app needs several GB of heap. On a 7 GB GitHub runner a common starting point is:
org.gradle.jvmargs=-Xmx4g -XX:+UseParallelGC -Dfile.encoding=UTF-8
org.gradle.workers.max=2
kotlin.daemon.jvmargs=-Xmx2gIn a container with less memory, fewer workers and a smaller heap beat an out-of-memory kill. With one worker, as in this course's container, tasks run strictly one after another: slower, but deterministic and frugal, which is also a reasonable choice for small CI containers.
Configuration cache and build cache, measured
mkdir -p hello/app/src/main/java/com/example/hello && cd hello
cat > settings.gradle.kts <<'KTS'
pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } }
dependencyResolutionManagement { repositories { google(); mavenCentral() } }
rootProject.name = "hello"
include(":app")
KTS
echo 'plugins { id("com.android.application") version "9.4.1" apply false }' > build.gradle.kts
echo '<manifest xmlns:android="http://schemas.android.com/apk/res/android"><application android:label="Hello" /></manifest>' > app/src/main/AndroidManifest.xml
printf 'package com.example.hello;\npublic final class Util { public static int twice(int x) { return 2 * x; } }\n' > app/src/main/java/com/example/hello/Util.java
cat > app/build.gradle.kts <<'KTS'
plugins { id("com.android.application") }
android {
namespace = "com.example.hello"
compileSdk = 36
defaultConfig { applicationId = "com.example.hello"; minSdk = 24; targetSdk = 36; versionCode = 1; versionName = "1.0" }
}
KTS
printf 'org.gradle.configuration-cache=true\norg.gradle.caching=true\n' > gradle.properties
gradle wrapper > /dev/null
summary() { grep -E 'configuration cache|Configuration cache|actionable'; }
echo "=== 1. first build"
./gradlew assembleDebug | summary
echo "=== 2. nothing changed"
./gradlew assembleDebug | summary
echo "=== 3. after clean (as on a fresh CI checkout with a restored cache)"
./gradlew -q clean
./gradlew assembleDebug | summary
echo "=== 4. a build script change invalidates the configuration cache"
sed -i 's/versionCode = 1;/versionCode = 2;/' app/build.gradle.kts
./gradlew assembleDebug | summary=== 1. first build
33 actionable tasks: 33 executed
Configuration cache entry stored.
=== 2. nothing changed
Reusing configuration cache.
33 actionable tasks: 33 up-to-date
Configuration cache entry reused.
=== 3. after clean (as on a fresh CI checkout with a restored cache)
Reusing configuration cache.
33 actionable tasks: 12 executed, 21 from cache
Configuration cache entry reused.
=== 4. a build script change invalidates the configuration cache
Calculating task graph as configuration cache cannot be reused because file 'app/build.gradle.kts' has changed.
33 actionable tasks: 7 executed, 26 up-to-date
Configuration cache entry stored.
Reading the four runs:
- Cold: the task graph is calculated and stored; every task executes and stores its outputs in the build cache.
- Warm daemon, nothing changed: the configuration is reused (no build script runs at all) and every task is up to date.
- After
clean: the outputs are gone, but most tasks come backFROM-CACHEinstead of executing. This is the CI case: a fresh checkout plus a restored~/.gradle/caches/build-cache-1(or a remote cache) skips most work. - Changed build script: Gradle explains why it can't reuse the configuration and recomputes it; tasks whose inputs didn't change are still up to date.
Caching on CI
| Cache | Speeds up | How |
|---|---|---|
~/.gradle/caches/modules-2, ~/.gradle/wrapper |
dependency and wrapper downloads | the CI's cache action keyed on wrapper properties + *.gradle.kts + libs.versions.toml |
~/.gradle/caches/build-cache-1 (local build cache) |
task outputs | the same cache action; works with org.gradle.caching=true |
| remote build cache | task outputs across all machines and developers | a cache node (Develocity, or the open-source gradle/build-cache-node image) |
| configuration cache entries | the configuration phase | the project's .gradle/configuration-cache folder; worth persisting only if the cache action supports it |
On GitHub Actions, gradle/actions/setup-gradle handles the first two (and cleans up stale entries); see GitHub Actions for Android Releases. A remote cache is configured in settings.gradle.kts, with CI as the only writer:
buildCache {
remote<HttpBuildCache> {
url = uri("https://gradle-cache.example.com/cache/")
isPush = System.getenv("CI") != null // developers read, CI writes
credentials {
username = providers.environmentVariable("GRADLE_CACHE_USER").orNull
password = providers.environmentVariable("GRADLE_CACHE_PASSWORD").orNull
}
}
}Finding the slow part
./gradlew assembleRelease --profile # HTML report in build/reports/profile/
./gradlew assembleRelease --scan # Build Scan (uploads to scans.gradle.com or your Develocity server)--profile works offline and splits time into configuration, dependency resolution and task execution. Build Scans add the task timeline, cache hit rates and "why did this task run?"; the Toolkit lesson covers Android Studio's Build Analyzer and the Gradle Profiler for repeatable benchmarks.
Test yourself
The project's gradle.properties sets org.gradle.jvmargs=-Xmx2g but the build uses -Xmx768m. Why?
- Gradle ignores jvmargs
- ~/.gradle/gradle.properties (the Gradle user home) overrides the project file
- AGP caps memory
- The wrapper sets it
After clean, most tasks report FROM-CACHE. Which feature is that?
- The configuration cache
- The build cache (org.gradle.caching=true)
- The Gradle daemon
- Incremental compilation
What does the configuration cache skip on a hit?
- Compiling Java
- Running build scripts and building the task graph
- Downloading dependencies
- Signing the APK