Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Gradle cannot store the configuration cache for an Android build

ErrorConfiguration cache state could not be cached

Short 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.

  • Applies to Gradle 8.14, Gradle 9, Android Gradle Plugin 8.x, Android Studio current stable
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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=true
  2. 2Match what you see, then jump to its fix

    1. 1invocation of 'Task.project' at execution time is unsupported with the configuration cache.A task action uses Project at execution timeFix
    2. 2field 'x' of task ... cannot serialize object of type ...A task field holds an object Gradle cannot serializeFix
    3. 3The 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

What does the error look like?

  • Configuration cache state could not be cached
  • invocation of 'Task.project' at execution time is unsupported with the configuration cache.
  • cannot serialize object of type
  • Configuration 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/null

2Fix

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=true

Confirmed by docs.gradle.org

Did this fix it?

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=warn

2Fix

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=true

Confirmed by docs.gradle.org

Did this fix it?

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=*.kts

2Fix

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=true

Confirmed by docs.gradle.org

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.