What does “multiDexEnabled true” mean?

2019-01-16 23:52发布

问题:

What is meant by "multiDexEnabled true" in Android gradle. Why do we use this? What is the effect if it is enabled?

回答1:

Android application (APK) files contain executable bytecode files in the form of Dalvik Executable (DEX) files, which contain the compiled code used to run your app. The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536, including Android framework methods, library methods, and methods in your own code. Getting past this limit requires that you configure your app build process to generate more than one DEX file, known as a multidex configuration.

You should read official guide line about Building Apps with Over 64K Methods



回答2:

Android applications by default have SingleDex support which limits your application to have only 65536 methods(references). So multidexEnabled = true simply means that now you can write more than 65536 methods(references) in your application.

But I will never write 65536 methods!

When we say the number of methods, it means

methods written by you + Android Framework methods + Third party library (eg Volley, Retrofit, Facebook SDK etc) methods.

I have read somewhere in a post that
App Compat 24.2.1 contains 16.5k methods
Google Play Services GCM 9.6.1 contains 16.7k methods.
So if you have just written a simple Hello world application which has App Compat 24.2.1 then your application is already having 16.7k methods.

How to enable multidex support

it depends on minSdkVersion of your app

If minSdkVersion >= 21 then you can enable it by writing multidexEnabled = true
if minSdkVersion <21 then you will have to include Multidex Compatibily library in your gradle.
See more on enabling multidex support

Advantage of multiDex

multidex allows your applications to have more third-party libraries.

More on .dex files

Android applications are compiled into a .dex file/files which in turn zipped to a single .apk file. .dex files have bytecodes which are used by Dalvik Virtual Machine(DVM).
You can read more on .dex and DVM



回答3:

It allows you to build apps with over 64k methods. For more information see here http://developer.android.com/intl/es/tools/building/multidex.html



回答4:

multidex support third party library to your application

If minSdkVersion >= 21 then you can enable it by writing multidexEnabled = true if minSdkVersion <21 then you will have to include Multidex Compatibily library in your gradle. See more on enabling multidex support