Multidex issue in Android

2019-07-21 05:17发布

问题:

I dont Know how to solve Multidex issue in Android. My app has lots of SDKs integration, so App is crossing 65k Over Method limit. I went through lots of tutorials and blogs. I got so many solutions. One of them is below mentioned as part of my gradle. Please someone provide a good and working solution.

android {
minSdkVersion 16
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
    applicationId "your.package.name"
    ..........    }

buildTypes {
    release {
     .............................
    }
}
// Inside android part of build.gradle
dexOptions {
    preDexLibraries = false
}

} 
 .............
 ............

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
 ..................

 // Inside dependencies part of build.gradle
compile 'com.android.support:multidex:1.0.1'
..............
 }


 // Outside android part of build.gradle
afterEvaluate {
tasks.matching {
    it.name.startsWith('dex')
}.each { dx ->
    if (dx.additionalParameters == null) {
        dx.additionalParameters = []
    }
    dx.additionalParameters += '--multi-dex'
   }

 ........
 .......
}

回答1:

This what we have been using in our project. It seem to work just fine. If you have a machine with low memory, you should lover the 2g parameter as it means that multidex can use up to 2GB of memory. This wasn't an issue for us.

 defaultConfig{
       // All other stuff you have, just add this line
       multiDexEnabled true
    }   

dexOptions {
        incremental true
        javaMaxHeapSize "2g"
    }

dependencies {
   'com.android.support:multidex:1.0.1'
}

In your project you should create an Application class and it should extend the MultiDexApplication

    import android.support.multidex.MultiDexApplication;
    public class YourApllication extends MultiDexApplication {

        @Override
        public void onCreate() {
          super.onCreate();
      }
   }