Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Android MultiDex?

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?

like image 916
Roberto Tellez Ibarra Avatar asked Nov 07 '15 22:11

Roberto Tellez Ibarra


People also ask

Is multidex needed?

Android 5.0 (API level 21) and higher uses ART which supports multidexing. Therefore, if your minSdkVersion is 21 or higher, the multidex support library is not needed.

What is a multidex application?

Getting past this limit requires that you configure your app build process to generate more than one DEX file, known as a multidex configuration. 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.

How do I enable multidex?

Android Multidex and the Dex Method Limit Enabling Multidex In order to enable a multidex configuration you need: to change your Gradle build configuration. to use a MultiDexApplication or enable the MultiDex in your Application class.

What is multidex support for Android?

Multidex support for Android 5.0 and higher. Android 5.0 (API level 21) and higher uses a runtime called ART which natively supports loading multiple DEX files from APK files. ART performs pre-compilation at app install time which scans for classesN.dex files and compiles them into a single .oat file for execution by the Android device.

How do I run multiple dex files on Android?

Android 5.0 (API level 21) and higher uses a runtime called ART which natively supports loading multiple DEX files from APK files. ART performs pre-compilation at app install time which scans for classesN.dex files and compiles them into a single .oat file for execution by the Android device.

How to add the multidex support library to your project?

To add the multidex support library to your project for API level lower than 21, add the following dependency in your build.gradle file. By adding this library, your app can manage the access of additional DEX files.

How does the build system work with multidex APIs?

The build system then packages all DEX files into your APK. At runtime, the multidex APIs use a special class loader to search all of the available DEX files for your methods (instead of searching only in the main classes.dex file).


4 Answers

Quoting the documentation:

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.

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:

Conversion to Dalvik format failed: Unable to execute dex: method ID not in [0, 0xffff]: 65536
like image 59
CommonsWare Avatar answered Oct 19 '22 18:10

CommonsWare


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

like image 33
Rohit Singh Avatar answered Oct 19 '22 18:10

Rohit Singh


Accroding Android developer official site.

If your minSdkVersion is set to 21 or higher, multidex is enabled by default and you do not need the multidex support library.
like image 27
Masum Avatar answered Oct 19 '22 20:10

Masum


When your app and the libraries it references exceed 65,536 methods, you encounter a build error that indicates your app has reached the limit of the Android build architecture

How to enable MultiDex in your project

Bulid.gradle

 defaultConfig {
    applicationId "******"
    minSdkVersion 21
    targetSdkVersion 30
    versionCode 8
    versionName "05.15.21.8"
    multiDexEnabled true

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}


    dependencies {
    implementation 'com.android.support:multidex:1.0.3'
}

inherit MultiDexApplication

    public class App extends MultiDexApplication {

    private static App instance;

    @Override
    public void onCreate() {
        MultiDex.install(this);
        super.onCreate();
        instance = this;
    }

    public static App getInstance ()
    {
        return instance;
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

In your Manifest

 <application
     android:name=".App"
 </application>

Thank You

like image 38
Kumar Santanu Avatar answered Oct 19 '22 20:10

Kumar Santanu