Gradle cannot store the configuration cache for an Android build
Configuration cache state could not be cachedShort answer
Gradle refuses to store the configuration cache when a task or plugin holds something that cannot be saved. Open the HTML report it links to. The message names the task and field. The usual fixes are removing project access from task actions, moving live objects out of task fields, and replacing build listeners with a build service.
Fix it fast
1Find out which cause you have
Run the build again and open the HTML report path printed at the end. The report groups problems by message and by task, so you see which task and which field caused each one.
./gradlew assembleDebug -Dorg.gradle.configuration-cache=true2Match what you see, then jump to its fix
- 1
invocation of 'Task.project' at execution time is unsupported with the configuration cache.A task action uses Project at execution timeFix - 2
field 'x' of task ... cannot serialize object of type ...A task field holds an object Gradle cannot serializeFix - 3
The report points at gradle.taskGraph.afterTask or gradle.buildFinished in a build script or plugin.A build listener runs on task graph or build completionFix
- 1
What does the error look like?
Configuration cache state could not be cachedinvocation of 'Task.project' at execution time is unsupported with the configuration cache.cannot serialize object of typeConfiguration Cache entry discarded with 1 problem.
Cause 1 of 3
A task action uses Project at execution time
1Check
Search your build scripts and buildSrc or convention plugins for project.copy, project.exec, project.delete, project.logger or getProject() inside doLast, doFirst or a @TaskAction.
grep -rnE "project\.(copy|exec|delete|logger)" build.gradle* app/build.gradle* buildSrc build-logic 2>/dev/null2Fix
Inject the matching service into the task instead. FileSystemOperations replaces project.copy and project.delete, ExecOperations replaces project.exec, and Task.logger replaces project.logger.
abstract class CopyDocs : DefaultTask() {
@get:Inject abstract val fs: FileSystemOperations
@TaskAction fun run() {
fs.copy {
from("docs")
into(layout.buildDirectory.dir("docs"))
}
}
}3Verify
Run the same task twice. Both runs finish without a configuration cache problem report.
./gradlew assembleDebug -Dorg.gradle.configuration-cache=true
./gradlew assembleDebug -Dorg.gradle.configuration-cache=trueConfirmed by docs.gradle.org
Cause 2 of 3
A task field holds an object Gradle cannot serialize
1Check
The report names the field and the type. Types such as Project, Gradle, Settings, ClassLoader and Thread cannot be saved, and neither can references to Configuration or SourceSet inside task fields.
./gradlew assembleDebug -Dorg.gradle.configuration-cache=true --configuration-cache-problems=warn2Fix
Keep only simple values in task state: declare a typed input such as a Property<String> or ConfigurableFileCollection, or create the object inside the @TaskAction instead of storing it in a field.
abstract class Report : DefaultTask() {
@get:Input abstract val label: Property<String>
@get:InputFiles abstract val sources: ConfigurableFileCollection
}3Verify
Run the build with the default failing mode so no problem is hidden.
./gradlew assembleDebug -Dorg.gradle.configuration-cache=trueConfirmed by docs.gradle.org
Cause 3 of 3
A build listener runs on task graph or build completion
1Check
Callbacks such as BuildListener or Gradle.taskGraph.whenReady are not replayed when the cache is reused, so Gradle reports them as problems.
grep -rnE "buildFinished|taskGraph\.(afterTask|beforeTask|whenReady)" . --include=*.gradle --include=*.kts2Fix
Replace the listener with a build service that implements OperationCompletionListener and register it with BuildEventsListenerRegistry. If a third-party plugin does the listening, update that plugin.
// use a build service registered with
// BuildEventsListenerRegistry.onTaskCompletion(...)
// in place of gradle.taskGraph.afterTask { } / gradle.buildFinished { }3Verify
The report no longer lists the listener, and the build finishes without a problem report.
./gradlew assembleDebug -Dorg.gradle.configuration-cache=trueConfirmed by docs.gradle.org
How do I stop it coming back?
- Use FileSystemOperations, ExecOperations and typed inputs in every custom task from the start.
- Keep the default failing mode in CI. Warning mode is for migration, not a permanent setting.
- Update Gradle plugins when the report names one, since newer releases often fix their own cache problems.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Mobile apps
Gradle says deprecated features were used and the build will break on the next major version
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
This is a warning, not a failure.
3 causes, each with a fixMobile apps
adb cannot start because something already holds its port
adb start-server failed
The adb server must bind local TCP port 5037, and every adb client talks to it there.
2 causes, each with a fixMobile apps
The phone shows as unauthorized and Android Studio will not run the app
adb devices lists the phone but Android Studio cannot deploy to it
An unauthorized phone means it has not trusted this computer's RSA key.
3 causes, each with a fixMobile apps
Android Studio cannot pair the phone over Wi-Fi
The Pair devices over Wi-Fi dialog lists no device
Wireless pairing needs the phone on Android 11 or higher and on the same Wi-Fi network as the computer, with current Platform-Tools.
4 causes, each with a fixMobile apps
adb cannot connect to its daemon or start the server
* daemon started successfully *
The adb server is a background process on your computer that listens on TCP port 5037.
2 causes, each with a fixMobile apps
adb prints 'server version doesn't match this client; killing...'
adb server version doesn't match this client; killing...
adb runs a background server, and every adb command is a client.
3 causes, each with a fix