Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 652
technophyle Avatar asked Jan 19 '15 19:01

technophyle


1 Answers

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"
    }
}
like image 163
Robert Nekic Avatar answered Nov 17 '22 01:11

Robert Nekic