androidgradleandroid-multidex

How to enable multidexing with the new Android Multidex support library


I want to use the new Multidex support library to break the method limit for one of my apps.

With Android Lollipop Google introduced a multidex support library that makes it easy to multidex.

What steps are needed to use this library and to build my app with multidex support?


Solution

  • Edit:

    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.


    Modify your build.gradle:

    android {
        compileSdkVersion 22
        buildToolsVersion "23.0.0"
    
             defaultConfig {
                 minSdkVersion 14 //lower than 14 doesn't support multidex
                 targetSdkVersion 22
    
                 // Enabling multidex support.
                 multiDexEnabled true
             }
    }
    
    dependencies {
        implementation 'com.android.support:multidex:1.0.3'
    }
    

    If you are running unit tests, you will want to include this in your Application class:

    public class YouApplication extends Application {
    
        @Override
        protected void attachBaseContext(Context base) {
            super.attachBaseContext(base);
            MultiDex.install(this);
        }
    
    }
    

    Or just make your application class extend MultiDexApplication

    public class Application extends MultiDexApplication {
    
    }
    

    For more info, this is a good guide.