Here, we will try to learn how to enable android multidex ..
API level lower than 21: This is if your minSdkVersion is projected to lower than 21.
API level 21 & higher than that.
Kindly Note: Follow only one of the following based on the minSdkVersion in between two ⬇️
Android Multidex support for API level lower than 21
Android Multidex support for API level 21 and higher
For accomplish app code, versions of Android lower than Android 5.0 [API level 21] use the Dalvik runtime instead of java code & the limitation of using Dalvik is that ,
you unable to use more than one classes.dex bytecode file per APK.
To remove this limitation/restricson, you will have to add the Android Multidex support library.
To add the multidex support library to your android project for API level lower than 21, add the following dependency in your build.gradle file.
By adding upon this library, your android app can head the access of additional DEX files. In simple words,, if you are having more than 64K methods,, then you will be having more than 1 DEX file & these DEX files will be control by using this multidex support library..
Then, try to modify the module-level build.gradle file to enable multidex using multiDexEnabled true.
2. If you do override pass over the Application class,, change it to extend MultiDexApplication (if possible) as following:-
public class MyApplication extends MultiDexApplication { … }
3. Or if you do override passover the Application class but it’s not possible to change the base class, then you can instead override the attachBaseContext() method & call MultiDex.install(this) to enabling up multidex:
public class MyApplication extends SomeOtherApplication { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } }
Now , you done your job ..
That above was all here about the API level lower than 21. Now,, let’s jump over to the next method to implement..
Multidex support for API level 21 & higher
Your task will become much easier if you are making an App for Android version 5.0 (API level 21) or higher… API level 21 utilize a runtime called ART which carry loading multiple DEX files from APK files. So,, you doesn’t require to add the support library for multidex in Android 5.0 or higher.
Now, for API level 21 or higher,, you require to set multiDexEnabled to true in your module-level build.gradle file, as mentioned here :- ⬇️
2. If you do override the Application class, change it to extend MultiDexApplication (if possible) as follows:
public class MyApplication extends MultiDexApplication { … }
3. Or if you do overrule the Application class but it’s not feasible to change the base class, then you can instead override the attachBaseContext() method and call MultiDex.install(this) to enabling up multidex:
public class MyApplication extends SomeOtherApplication { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } }
Your this task is also done now..
So, now when you make your app, one primary DEX file will be constructed,, and supporting DEX files will also be added to it.. The primary DEX file is usually marked as classes.dex and other keep up DEX files as classes1.dex, classes2.dex,, and so forth..