How to Enable Multidex for Android App
The number of methods an Android app can have is limited to 65,536 (64 * 1024). Once the limit is crossed, the app builds fail with an error that says Cannot fit requested classes in a single dex file.
AGPBI: {"kind":"error","text":"Cannot fit requested classes in a single dex file (# methods: 76137 \u003e 65536)","sources":[{}],"tool":"D8"}
> Task :app:transformDexArchiveWithDexMergerForDebug FAILED
java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives:
The number of method references in a .dex file cannot exceed 64K.
To continue adding methods beyond the infamous 64k method limit, we need to enable multidex in our application. Let's get started.
1) Add the dependency to module level
build.gradle
file.implementation 'com.android.support:multidex:1.0.3'
2) In the same module level
build.gradle
file and inside the defaultConfig
block, set the multiDexEnabled
flag to true
.multiDexEnabled true
3) Now, we have three different options to complete the integration.
(ii) If our application class already extends some other application class like
(iii) This is the most commonly chosen option. As our applications already have an application class, all we need to do is extend our application class with
(i) If we don't want to create our own application class then simply add the
MultiDexApplication
class to the AndroidManifest.xmlandroid:name="android.support.multidex.MultiDexApplication"
(ii) If our application class already extends some other application class like
DaggerApplication
then add the MutiDex.install
to attachBaseContext
.@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
(iii) This is the most commonly chosen option. As our applications already have an application class, all we need to do is extend our application class with
MultiDexApplication
instead of Application.
Once added, the Dalvik Runtime (a default Runtime on the versions lower than API 21) or Android Runtime (a default Runtime on the API versions 21 and above) builds multiple dex files in the sequence of classes.dex classes2.dex, classes3.dex .... The Multidex library stays as a part of the primary dex and manages the access to the other dex files.
The downside of the enabling Multidex in our application is that the build times will become slower. The applications can delay hitting the 64k limit by
The downside of the enabling Multidex in our application is that the build times will become slower. The applications can delay hitting the 64k limit by
1. Keeping a tap on the number of methods, the third party libraries added to the once project
2. Removing unused dependencies.
3. Avoiding the addition of group dependencies.
Disclaimer: In case the
2. Removing unused dependencies.
3. Avoiding the addition of group dependencies.
Disclaimer: In case the
minSdkVersion
is greater than or equal to 21 then no need to add the multidex library as the Android Runtime (ART) automatically splits the dex files once they pass the 64k limit.