Implementing MultiDex results in compiling for so long, and finally heap space error

穿精又带淫゛_ 提交于 2019-12-09 13:16:28

问题


I have a big Android project where I got the Unable to execute dex: method ID not in [0, 0xffff]: 65536 error; I believe some of you guys have definitely gone through this issue before. This is an error due to too many methods referenced in the app.

I have looked for different sources online and found this might be the best solution.

And I did the following:

  1. Added multiDexEnabled = true in the defaultConfig block of build.gradle.
  2. Added the following dependency:

    dependencies {
      compile 'com.android.support:multidex:1.0.0'
    }
    
  3. Overrided the following code inside my App class.

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    

Now, the error is gone, but I have a new problem. When I run the app, the compiler takes more than 3 minutes for compiling and running the app, finally giving me this error:

UNEXPECTED TOP-LEVEL ERROR:
java.lang.OutOfMemoryError: Java heap space

I understand this error occurs due to heavy memory usage, but I don't know how to solve it. I am using Android Studio 1.0.2, and Android API 21.

Thanks!

EDIT:

I already checked How to fix “OutOfMemoryError: java heap space” while compiling MonoDroid App in MonoDevelop, but this does not cover the reason of the problem, and moreover, I am not using Xamarin Studio.

EDIT2:

The exact symptom is like this:
When I compile the code, the gradle console shows me hundreds of warnings saying "Ignoring InnerClasses attribute for an anonymous inner class" (which some other stackoverflow answers suggest that are not serious warnings), and then shows nothing but a blinking cursor for a while, and after about one minute, it gives me the error.


回答1:


Have you tried adding the heap size adjustment to your build.gradle file? For example this will set the max heap size for dexing to 4GB.

android {
    ...
    dexOptions {
        javaMaxHeapSize "4g"
    }
}



回答2:


Try this in build gradle

android {
compileSdkVersion 21
buildToolsVersion "21.1.1"

defaultConfig {
           minSdkVersion 14 //lower than 14 doesn't support multidex
           targetSdkVersion 21
// Enabling multidex support.
           multiDexEnabled true
    }
 }
dependencies {
   compile 'com.android.support:multidex:1.0.0'
  }


来源:https://stackoverflow.com/questions/28031860/implementing-multidex-results-in-compiling-for-so-long-and-finally-heap-space-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!