There are many posts about MultiDex. I have experienced, sometimes, errors solved including multiDexEnabled true
in the defaultConfig
section of my build.gradle.
But, what exactly is this feature? What are the scenarios for using it?
There are many posts about MultiDex. I have experienced, sometimes, errors solved including multiDexEnabled true
in the defaultConfig
section of my build.gradle.
But, what exactly is this feature? What are the scenarios for using it?
Quoting the documentation:
So, the feature is: it allows your complex app to compile. The scenarios for using it are when your app fails to compile due to hitting the 64K DEX method reference limit. This appears as a build error, such as:
It's as simple as this
A single .dex file can have 65,536 methods(references) so if the number of references exceeds 65,536, you go with multidex.
More Explanation!
An android application program is compiled into a .dex file which in turn zipped to a single .apk file.
DVM (Dalvik Virtual Machine) uses .dex file/files to execute bytecodes.
What causes the number of references to exceed 65,536 limits?
Methods written by you + Android Framework methods + Third party library(eg Volley,Retrofit, Facebook SDK etc) methods.
I have read "somewhere"
App Compat 24.2.1 contains 16.5k methods
Google Play Services GCM 9.6.1 contains 16.7k methods
So if you have created a simple Hello world application which has App Compat 24.2.1, you are already 1/4 way to cross the single dex methods limit
What is MultiDex in Android?
Dex stands for Dalvik Executable, which is what Google's virtual machine processor (Dalvik) uses to handle Android Applications. Android was built with small and simple apps in mind and the constraints on one single Dalvik Executable pinned the roof of code references at 65,536 methods. Because of this issue and the way the Dalvik machine handles code execution there were some compiling and invocation issues, until the Monkey Patch or MultiDex integration. MultiDex integration in Android Studio allows Android Developers the ability to compile and execute a code-base with over 65,536 methods!
What it also allows is, for google (and others) to apply DRM, say.. widevine, in pieces of code where it causes problems for most non-developers..