Multidex in Android - GeeksforGeeks

19 Sep, 2021
Now as we are aware of Methods/Functions in android we know that every library has a large number of pre-built methods. In many, we have to use a lot of libraries according to the needs of our project which obviously increases the total number of methods in our android app. After adding multiple libraries an error occurs saying that ... 
cannot fit requested classes in a single dex file 99876 > 65536
So before understanding this error we have to understand 
How android build system works?
What is a DEX?
What is Multidexing?
How to achieve Multidexing?
How Multidexing actually works?
We make our app using Activities, Layouts, resource files, dependencies. All these get into the compiler and get converted into a single DEX (Dalvik executable) which is compiled resource ready for APK packing. Then it gets converted into an .APK file. Take a look at the below image for a clear understanding, here resource files mean the additional files we add to the project like images, videos, svg files.
Now we know the source code is converted into a DEX (Dalvik Executable) file which can run as an Android App and can only be understood by a computer. Most importantly a single DEX file can only contain 65536 numbers of methods. If your project is exceeding the number of methods, the error mentioned above occurs and therefore the use of multidexing becomes a must.
If the number of methods in our project is less than or equal to 65536 then the app code will be easily converted into a DEX file. But if the number of methods is greater than 65536 then we have to allow our app to create more than 1 DEX file and hence calling it “Multidexing”. 
Step by step Implementation
Step 1: Create new project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. You can choose any language Java/Kotlin.
Step 2: Add Dependencies
If you are using an Android API version below 21 add the following dependency to your build.gradle file.
implementation 'com.android.support:multidex:1.0.3'
Step 3: Working with build.gradle(module) file
Ignore Step 1 if you are using API version 21 or above. Most probably you are maybe using above 21. Go to build.gradle file, inside that you will find the scope { } of defaultConfig then inside that scope just add multiDexEnabled = true as shown below then click sync now.
multiDexEnabled = true
Until now we saw if the number of methods is greater than 65536 we have to use more than 1 DEX file. Once you enable multidexing and if a single DEX file crosses its limit then a Primary DEX file will be created and is followed by Supporting DEX files. The Supporting DEX files will only be created if the limit is crossed in the Primary DEX file. 
Whenever you get errors listed below or related to dex/multidex , you can try the above steps, It will help to solve the problem
Too many field references: 131000; max is 65536.
You may try using --multi-dex option.
Conversion to Dalvik format failed:
Unable to execute dex: method ID not in [0, 0xffff]: 65536
cannot fit requested classes in a single dex file 99876 > 65536
Picked
Android
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
How to Post Data to API using Retrofit in Android?
Android Listview in Java with Example
Retrofit with Kotlin Coroutine in Android
How to Add Image to Drawable Folder in Android Studio?
How to Change the Background Color After Clicking the Button in Android?
How to Retrieve Data from the Firebase Realtime Database in Android?
GridView in Android with Example
ImageView in Android with Example