Gradle "Execution failed for task" - Diagnose and Fix in CI
Execution failed for task is Gradle’s generic failure wrapper. The actual error is always in the What went wrong block beneath it - a compile error, a failing test, or a process that exited non-zero.
What this error means
The build stops with Execution failed for task ':module:taskName' followed by a Caused by/What went wrong section. The task name tells you the phase; the detail below tells you the cause.
> Task :app:test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:test'.
> There were failing tests. See the report at: file://.../build/reports/tests/test/index.htmlCommon causes
A downstream task failed
The wrapper names the task; the cause is task-specific - :compileJava means a compile error, :test means failing tests, an Exec task means a non-zero exit from a spawned process.
A plugin or script threw during execution
Custom build logic or a plugin can throw at execution time. The Caused by stack trace points at the offending code.
How to fix it
Read the What-went-wrong block and get a stacktrace
Re-run with --stacktrace (or --info) so the real exception and its origin are visible.
./gradlew :app:test --stacktrace --infoOpen the task-specific report
- For
:test, openbuild/reports/tests/test/index.htmlfor the failing assertions. - For
:compileJava, fix the compile error shown above the wrapper. - For an
Exectask, check the exit code and the command’s own stderr.
How to prevent it
- Always run CI Gradle with
--stacktraceso failures are diagnosable from logs. - Upload Gradle reports as CI artifacts for post-mortem.
- Keep custom build logic in tested, well-scoped plugins rather than inline scripts.