Sub topic 17
Incremental Builds and the Gradle Build Cache
When you run ./gradlew build twice in a row, the second run finishes much faster than the first. Gradle notices that nothing changed and skips the work it already did. This chapter explains how Gradle decides what to skip. It covers two features: incremental builds and the build cache.
Why Skipping Work Matters
A build is the full set of steps that turns your source code into a finished program. In earlier chapters you saw that Gradle runs a build as a set of tasks. A task is one unit of work, such as compiling code or running tests.
Small projects build quickly. Big projects can have hundreds of tasks. If every task ran every time, you would wait minutes after changing one line. Gradle avoids this by running only the tasks whose work is out of date.
Inputs and Outputs
Gradle can skip a task only if it knows what goes into the task and what comes out of it.
- An input is anything the task reads. For
compileJava, the inputs include your.javafiles, the classpath, and the Java version. - An output is anything the task writes. For
compileJava, the outputs are the.classfiles inbuild/classes.
Gradle takes a fingerprint of the inputs and outputs. A fingerprint is a short code computed from the contents of the files. If any file changes, its fingerprint changes too.
Up-to-Date Checks
Before running a task, Gradle runs an up-to-date check. It compares the current fingerprints with the ones it saved after the last run. If the inputs and outputs all match, the task is up to date, and Gradle skips it.
Try it with the App project from earlier chapters. Use --console=plain so Gradle prints one line per task.
./gradlew build --console=plain
./gradlew build --console=plainThe first run compiles and tests everything. The second run prints lines like these:
> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :jar UP-TO-DATE
> Task :compileTestJava UP-TO-DATE
> Task :test UP-TO-DATE
> Task :build UP-TO-DATE
BUILD SUCCESSFUL in 600msNow edit App.java, for example by changing "Hello, " to "Hi, ", and build again. compileJava runs because its input changed. test runs too, because its input (the compiled classes) changed. Your test will now fail, which shows that Gradle did check your new code.
Task Outcome Labels
Gradle labels each task with what happened to it.
| Label | Meaning |
|---|---|
| (no label) | The task ran its actions. |
| `UP-TO-DATE` | Inputs and outputs did not change, so the task was skipped. |
| `FROM-CACHE` | The outputs were copied from the build cache instead of being made again. |
| `NO-SOURCE` | The task had no input files, so there was nothing to do. |
| `SKIPPED` | The task was turned off, for example with `-x test`. |
Incremental Java Compilation
An up-to-date check works on the whole task. Gradle also goes one step further inside compileJava. When only some source files change, Gradle recompiles just those files and the files that depend on them. This is called incremental compilation. In a project with thousands of classes, editing one class does not force a full recompile.
Declaring Inputs and Outputs on Your Own Tasks
Built-in tasks already declare their inputs and outputs. A custom task does not, unless you add them. A task with no declared outputs is never up to date, so it runs every time.
This task writes a greeting to a file. It declares one input property and one output file. Add it to build.gradle.kts:
tasks.register("generateGreeting") {
group = "custom"
description = "Writes a greeting to build/greeting.txt"
val message = "Hello from Gradle"
val outputFile = layout.buildDirectory.file("greeting.txt")
inputs.property("message", message)
outputs.file(outputFile)
outputs.cacheIf { true }
doLast {
outputFile.get().asFile.writeText(message)
}
}Here is what each part does:
inputs.propertytells Gradle that the text is an input. If you change the text, the task reruns.outputs.filetells Gradle which file the task creates. If someone deletes that file, the task reruns.outputs.cacheIf { true }lets Gradle store this task's output in the build cache, which the next section covers.
Run it twice:
./gradlew generateGreeting --console=plain
./gradlew generateGreeting --console=plainThe second run shows > Task :generateGreeting UP-TO-DATE.
The Build Cache
Up-to-date checks only compare a task with its own last run in the same folder. Suppose you run ./gradlew clean. That deletes the build folder, so every task has to run again.
The build cache fixes this. It is a store of task outputs, kept outside your project. Gradle computes a cache key for each task from its inputs. After a task runs, Gradle saves its outputs under that key. Later, if a task has the same key, Gradle copies the saved outputs back instead of doing the work. The task then shows FROM-CACHE.
The cache is off by default. Turn it on for one run with a flag:
./gradlew build --build-cache
./gradlew clean
./gradlew build --build-cache --console=plainAfter the clean, the last command shows compileJava FROM-CACHE instead of compiling again.
To keep the cache on for every build, add this line to a gradle.properties file in the project's root folder:
org.gradle.caching=trueBy default the cache lives on your machine, in ~/.gradle/caches/build-cache-1. Teams can also set up a remote build cache on a shared server. Then a result built on one machine, such as the CI server, can be reused on a developer's laptop.
Edge Cases and Common Problems
- Changing inputs every run. If an input includes the current time or a random number, the fingerprint is new every time. The task never becomes up to date and never hits the cache.
- Forcing a full rebuild. Use
./gradlew build --rerun-tasksto ignore up-to-date checks and the cache. This helps when you suspect a stale result. - Different machines, different paths. Gradle's built-in tasks avoid putting absolute file paths in their cache keys, so caches work across machines. Your own tasks should do the same by using
layout.buildDirectoryinstead of fixed paths. - Tests and the cache. The
testtask is cacheable. If neither the code nor the tests changed, Gradle may not rerun them. Use./gradlew test --rerunto force the tests to run once.
Summary
Gradle skips a task when its inputs and outputs have not changed (UP-TO-DATE). It restores outputs from the build cache when it has seen the same inputs before (FROM-CACHE). Incremental compilation recompiles only the changed files. Both features depend on tasks declaring their inputs and outputs correctly.
Exercise
You run ./gradlew build, then ./gradlew clean, then ./gradlew build again without the build cache turned on. Will compileJava show UP-TO-DATE? Why or why not?
Show answer
No. The clean task deleted the build folder, so the outputs of compileJava no longer match what Gradle saved. The task must run again. With the build cache on, it would show FROM-CACHE instead.
Exercise
A custom task has a doLast block that writes a file, but it declares no inputs or outputs. How often does it run, and how do you fix that?
Show answer
It runs on every build, because Gradle has no outputs to check. Declare its inputs with inputs.property or inputs.file and its output with outputs.file, and it will be skipped when nothing has changed.
Exercise
Change the message in the generateGreeting task to "Hi from Gradle" and run it twice. What label do you see each time?
Show answer
The first run has no label, meaning it executed, because the message input changed. The second run shows UP-TO-DATE.