error while merging dex archieve
enable multidex android

Hi Guys, Welcome to Proto Coders Point, In this tutorial, we will checkout & solve the common error that “error while merging dex archives”.

Video Tutorial to solve the same

The error says: DexArchiveMergerException

Cannot fit requested classes in a single file: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives:

so before looking into how to solve multi dex error, let’s learn basic about DEX(Dalvik Executable).

What is the dex file? Understanding Multidex in android

In Java, your code is converted from .java to .class file during compile time, so that our machine can understand the .class file but in android, the java code is compiled into DEX(Dalvik Executable) file so that our Android machine can understand it.

This article is for android/flutter developers who face DEX error “cannot fit requested classes in a single file: error while merging dex archieve“.

 error while merging dex archieve
error while merging dex archieve

The error itself says you, why you are facing “multi Dex error”, The number of methods references in a .dex file cannot exceed 64K (# 68563 > 65536).

A Single DEX file limits the number of methods referenced, A single DEX file is limited to 64K i.e. 65536, which simply means that you cannot create more than 65536 methods in a single DEX file(“cannot fit requested classes in a single file“).

usually, this DEX error occurs when a developer adds external android/flutter libraries,

So, sometimes by adding external libraries to your android/flutter projects, if your project merged app exceeds 65536 methods, than you will encounter a build error as mentioned above “error while merging dex archieve“.

Ways to enable multidex support in android studio project

Solution 1: Clean project and invalidate caches & restart

Once you add any external libraries in your android studio project always keep habit to clean project and invalidate cache restart.

Step 1: Menubar -> Build -> Clean Project then Rebuild project.

Step 2: Menubar -> Invalidate Caches/Restart.

Solution 2: Enable multidex for app over 64K methods

To enable multidex, if you are not using AndroidX, you need to add deprecated library in dependency

dependencies {
    implementation "com.android.support.multidex: 1.0.3 " //add this
    
}

By adding above multidex it will become a primary DEX file of your app.

if your minSdkVersion is set to 21 or higher, The Multidex is been enabled by default while creating new project, if not,

then do the followng steps:-

Step 1: modify Module level app/build.gradle

android {
    defaultConfig {
        applicationId "com.example.flutter_app_simple"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true //add this line
    }
}

dependencies {
    implementation "com.android.support.multidex: 1.0.3"  // add this line

}

Step 2: Add multidex in manifest <application> tag

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">
    <application
            android:name="androidx.multidex.MultiDexApplication" >  <!-- add this line -->
        ...
    </application>
</manifest>