How to enable multidexing with the new Android Multidex support library

后端 未结 14 2405
逝去的感伤
逝去的感伤 2020-11-21 12:57

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

相关标签:
14条回答
  • 2020-11-21 13:43

    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);
        }
    }
    
    0 讨论(0)
  • 2020-11-21 13:45

    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>
    
    0 讨论(0)
  • 2020-11-21 13:47

    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'
        }
    }
    
    0 讨论(0)
  • 2020-11-21 13:47

    Multi_Dex.java

    public class Multi_Dex extends Application {
        @Override
        protected void attachBaseContext(Context base) {
            super.attachBaseContext(base);
            MultiDex.install(this);
        }
    }
    
    0 讨论(0)
  • 2020-11-21 13:48

    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.

    0 讨论(0)
  • 2020-11-21 13:49

    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.

    0 讨论(0)
提交回复
热议问题