Reputation: 189594
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?
Upvotes: 173
Views: 291653
Reputation: 191
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
Upvotes: 3
Reputation: 6679
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."
minSdk
>= 21You do not need to do anything. All of these devices use the Android RunTime (ART) VM, which supports multidex natively.
minSdk
< 21In 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>
Upvotes: 26
Reputation: 18806
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
.
Upvotes: 1
Reputation: 480
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
}
Upvotes: 0
Reputation: 33894
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.
Upvotes: 366
Reputation: 361
build.gradle
multiDexEnabled true
implementation 'androidx.multidex:multidex:2.0.1'
AndroidManifest.xml
<application
android:name="androidx.multidex.MultiDexApplication"
Upvotes: 8
Reputation: 3491
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>
Upvotes: 20
Reputation: 141
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'
}
}
}
Upvotes: 2
Reputation: 1
Multi_Dex.java
public class Multi_Dex extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
Upvotes: -6
Reputation: 189594
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.
Upvotes: 47
Reputation: 3282
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****
}
}
Upvotes: -2
Reputation: 208
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'
}
}
Upvotes: 1
Reputation: 1597
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.
Upvotes: 3
Reputation: 4773
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);
}
}
Upvotes: 9
Reputation: 1701
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>
Upvotes: 0