Skip to content
Latchkey

Gradle "Circular dependency between ... tasks" - Fix Task Cycle in CI

Gradle builds a directed acyclic graph of tasks. This error means your wiring created a cycle - task A depends on B which (directly or transitively) depends back on A - so there is no valid execution order and Gradle refuses to run.

What this error means

The build fails before executing tasks with Circular dependency between the following tasks: followed by the loop, drawn with \--- arrows showing each edge in the cycle.

gradle output
FAILURE: Build failed with an exception.
* What went wrong:
Circular dependency between the following tasks:
:app:compileJava
\--- :app:generateSources
     \--- :app:compileJava (*)

Common causes

Mutually dependent tasks

An explicit dependsOn (or mustRunAfter/input-output wiring) makes two tasks require each other. Often a codegen task is wired to depend on compile while compile depends on the generated sources.

A task consuming its own output as an input

A task whose declared inputs include an output another task derives from this same task creates a transitive loop in the graph.

How to fix it

Break the cycle with a one-directional dependency

Make codegen run before compile and have compile depend on the generated sources - not the reverse.

build.gradle.kts
val generateSources by tasks.registering { /* writes generated/ */ }
tasks.named("compileJava") {
    dependsOn(generateSources)   // compile -> generate, one direction only
}
// remove any generateSources.dependsOn("compileJava")

Visualize the graph to find the back-edge

Use a task-tree view to see exactly which edge closes the loop, then remove it.

Terminal
./gradlew :app:compileJava --dry-run
# or with a task-tree plugin:
# ./gradlew :app:compileJava taskTree

How to prevent it

  • Wire task dependencies in one direction; let consumers depend on producers, never both ways.
  • Use input/output wiring so Gradle infers ordering instead of manual dependsOn loops.
  • Visualize the task graph after adding cross-task dependencies.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →