How to Fix "Gradle Build Failed With an Exception" in React Native Android
Quick answer
Building your React Native app for Android fails with Gradle's generic "build failed with an exception" summary, which — much like a similar summary from other...
Building your React Native app for Android fails with Gradle's generic "build failed with an exception" summary, which — much like a similar summary from other build tools — doesn't tell you what actually went wrong on its own. The real cause is always further up in the same output, and Gradle offers specific flags to surface it more clearly.
The Problem
The build fails with a summary that doesn't explain the actual cause:
$ npx react-native run-android
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:mergeDebugResources'.
> A failure occurred while executing com.android.build.gradle.tasks.MergeResources$MergeResourcesRunnable
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
Gradle explicitly tells you which flags will get you more detail — it's worth actually using them rather than guessing based on the summary alone.
Why It Happens
Gradle's default output deliberately keeps the top-level summary short, trusting you to opt into more verbose output when you actually need to debug a failure. The underlying cause behind this generic message varies enormously:
- A native dependency conflict — two libraries requiring incompatible versions of the same underlying Android dependency.
- Stale Gradle or build cache state — a previous build left inconsistent cached artifacts that conflict with the current attempt, especially common after switching branches or updating dependencies.
- A missing or misconfigured Android SDK component — a required build tools version, platform, or NDK version isn't installed.
- Duplicate resource definitions — two different libraries or modules define a resource (a string, a drawable, a color) with the same name, which the specific "mergeDebugResources" task shown above is especially likely to surface.
- An out-of-memory failure inside the Gradle daemon itself during a resource-intensive build step, especially on machines with limited RAM.
The Fix
Always start by getting the actual stack trace, since the default summary genuinely doesn't contain enough information to diagnose the problem:
cd android
./gradlew assembleDebug --stacktrace
For even more detail if the stack trace alone isn't clear enough:
./gradlew assembleDebug --info
For the common "stale build state" cause, clear Gradle's caches and retry with a clean build before investigating further, since this alone resolves a large fraction of these failures without needing deeper debugging:
cd android
./gradlew clean
cd ..
npx react-native run-android
If the stack trace reveals a duplicate resource conflict, it typically names the exact resource and both source locations:
ERROR: [string/app_name] AndroidManifest.xml, /myapp/android/app/src/main/res/values/strings.xml:3
also found in /myapp/node_modules/some-library/android/src/main/res/values/strings.xml:2
Rename or remove the conflicting resource in your own project's files, since you generally can't modify a third-party library's resources directly:
<!-- android/app/src/main/res/values/strings.xml -->
<string name="app_name">My Actual App Name</string>
<!-- rename to avoid colliding with the library's own "app_name" resource -->
If the failure is an out-of-memory error inside Gradle itself, increase the memory allocated to the Gradle daemon in gradle.properties:
# android/gradle.properties
org.gradle.jvmargs=-Xmx4096m -XX:MaxMetaspaceSize=512m
For dependency version conflicts, check the full dependency tree to identify exactly which two libraries are pulling in incompatible versions of a shared dependency:
cd android
./gradlew app:dependencies --configuration debugRuntimeClasspath > deps.txt
Search the output for the specific conflicting library named in your error to see the full chain of what's requiring which version, then align them explicitly in your build.gradle if needed:
// android/app/build.gradle
configurations.all {
resolutionStrategy {
force 'com.facebook.react:react-android:0.72.0'
}
}
Still Not Working?
If clearing caches and reviewing the stack trace doesn't reveal an obvious fix, verify your installed Android SDK components actually match what the project expects — an outdated or missing build tools version, compile SDK version, or NDK is a common source of obscure failures that don't clearly announce themselves as version problems:
cat android/build.gradle | grep -A 5 "buildscript"
Cross-check the versions listed there (compileSdkVersion, buildToolsVersion) against what's actually installed via Android Studio's SDK Manager, and install anything missing. Running the community CLI's diagnostic tool is often the fastest way to spot a mismatch without manually cross-referencing every version number by hand:
npx @react-native-community/cli doctor