MultiDex implementation leads to compilation for so long, and finally, a heap error

I have a big Android project where I got Unable to execute method identifier dex: ID not in [0, 0xffff]: 65536 error; I believe that some of you guys have definitely experienced this problem before. This is a bug due to too many methods specified in the application.

I searched various sources on the Internet and found this might be the best solution.

And I did the following:

  • Added multiDexEnabled = true to the defaultConfig build.gradle block.
  • The following dependency is added:

     dependencies { compile 'com.android.support:multidex:1.0.0' } 
  • In my App class, the following code is overridden.

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

Now the error has disappeared, but I have a new problem. When I run the application, the compiler takes more than 3 minutes to compile and run the application, and finally gives me this error:

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

I understand that 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" when compiling a MonoDroid application in MonoDevelop , but this does not explain the cause of the problem, and moreover, I do not use Xamarin Studio.

EDIT2

The exact symptom is as follows:
When I compile the code, the gradle console shows me hundreds of warnings that say: “Ignoring the InnerClasses attribute for an anonymous inner class” (that some other answers from stackoverflow indicate that these are not serious warnings), and then only shows a blinking cursor for time, and after about one minute it gives me an error.

+5
source share
2 answers

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

 android { ... dexOptions { javaMaxHeapSize "4g" } } 
+9
source

Try this in gradle assembly

 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' } 
-1
source

Source: https://habr.com/ru/post/1211441/


All Articles