How to enable multidexing with the new Android Multidex support library

249,833

Solution 1

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.

Solution 2

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.

Solution 3

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>

Solution 4

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>

Solution 5

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);
    }
}
Share:
249,833

Related videos on Youtube

SHRISH M
Author by

SHRISH M

Updated on October 03, 2020

Comments

  • SHRISH M
    SHRISH M over 3 years

    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?

    • Keshav Gera
      Keshav Gera over 5 years
      multidex support third party library to your application
  • lacas
    lacas over 9 years
    how can I use this without gradle?
  • Devrim
    Devrim over 9 years
    @Janusz is it possible to use multidex option without gradle?
  • SHRISH M
    SHRISH M over 9 years
    I don't think this is possible without gradle.
  • SHRISH M
    SHRISH M over 9 years
    If you are using the new builds tools yet this is definitely the way to go.
  • Marino
    Marino over 9 years
    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?
  • Chad Bingham
    Chad Bingham over 9 years
    @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'
  • Marino
    Marino over 9 years
    @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!
  • portfoliobuilder
    portfoliobuilder over 9 years
    What is the Eclipse Ant fix, not gradle?
  • BaDo
    BaDo over 9 years
    My project dont create gradle file, i created one and followed your steps, but it is not working for me
  • JPM
    JPM almost 9 years
    So glad we went to gradle, using ant this would have been a nightmare.
  • Chad Bingham
    Chad Bingham almost 9 years
    This will not enable multi dexing. You are still missing the application class.
  • smoothumut
    smoothumut almost 9 years
    Thanks for the comment, This worked for my case. Here is the link I got reference from developer.android.com/tools/building/multidex.html
  • Chad Bingham
    Chad Bingham almost 9 years
    Yeah, your right. It looks like you don't need to have it in your application class unless you are running tests.
  • Raji A C
    Raji A C almost 9 years
    I am also trying to build my project with moulted support,but while running the application it is crashing saying ClassNotFound Exception for my application class.Also only classes.ex file is created even though I specified splitdexfile.Please help
  • Buddy
    Buddy almost 9 years
    How about if we don't use application class? Is adding android:name="android.support.multidex.MultiDexApplication" to application tag in AndroidManifest.xml sufficient?
  • Zapnologica
    Zapnologica over 8 years
    Do you not need to add it to the release section as well?
  • Chad Bingham
    Chad Bingham over 8 years
    @Zapanologica If it is in your 'defaultConfig' you should be good.
  • Anuj
    Anuj over 8 years
    This is how it fixed the Multi-Dex crash on my app, thanks a lot, please refer the link developer.android.com/tools/building/multidex.html
  • mboy
    mboy over 8 years
    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..
  • akshay7692
    akshay7692 over 8 years
    @mboy : In that case you have to override attachBaseContext(Context base) in your application class. See Janusz's answer.
  • mboy
    mboy over 8 years
    @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..
  • akshay7692
    akshay7692 over 8 years
    @mboy : No, MainActivity and application class are different. Know more about application class here : rominirani.com/android-application-class
  • mboy
    mboy over 8 years
    I understand now.. Thank you so much!
  • EntangledLoops
    EntangledLoops about 8 years
    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.
  • Naveed Ahmad
    Naveed Ahmad about 8 years
    compile 'com.android.support:multidex:1.0.1' is not needed in 23
  • Chad Bingham
    Chad Bingham about 8 years
    What is gradle.builder?
  • pavel
    pavel about 8 years
    your gradle file which is located in your android studio project
  • Harrish Android
    Harrish Android almost 8 years
    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
  • dazza5000
    dazza5000 over 7 years
    I had everything else, but this is what fixed it for me on KitKat devices: dexOptions { preDexLibraries = false }
  • Jamil
    Jamil over 7 years
    android:name="android.support.multidex.MultiDexApplication" ... this part saves my life
  • Sakiboy
    Sakiboy over 7 years
    @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'.
  • Malik Abu Qaoud
    Malik Abu Qaoud over 7 years
    @Harsha its not recommended to use this but try to add largeHeap to your manifest file <application android:largeHeap="true"></application>
  • Harsha
    Harsha about 7 years
    android:largeHeap="true" android:allowBackup="true" am added both
  • Malik Abu Qaoud
    Malik Abu Qaoud about 7 years
    add this to the gradle file >>>>>>>>> dexOptions { javaMaxHeapSize "4g" }
  • Juan Mendez
    Juan Mendez over 6 years
    based on documentation, compile 'com.android.support:multidex:1.0.1' is required if your minSdkVersion is set to 20. developer.android.com/studio/build/multidex.html
  • Sergio
    Sergio over 6 years
    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
  • Vova
    Vova over 6 years
    Multydex has new version 1.0.3
  • ratsimihah
    ratsimihah about 6 years
    For APIs < 21 you might need to extend MultiDexApplication instead of Application as well.
  • Chad Bingham
    Chad Bingham about 6 years
    @ratsimihah There is no requirement to extend that class. It is just a helper.
  • Aung Myint Thein
    Aung Myint Thein almost 5 years
    I am getting "Cannot find symbol for MultiDex". Anyone faced this problem before?
  • Tatsuya Fujisaki
    Tatsuya Fujisaki almost 4 years
    To be clear, for minSdkVersion 21+, not only the multidex support library but also any configuration of multidex such as "multiDexEnabled true" is unnecessary.
  • Mahdi-Malv
    Mahdi-Malv over 3 years
    So no MultiDexApplication and androidx:multidex is needed? Why other answers provide two more steps?
  • nicolas asinovich
    nicolas asinovich over 2 years
    For minSdk >= 21 not need multidex - this is true, thanks!