How to enable multidexing with the new Android Multidex support library
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);
}
}
Related videos on Youtube
SHRISH M
Updated on October 03, 2020Comments
-
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 over 5 yearsmultidex support third party library to your application
-
-
lacas over 9 yearshow can I use this without gradle?
-
Devrim over 9 years@Janusz is it possible to use multidex option without gradle?
-
SHRISH M over 9 yearsI don't think this is possible without gradle.
-
SHRISH M over 9 yearsIf you are using the new builds tools yet this is definitely the way to go.
-
Marino over 9 yearsSo, 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 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 addmultiDexEnabled true
to your defaultConfig of your build.gradle (notice there is not a=
) 3 add the'com.android.support:multidex:1.0.0'
-
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 over 9 yearsWhat is the Eclipse Ant fix, not gradle?
-
BaDo over 9 yearsMy project dont create gradle file, i created one and followed your steps, but it is not working for me
-
JPM almost 9 yearsSo glad we went to gradle, using ant this would have been a nightmare.
-
Chad Bingham almost 9 yearsThis will not enable multi dexing. You are still missing the application class.
-
smoothumut almost 9 yearsThanks 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 almost 9 yearsYeah, your right. It looks like you don't need to have it in your application class unless you are running tests.
-
Raji A C almost 9 yearsI 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 almost 9 yearsHow about if we don't use application class? Is adding
android:name="android.support.multidex.MultiDexApplication"
toapplication
tag inAndroidManifest.xml
sufficient? -
Zapnologica over 8 yearsDo you not need to add it to the release section as well?
-
Chad Bingham over 8 years@Zapanologica If it is in your 'defaultConfig' you should be good.
-
Anuj over 8 yearsThis 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 over 8 yearsI 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 over 8 years@mboy : In that case you have to override attachBaseContext(Context base) in your application class. See Janusz's answer.
-
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 over 8 years@mboy : No, MainActivity and application class are different. Know more about application class here : rominirani.com/android-application-class
-
mboy over 8 yearsI understand now.. Thank you so much!
-
EntangledLoops about 8 yearsI 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 about 8 yearscompile 'com.android.support:multidex:1.0.1' is not needed in 23
-
Chad Bingham about 8 yearsWhat is
gradle.builder
? -
pavel about 8 yearsyour gradle file which is located in your android studio project
-
Harrish Android almost 8 yearsperfect 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 over 7 yearsI had everything else, but this is what fixed it for me on KitKat devices: dexOptions { preDexLibraries = false }
-
Jamil over 7 yearsandroid:name="android.support.multidex.MultiDexApplication" ... this part saves my life
-
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 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 about 7 yearsandroid:largeHeap="true" android:allowBackup="true" am added both
-
Malik Abu Qaoud about 7 yearsadd this to the gradle file >>>>>>>>> dexOptions { javaMaxHeapSize "4g" }
-
Juan Mendez over 6 yearsbased 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 over 6 yearsIt 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 over 6 yearsMultydex has new version 1.0.3
-
ratsimihah about 6 yearsFor APIs < 21 you might need to extend
MultiDexApplication
instead ofApplication
as well. -
Chad Bingham about 6 years@ratsimihah There is no requirement to extend that class. It is just a helper.
-
Aung Myint Thein almost 5 yearsI am getting "Cannot find symbol for MultiDex". Anyone faced this problem before?
-
Tatsuya Fujisaki almost 4 yearsTo 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 over 3 yearsSo no
MultiDexApplication
andandroidx:multidex
is needed? Why other answers provide two more steps? -
nicolas asinovich over 2 yearsFor minSdk >= 21 not need multidex - this is true, thanks!