How to enable multidexing with the new Android Multidex support library

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

    build.gradle

    multiDexEnabled true
    implementation 'androidx.multidex:multidex:2.0.1'
    

    AndroidManifest.xml

    <application
        android:name="androidx.multidex.MultiDexApplication"
    
    0 讨论(0)
  • 2020-11-21 13:51

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

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

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

    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'
            }
        }
    }
    
    0 讨论(0)
  • 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.

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