DEX method exceeds 64K limit and gradle compilation Oom problem resolution

Source: Internet
Author: User
Tags gc overhead limit exceeded


If you are an Android developer, you have at least heard of Dalvik's 64K method limit for egg aches. In a nutshell, you can call a lot of methods in a Dex file, but you only have to call the first 65,536 of them, because this is all the space in the method call collection. If your source code and the crazy drag-and-drop method in the three-way vault of the day exceeds this limit. Look at this article.

UNEXPECTED TOP-LEVEL EXCEPTION:com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536    at com.android.dx.merge.DexMerger$6.updateIndex(DexMerger.java:502)    at com.android.dx.merge.DexMerger$IdMerger.mergeSorted(DexMerger.java:277)    at com.android.dx.merge.DexMerger.mergeMethodIds(DexMerger.java:491)    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:168)    at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:302)    at com.android.dx.command.dexer.Main.run(Main.java:245)    at com.android.dx.command.dexer.Main.main(Main.java:214)    at com.android.dx.command.Main.main(Main.java:106)

Click here to see more related topics

To solve this problem, someone in the Android development community came up with some solutions, such as Dmarcato and Casidiablo. They are all feasible, but require some more stringent conditions.

Finally, Google decided to provide a set of official solutions, released the Multidex Support Library on October 14, followed by a few weeks gradle in the v0.14.0 release.

Using the Multidex Support library

If you're using Android Studio, this is easy to use. If not, it is highly recommended that you migrate here. Because Google will soon be unaware of the eclipse plugin and the old Ant-based system build way.

1th Step
Add a Build.gradle support Multidex library that relies on your

dependencies { ...    compile ‘com.android.support:multidex:‘    ... }

2nd Step
Turn on multidexenabled in BuildType or Productflavor.

defaultConfig {    ...     multiDexEnabled true     ... }

Now, depending on your project situation, you have 3 options:

    1. If you did not create your own application class, configure it in your manifest file Androidmanifest.xml android.support.multidex.MultiDexApplication .

         ....    android:name="android.support.multidex.MultiDexApplication"    
    2. If you have your own application class, let it inherit android.support.multidex.MultiDexApplication instead ofandroid.app.Application
    3. If your application inherits other classes, and you don't want to change or change. Follow the methods below to overrideattachBaseContext()

      public class MyApplication extends FooApplication {    @Override    protected void attachBaseContext(Context base) {       super.attachBaseContext(base);       MultiDex.install(this);    } }

Whichever you choose, you'll create multiple Dex files of almost the same size instead of a single large Dex file. Run back to colleagues when loading all of these Dex files.

When compiling the app, Gradle will generate a lot of Dex files and an apk file that you can run on your device or emulator.


You can see the above effect from this project

Precautions

Out of memory issues
For projects with many dependencies, compilation may be interrupted due to the following error

Error:Execution failed for task ‘:app:dexDebug‘. ...    Error Code:       3    Output:       UNEXPECTED TOP-LEVEL ERROR:       java.lang.OutOfMemoryError: GC overhead limit exceeded at com.android.dx.cf.cst.ConstantPoolParser.parse0(ConstantPoolParser.java:326) ...

Add the following code below the Build.gralde android tab to resolve

dexOptions {    incremental true    javaMaxHeapSize "4g" }

Slow application Startup
Based on our experience, this support library has been added, and in most cases it is normal. For some devices, such as the Kindle Fire, the app will start much slower than before. Loading all classes takes a lot of time when the app is started. This results in a black screen for a period of time, even causing the ANR.

Click here for more recommended solutions

Conclusion

While this can solve the Dex 64K problem most of the time, it should be reserved for use. Before you try to use it, try removing unwanted dependencies and using Proguard obfuscation if you have to use this scenario. Make sure you have tested on the old device.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

DEX method exceeds 64K limit and gradle compilation Oom problem resolution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.