168

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?

Snicolas
  • 37,840
  • 15
  • 114
  • 173
Janusz
  • 187,060
  • 113
  • 301
  • 369

15 Answers15

360

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.

Chad Bingham
  • 32,650
  • 19
  • 86
  • 115
  • 2
    If you are using the new builds tools yet this is definitely the way to go. – Janusz Dec 04 '14 at 08:36
  • So, what if I was using the same method described below here from Janusz and all was working fine, and then I updated to android studio 1.0 (and so gradle 1.0) and I'm not able to get it working in any way. Nor this one described by you, that looks like the "official one" for google too.. I still get "failed to run command --multi-dex". Anyone have the same problem? – Marino Dec 09 '14 at 01:53
  • 1
    @jem88 You updated your gradle to 1.0?? You should be on 2.2.+ (Take a look in your `gradle-wrapper.properties`) The key points: **1** your buildToolsVersion needs to be 21.1.1 or higher. **2** add `multiDexEnabled true` to your defaultConfig of your build.gradle (notice there is not a `=`) **3** add the `'com.android.support:multidex:1.0.0'` – Chad Bingham Dec 09 '14 at 02:00
  • @Binghammer Sorry for the mistake, I mean gradle 2.2.1. I've that, I'm using build tools 21.1.1, I've multiDexEnabled true in default config and I've multidex:1.0.0. And all the other things are exactly in the same way I had before the update, when it was working fine. I'm on this from couple of hours now, can't figure this out! – Marino Dec 09 '14 at 02:39
  • 4
    How about if we don't use application class? Is adding `android:name="android.support.multidex.MultiDexApplication"` to `application` tag in `AndroidManifest.xml` sufficient? – Buddy Sep 03 '15 at 15:55
  • Do you not need to add it to the release section as well? – Zapnologica Nov 05 '15 at 19:31
  • @Zapanologica If it is in your 'defaultConfig' you should be good. – Chad Bingham Nov 05 '15 at 19:44
  • This is how it fixed the Multi-Dex crash on my app, thanks a lot, please refer the link http://developer.android.com/tools/building/multidex.html – Anuj Jan 11 '16 at 08:01
  • 1
    compile 'com.android.support:multidex:1.0.1' is not needed in 23 – Naveed Ahmad May 13 '16 at 07:19
  • perfect answer!. its worked for me after including multidex enabled true, compile multidex dependancy and add install multidex code in my Application class. its solved my big issue. Thanks @Chad Bingham – Harrish Android Jul 06 '16 at 07:51
  • 3
    @NaveedAhmad, according to the google developer docs: `..if your minSdkVersion is set to 20 or lower you must use...` `'compile 'com.android.support:multidex:1.0.1'`. – Sakiboy Mar 03 '17 at 06:54
  • based on documentation, compile 'com.android.support:multidex:1.0.1' is required if your minSdkVersion is set to 20. https://developer.android.com/studio/build/multidex.html – Juan Mendez Nov 16 '17 at 16:53
  • It seems that multidex library is in the maven repository. So check if it's also added in the build.gradle config. Like: repositories { ... mavenCentral() ... } and buildscript { repositories { mavenCentral() } ... } It's helped me – Sergio Jan 30 '18 at 09:29
  • Multydex has new version 1.0.3 – Vova Mar 16 '18 at 11:25
  • For APIs < 21 you might need to extend `MultiDexApplication` instead of `Application` as well. – ratsimihah May 18 '18 at 11:11
  • @ratsimihah There is no requirement to extend that class. It is just a helper. – Chad Bingham May 18 '18 at 16:01
  • To be clear, for minSdkVersion 21+, not only the multidex support library but also any configuration of multidex such as "multiDexEnabled true" is unnecessary. – Tatsuya Fujisaki Jul 21 '20 at 03:39
47

The following steps are needed to start multi dexing:

Add android-support-multidex.jar to your project. The jar can be found in your Android SDK folder /sdk/extras/android/support/multidex/library/libs

Now you either let your apps application class extend MultiDexApplication

public class MyApplication extends MultiDexApplication

or you override attachBaseContext like this:

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

I used the override approach because that does not mess with the class hierarchy of your application class.

