Sub topic 17
Incremental Builds and the Gradle Build Cache
When you run a build twice in a row, the second run should not repeat work that is already done. Gradle avoids that repeated work in two ways: incremental builds and the build cache. This chapter shows both.
Inputs and Outputs
Every Gradle task has inputs and outputs. An input is something a task reads, such as source files or settings. An output is something a task writes, such as class files or a JAR.
Before Gradle runs a task, it compares the current inputs and outputs with what it recorded last time. If nothing has changed, Gradle skips the task and marks it UP-TO-DATE. This skipping is called an incremental build.
Seeing UP-TO-DATE in Action
Run the build twice. The --console=plain option prints each task's result on its own line.
./gradlew build --console=plain
./gradlew build --console=plainOn the second run, you will see lines like these:
> Task :compileJava UP-TO-DATE
> Task :test UP-TO-DATENow edit App.java and build again. Gradle runs compileJava again, and every task that depends on its output runs too.
Declaring Inputs and Outputs for Your Own Task
The built-in tasks already declare their inputs and outputs. A custom task must declare them itself, or Gradle cannot tell when it is safe to skip the task. Add this to build.gradle.kts, and create a file named notes.txt in the project folder:
tasks.register("makeReport") {
val source = file("notes.txt")
val report = layout.buildDirectory.file("report.txt")
inputs.file(source)
outputs.file(report)
doLast {
report.get().asFile.writeText(source.readText().uppercase())
}
}Run ./gradlew makeReport --console=plain twice. The second run reports UP-TO-DATE. Change notes.txt, and the task runs again.
The Build Cache
An incremental build only remembers the most recent run. The build cache goes further. It is a store of task outputs, saved under a key that Gradle computes from the task's inputs. If those same inputs come back later, even after clean, Gradle copies the saved outputs instead of running the task. Those tasks are marked FROM-CACHE.
To turn on the cache for every build, add this line to gradle.properties in the project folder:
org.gradle.caching=trueYou can also turn it on for a single run:
./gradlew clean build --build-cache --console=plainAfter clean deletes the build folder, tasks such as compileJava show FROM-CACHE instead of running from scratch.
Summary
| Result | Meaning |
|---|---|
| UP-TO-DATE | Nothing changed since the last run, so Gradle skipped the task |
| FROM-CACHE | Gradle reused outputs saved from an earlier run with the same inputs |
| (no label) | The task actually ran |
Exercise
You run ./gradlew clean build twice with the build cache turned off. Will compileJava be UP-TO-DATE on the second run? Why?
Show answer
No. The clean task deletes the build folder, so the outputs that compileJava made are gone and the task must run again. With the build cache turned on, it would show FROM-CACHE instead.
Exercise
A custom task has no declared inputs or outputs. What happens when you run it twice?
Show answer
It runs both times. Gradle has nothing to compare, so it cannot prove the task is up to date.