How to enable multidexing with the new Android Multidex support library

后端 未结 14 2597
逝去的感伤
逝去的感伤 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:56

    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.

提交回复
热议问题