Now your app is ready to use multi dex. The next step is to convince gradle to build a multi dexed apk. The build tools team is working on making this easier, but for the moment you need to add the following to the android part of your apps build.gradle

   dexOptions {
      preDexLibraries = false
   }

And the following to the general part of your apps build.gradle

afterEvaluate {
   tasks.matching {
      it.name.startsWith('dex')
   }.each { dx ->
      if (dx.additionalParameters == null) {
         dx.additionalParameters = ['--multi-dex']
      } else {
         dx.additionalParameters += '--multi-dex'
      }
   }
}

More info can be found on Alex Lipovs blog.

Leo DroidCoder
  • 14,527
  • 4
  • 62
  • 54
Janusz
  • 187,060
  • 113
  • 301
  • 369
23

Here is an up-to-date approach as of October 2020, with Android X. This comes from Android's documentation, "Enable multidex for apps with over 64K methods."

For minSdk >= 21

You do not need to do anything. All of these devices use the Android RunTime (ART) VM, which supports multidex natively.

For minSdk < 21

In your module-level build.gradle, ensure that the following configurations are populated:

android {
    defaultConfig {
        multiDexEnabled true
    }
}

dependencies {
    implementation 'androidx.multidex:multidex:2.0.1'
}

You need to install explicit multidex support. The documentation includes three methods to do so, and you have to pick one.

For example, in your src/main/AndroidManifest.xml, you can declare MultiDexApplication as the application:name:

<manifest package="com.your.package"
          xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:name="androidx.multidex.MultiDexApplication" />
</manifest>
Jameson
  • 6,400
  • 6
  • 32
  • 53
20

SIMPLY, in order to enable multidex, you need to ...

android {
compileSdkVersion 21
buildToolsVersion "21.1.0"

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

    // Enabling multidex support.
    multiDexEnabled true
}
...
}

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

also you must change your manifest file. In your manifest add the MultiDexApplication class from the multidex support library to the application element like this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.android.multidex.myapplication">
   <application
       ...
       android:name="android.support.multidex.MultiDexApplication">
       ...
   </application>
</manifest>
Nikunj Paradva
  • 15,332
  • 5
  • 54
  • 65
smoothumut
  • 3,423
  • 1
  • 25
  • 35
  • 1
    This will not enable multi dexing. You are still missing the application class. – Chad Bingham Aug 04 '15 at 18:38
  • 1
    Thanks for the comment, This worked for my case. Here is the link I got reference from https://developer.android.com/tools/building/multidex.html – smoothumut Aug 06 '15 at 05:22
  • 1
    Yeah, your right. It looks like you don't need to have it in your application class unless you are running tests. – Chad Bingham Aug 07 '15 at 18:17
  • I don't get this.. My application manifest has already a name tag. I don't think i will replace it with `android:name="android.support.multidex.MultiDexApplication` Please guide me.. – mboy Mar 01 '16 at 13:26
  • @mboy : In that case you have to override attachBaseContext(Context base) in your application class. See Janusz's answer. – akshay7692 Mar 15 '16 at 10:33
  • @akshay7692 I have only one activity class(MainActivity) and others are fragments called by pressing some button from the MainActivity.. I'm not sure what is application class is.. Application class is the same as my MainActivty class? Pls correct me if im wrong.. – mboy Mar 16 '16 at 00:30
  • 1
    @mboy : No, MainActivity and application class are different. Know more about application class here : http://rominirani.com/android-application-class/ – akshay7692 Mar 17 '16 at 13:15
  • I understand now.. Thank you so much! – mboy Mar 18 '16 at 12:04
  • I think putting `package="com.example.android.multidex.myapplication">` is possibly misleading b/c it gives the impression that myapplication class should be included in the package name. It should just be the package root up to but not including the class. – EntangledLoops Mar 29 '16 at 14:19
  • android:name="android.support.multidex.MultiDexApplication" ... this part saves my life – Jamil Jan 25 '17 at 07:07
9

In your build.gradle add this dependency:

compile 'com.android.support:multidex:1.0.1'

again in your build.gradle file add this line to defaultConfig block:

multiDexEnabled true

Instead of extending your application class from Application extend it from MultiDexApplication ; like :

