TExcelTAD

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:

text
command line -P / -D            highest
~/.gradle/gradle.properties      (GRADLE_USER_HOME)  per machine: CI runner, developer laptop
<project>/gradle.properties      committed defaults for everyone

The 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:

bash · try it yourself
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:

properties
org.gradle.jvmargs=-Xmx4g -XX:+UseParallelGC -Dfile.encoding=UTF-8
org.gradle.workers.max=2
kotlin.daemon.jvmargs=-Xmx2g

In 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

bash · try it yourself
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:

  1. Cold: the task graph is calculated and stored; every task executes and stores its outputs in the build cache.
  2. Warm daemon, nothing changed: the configuration is reused (no build script runs at all) and every task is up to date.
  3. After clean: the outputs are gone, but most tasks come back FROM-CACHE instead 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.
  4. 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:

kotlin
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

bash
./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?

  1. Gradle ignores jvmargs
  2. ~/.gradle/gradle.properties (the Gradle user home) overrides the project file
  3. AGP caps memory
  4. The wrapper sets it

After clean, most tasks report FROM-CACHE. Which feature is that?

  1. The configuration cache
  2. The build cache (org.gradle.caching=true)
  3. The Gradle daemon
  4. Incremental compilation

What does the configuration cache skip on a hit?

  1. Compiling Java
  2. Running build scripts and building the task graph
  3. Downloading dependencies
  4. Signing the APK