TechBuzzPro

Android Gradle Build Errors: How to Fix Them

· 6 min read · Phones & Tablets

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Most Android Gradle build errors come down to one of a few causes: a version mismatch between the Android Gradle Plugin (AGP), Gradle and the JDK; a stale or corrupted cache; missing SDK components; or a dependency that cannot be resolved. The fastest fix is to read the first error in the Build output, not the last, and match it to one of those causes. The steps below walk through the reliable order: read the error, check versions, clean and rebuild, then repair the environment.

What You'll Need

  • Android Studio installed and updated to a current stable version.
  • The project open, with access to its build.gradle / build.gradle.kts files and gradle-wrapper.properties.
  • A working internet connection for dependency downloads.
  • Command-line access to the project folder (Android Studio's Terminal tab is fine).
  • Your JDK version noted — Android Studio bundles one, and that bundled JDK is usually the safest choice.

Step-by-Step

  1. Read the first error, not the last. Open the Build tool window and scroll to the top of the failure. Gradle often prints a cascade of follow-on errors; the first one is usually the real cause. Look for the task that failed and the exception type — for example Unsupported class file major version (JDK mismatch), Could not resolve (dependency or repository problem), or Manifest merger failed (conflicting manifest entries).
  2. Sync the project first. Click Sync Project with Gradle Files in Android Studio (the elephant icon in the toolbar). Many errors are just an out-of-date sync after a dependency or plugin change. If sync fails, the error message there is more specific than a build failure.
  3. Check your version alignment. Open gradle/wrapper/gradle-wrapper.properties and note the distributionUrl Gradle version. Then open the project-level build.gradle and note the AGP version in the com.android.tools.build:gradle plugin line. These two must be compatible — each AGP release supports a specific Gradle range. If you recently upgraded one, upgrade the other to match, or roll back.
  4. Confirm the JDK. In Android Studio, open Settings (or Preferences on macOS) and find the Gradle section under Build, Execution, Deployment. Check which JDK is selected for Gradle. The bundled JDK is the safest option. A mismatch here produces errors like Unsupported class file major version 65 or invalid source release.
  5. Clean and rebuild. Use Build > Clean Project, then Build > Rebuild Project. If that does not help, delete the build folders in the project root and in each module, then rebuild. This clears stale compiled output that can survive a normal clean.
  6. Clear the Gradle cache if dependencies look broken. In the Terminal tab, run ./gradlew --stop (use gradlew.bat --stop on Windows), then delete the .gradle folder inside your project and the global Gradle cache folder (~/.gradle/caches on macOS/Linux, %USERPROFILE%\.gradle\caches on Windows). Re-sync and rebuild. This forces fresh downloads and fixes corrupted cached artifacts.
  7. Verify SDK components. Open the SDK Manager and confirm the build tools version, platform version and any required NDK or CMake are installed. A missing platform or build-tools version causes errors like Failed to find target with hash string or Installed Build Tools revision ... is corrupted.
  8. Check repository declarations. In settings.gradle (or the project-level build.gradle in older setups), confirm google() and mavenCentral() are listed in both pluginManagement and dependencyResolutionManagement repositories. A missing google() is a common cause of Could not resolve com.android.tools.build:gradle.
  9. Resolve manifest and resource conflicts. For Manifest merger failed, read the reported attribute and either add a tools:replace rule or remove the duplicate entry. For resource errors, check for duplicate file names across modules and for missing values entries.
  10. Update Android Studio and plugins. Use Help > Check for Updates (or the equivalent on your OS) and update the Android Studio version, then re-sync. Outdated IDE versions can ship Gradle tooling that no longer matches your project.

Common Mistakes

  • Chasing the last error. The final line is often a generic "build failed" message; the first error carries the cause.
  • Upgrading AGP without Gradle. These are a matched pair. Changing one alone reliably breaks the build.
  • Deleting the whole project cache blindly. Clearing caches is safe, but clearing the wrong folder (for example your source or local.properties) wastes time and can break SDK paths.
  • Ignoring the JDK. Many "unsupported class file" and "invalid source release" errors are a JDK setting, not a code problem.
  • Editing dependency versions to silence errors. Downgrading a library to make a message disappear can hide a real incompatibility; fix the version alignment instead.

Tips

  • Run ./gradlew build --stacktrace (or --info) in the Terminal for a fuller trace than the Build window shows.
  • Keep a note of the exact AGP, Gradle and JDK versions that last built successfully — that is your known-good baseline.
  • Prefer the bundled JDK for Gradle unless you have a specific reason to use another.
  • After any dependency change, sync before building; it catches resolution problems earlier and with clearer messages.
  • Commit gradle-wrapper.properties and the wrapper JAR so every machine uses the same Gradle version.

Troubleshooting

If the build still fails after the steps above, isolate the problem. Create a new empty project in Android Studio and build it. If the empty project builds, the issue is in your project's configuration or dependencies; if it also fails, the problem is your environment (JDK, SDK, or Android Studio installation).

For dependency errors, run ./gradlew dependencies to see the resolved graph and spot conflicting versions. For version errors, compare your AGP and Gradle versions against the compatibility table in the official Android Gradle Plugin release notes. For cache problems that persist, stop the Gradle daemon, delete the project .gradle folder and the global cache, then re-sync. If Android Studio itself behaves oddly, use File > Invalidate Caches / Restart — this clears IDE caches only and does not touch your source code.

As a last resort, back up your source, then re-import the project: close it, delete the .idea folder and .gradle folder, and reopen the project so Android Studio regenerates its configuration. Your code and Gradle files are preserved; only IDE metadata is rebuilt.

Frequently Asked Questions

Why does my build fail only on a clean machine?

Usually a missing SDK component, a different JDK, or a dependency that is not declared in a repository both machines can reach. Compare the SDK Manager and JDK settings between the machines.

What does "Unsupported class file major version" mean?

Gradle is running on a newer JDK than the one your AGP version supports, or vice versa. Set Gradle to use the bundled JDK or a version compatible with your AGP.

Is it safe to delete the .gradle folder?

Yes. It contains caches and generated metadata, not source code. Gradle recreates it on the next sync or build.

Why does sync succeed but the build fail?

Sync checks configuration and dependency resolution; the build also compiles code and merges resources and manifests. A build-only failure usually points to code, resource or manifest issues.

Do I need to match AGP and Gradle versions exactly?

Not exactly, but they must be within the supported range for that AGP release. Check the official compatibility table before upgrading either one.

Should I use the command line or Android Studio to build?

Either works. The command line with --stacktrace often gives more detail, while Android Studio's Build window is easier to scan for the first error.