public class AppConfig extends MultiDexApplication {

now you're good to go! And in case you need it, all MultiDexApplication does is

public class MultiDexApplication extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Rez
  • 4,501
  • 1
  • 30
  • 27
8

build.gradle

multiDexEnabled true
implementation 'androidx.multidex:multidex:2.0.1'

AndroidManifest.xml

<application
    android:name="androidx.multidex.MultiDexApplication"
3

Step 1: Change build.grade

defaultConfig {
    ...
    // Enabling multidex support.
    multiDexEnabled true
}

dependencies {
    ...
    compile 'com.android.support:multidex:1.0.0'
}

Step 2: Setting on the Application class

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        MultiDex.install(this);
    }
}

Step 3: Change grade.properties

org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

It will work!. Thanks.

Sven R.
  • 1,049
  • 17
  • 24
sonnv1368
  • 1,547
  • 12
  • 17
2

First you should try with Proguard (This clean all code unused)

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
Moises Apaza Q
  • 131
  • 2
  • 4
1

Add to AndroidManifest.xml:

android:name="android.support.multidex.MultiDexApplication" 

OR

MultiDex.install(this);

in your custom Application's attachBaseContext method

or your custom Application extend MultiDexApplication

add multiDexEnabled = true in your build.gradle

defaultConfig {
    multiDexEnabled true
}

dependencies {
    compile 'com.android.support:multidex:1.0.0'
    }
}
Malik Abu Qaoud
  • 208
  • 1
  • 9
1

With androidx, the classic support libraries no longer work.

Simple solution is to use following code

In your build.gradle file

android{
  ...
  ...
  defaultConfig {
     ...
     ...
     multiDexEnabled true
  }
  ...
}

dependencies {
  ...
  ...
  implementation 'androidx.multidex:multidex:2.0.1'
}

And in your manifest just add name attribute to the application tag

<manifest ...>
    <application
        android:name="androidx.multidex.MultiDexApplication"
     ...
     ...>
         ...
         ...
    </application>
</manifest>

If your application is targeting API 21 or above multidex is enables by default.

Now if you want to get rid of many of the issues you face trying to support multidex - first try using code shrinking by setting minifyEnabled true.

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
1

If your minSdkVersion is set to 21 or higher, multidex is enabled by default and you do not need the multidex library:

Modify the module-level build.gradle file to enable multidex and add the multidex library as a dependency, as shown here:

  android {
        defaultConfig {
            ...
            minSdk = 15 
            targetSdk = 28
            multiDexEnabled = true
        }
        ...
    }

dependencies {
    implementation("androidx.multidex:multidex:2.0.1")
}

If you do override the Application class, change it to extend MultiDexApplication (if possible) as follows:

public class MyApplication extends MultiDexApplication { ... }

Or if you do override the Application class but it's not possible to change the base class, then you can instead override the attachBaseContext() method and callMultiDex.install(this) to enable multidex:

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

for more info check out this blog

Sourav Rana
  • 141
  • 1
  • 2
0

If you want to enable multi-dex in your project then just go to gradle.builder

and add this in your dependencie

 dependencies {
   compile 'com.android.support:multidex:1.0.0'}

then you have to add

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

// Enabling multidex support.
multiDexEnabled true}

Then open a class and extand it to Application If your app uses extends the Application class, you can override the oncrete() method and call

   MultiDex.install(this) 

to enable multidex.

and finally add into your manifest

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.multidex.myapplication">
    <application
   ...
     android:name="android.support.multidex.MultiDexApplication">
   ...
   </application>
 </manifest> 
pavel
  • 1,603
  • 22
  • 19
0

All answers above are awesome

Also add this otherwise your app will crash like mine without any reason

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
Abhishek Mathur
  • 478
  • 5
  • 12
-2

just adding this snipped in the build.gradle also works fine

android {
   compileSdkVersion 22
   buildToolsVersion "23.0.0"

     defaultConfig {
         minSdkVersion 14 //lower than 14 doesn't support multidex
         targetSdkVersion 22

         **// Enabling multidex support.
         **multiDexEnabled true****
     }
}
slfan
  • 8,950
  • 115
  • 65
  • 78
Chirag Thummar
  • 665
  • 6
  • 16
-6

Multi_Dex.java

public class Multi_Dex extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
lat_long
  • 1
  • 3