Skip to content

Enable Multidex For Apps With Gradle

  • by

If your app’s minSdkVersion is set to 21 or higher, then multidex is enabled by default and you don’t have to do anything to enable, you can skip this tutorial.

No, if your minSdkVersion is set to 20 or lower, then you must use the multidex support library and read this article to know how to enable multidexing capability to your application.

This guide is writing for those who get 64K method limit exceeded error while building APK. Normally app is compiled into DEX, Dalvik Executable, file inside APK(a zip format). For a dex file it has methods compiled it and there is limit of 64k (exactly 65,536—including Android framework methods) methods per dex file. If this limit exceeds then you get build error.

It gets often when you use large number of dependencies. for example pay services, image loading libraries , HTTP request libraries , json encode and decode libraries etc. These libraries are very much required for applications these days. It helps us saving time by simply adding it into dependency section in the gradle file.

MultiDex means splitting dex into multiple dex file so each has less than 64K methods.

Enable MultiDex In Gradle

Enable multidexing in main module (by default it is app). Also add support library for multidexing in dependency section if your minSdkVersion is lower than 21.

Open app/build.gradle and make the following 2 changes

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"

    defaultConfig {
        ...
        minSdkVersion 14
        targetSdkVersion 21
        ...

        // Change 1: Enabling multidex support.
        multiDexEnabled true
    }
    ...
}


//Change 2: If your minSdkVersion is lower than 21 then add below dependency
dependencies {
   implementation 'com.android.support:multidex:1.0.3'
  .....
  .....
}

multiDexEnabled true
this line enables multidex for your application.

Create MultiDexApplication Class

If your app has your own Application Class then extend Application class from MultiDexApplication instead of Application.

Example :

public class YouApplication extends MultiDexApplication {

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

}

Finally Link It To Application

<!--?xml version="1.0" encoding="utf-8"?-->

    
        ...
    


Official Document

After all the above steps completed now you are ready to build and run your application.

Leave a Reply

Your email address will not be published. Required fields are marked *