ANDROID • GRADLE BUILD SYSTEM

GRADLE BUILD FAILED & MANIFEST MERGER ERROR

UnityEditor.BuildPlayerWindow+BuildMethodException: Gradle build failed. See the Console for details.

What Is the Error?

Gradle is the build automation tool used by Android to resolve third-party plugins (Google Play Services, AdMob, In-App Purchasing, Firebase), merge XML manifests, and assemble the final .aab or .apk package.

When two plugins import conflicting versions of the same Android library (e.g. androidx.core), or when multiple AndroidManifest.xml files declare conflicting android:theme attributes, the Gradle merger halts the build with an error.

Step-by-Step Resolution

  1. Enable Custom Base Gradle Template:

    Go to Project Settings > Player > Android > Publishing Settings. Under Build, check:
    • Custom Main Gradle Template (mainTemplate.gradle)
    • Custom Gradle Properties Template (gradleTemplate.properties)

  2. Enable AndroidX & Jetifier in gradleTemplate.properties:

    Open Assets/Plugins/Android/gradleTemplate.properties and add the following flags to automatically migrate legacy support libraries:

    android.useAndroidX=true
    android.enableJetifier=true
  3. Run External Dependency Manager (EDM4U) Force Resolve:

    If using Google Mobile Ads or Firebase, go to Assets > External Dependency Manager > Android Resolver > Force Resolve. This deduplicates overlapping .aar dependencies.

  4. Enable Minify (ProGuard / R8) with Custom Keep Rules:

    If the dex limit (64k methods) is exceeded, enable Custom Proguard File in Publishing Settings.

Related Troubleshooting Guides

Frequently Asked Questions

This typically occurs when signing the release APK with an invalid Keystore password, or when Windows file path length exceeds 260 characters (MAX_PATH). Move your Unity project to a short directory path like D:/Projects/Game/ to avoid Gradle path truncation bugs.

Fixing Android Gradle JVM Out of Memory & NDK Compatibility

Gradle compilation failures during Android APK/AAB builds in Unity typically trace to three root causes: Java Heap OOM errors, conflicting AndroidX support libraries, or incompatible Android NDK/JDK versions.

Increasing Gradle Daemon Memory in gradle.properties

Open Project Settings → Player → Publishing Settings. Check Custom Gradle Properties Template. Locate Assets/Plugins/Android/gradle.properties and add:

org.gradle.jvmargs=-Xmx4096M -XX:MaxMetaspaceSize=1024m

Resolving Duplicate Class and Manifest Merger Collisions

When importing multiple monetization SDKs (like AdMob, Firebase, and AppLovin), conflicting Android library versions trigger duplicate class errors. Enable the External Dependency Manager for Unity (EDM4U) and run Assets → External Dependency Manager → Android Resolver → Force Resolve.