Skip to content

How to Fix Gradle Build Failed Errors

DodaTech 2 min read

In this tutorial, you'll learn about How to Fix Gradle Build Failed Errors. We cover key concepts, practical examples, and best practices.

The Problem

You run ./gradlew build and get a generic error:

BUILD FAILED in 3s

Without details about what went wrong. Gradle suppresses the full error output by default, making debugging harder.

Quick Fix

Step 1: Run with stack trace

./gradlew build --stacktrace

The --stacktrace flag prints the full exception trace, showing exactly which task failed and why.

Step 2: Run with full error details

./gradlew build --info

The --info flag increases log verbosity, showing task execution order, dependency resolution, and incremental build decisions.

Step 3: Clean and rebuild

./gradlew clean build

The clean task deletes the build/ directory, removing any corrupted or stale compiled output. This fixes most build failures caused by partial rebuilds.

Step 4: Clear the Gradle cache

rm -rf ~/.gradle/caches/
./gradlew build

Removing the cache forces Gradle to re-download all dependencies. Use this when the error is related to corrupted cached artifacts.

Step 5: Check Java version compatibility

./gradlew --version

Expected:

Gradle 8.5
JVM: 17.0.10 (OpenJDK 64-Bit Server VM)

Ensure the JVM version meets the Gradle version's requirements. Gradle 8.x requires Java 17+.

Step 6: Run a specific task with scan

./gradlew build --scan

The --scan flag generates a build scan URL that shows every task duration, dependency, and error in a web dashboard.

Step 7: Check for dependency conflicts

./gradlew dependencies --configuration compileClasspath

This prints the full dependency tree. Look for version conflicts or excluded transitive dependencies.

Step 8: Fix Gradle wrapper version

./gradlew wrapper --gradle-version 8.5

If the wrapper version is outdated, update it. Mismatched wrapper and plugin versions are a common source of build failures.

Step 9: Check for memory issues

export GRADLE_OPTS="-Xmx2g -XX:MaxMetaspaceSize=512m"
./gradlew build

If the build fails with an OutOfMemoryError, increase the JVM heap size available to Gradle.

Alternative Solutions

Use ./gradlew build --offline to test if the build succeeds without network access. If it fails offline but succeeds online, the issue is likely a flaky dependency server.

Common Errors

OutOfMemoryError during build: Increase Gradle's heap: export GRADLE_OPTS="-Xmx2g" or set org.gradle.jvmargs=-Xmx2g in gradle.properties.

Plugin not found: If a plugin like spring-boot is missing, add it to plugins block in build.gradle or ensure it is in the buildscript dependencies.

Deprecated Gradle features: Warnings about deprecated features may be upgraded to errors in the next Gradle version. Run ./gradlew build --warning-mode all to identify them.

Daemon process stuck: If Gradle hangs, kill the daemon: ./gradlew --stop and retry. If that fails, pkill -f GradleDaemon and restart.

Prevention

  • Pin dependency versions in build.gradle to avoid unexpected upgrades.
  • Run ./gradlew clean test before every commit to catch failures early.
  • Use a Gradle wrapper committed to the repository for reproducible builds.
  • Set up CI caching for ~/.gradle/caches/ to speed up builds.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro