The DEX method exceeds 64 K limit and the problem of gradle compiling OOM is solved. gradleoom
If you are an Android developer, you have heard of at least 64 K method restrictions on Dalvik. In summary, you can call a lot of methods in a DEX file, but you can only call the first 65,536 of them, because this is all the space in the method call set. If your source code and the method in the third-party library of the cool-looking days exceed this limit. It's right to read 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 view more related topics
To solve this problem, someone in the Android development community has come up with some solutions, such as dmarcato and casidiablo. They are all feasible, but they need some strict conditions.
In the end, Google decided to provide an official solution. In October 14, it released the MultiDex support library, and gradle also supported it in v0.14.0 in the next few weeks.
Use MultiDex to support libraries
If you are using Android Studio, this is easy to use. If not, we strongly recommend that you migrate it. Because Google will soon be unaware of the Eclipse plug-in and the old Ant-based system build method.
Step 2
Add the MultiDex library that depends on your build. gradle support
dependencies { ... compile 'com.android.support:multidex:' ... }
Step 2
Enable multiDexEnabled in buildType or productFlavor.
defaultConfig { ... multiDexEnabled true ... }
Now, you have three options based on your project:
No matter which one you choose, multiple dex files of the same size will be created to replace a single large dex file. When running, return to the colleague to load all these dex files.
When you compile the app that year, Gradle will generate many dex files and an apk file for you to run on the device or simulator.
You can see the above results from this project
Notes
Out of memory problems
For projects with many dependencies, compilation may be interrupted due to the following errors
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 under the build. gralde android tag to solve the problem.
dexOptions { incremental true javaMaxHeapSize "4g" }
Slow application startup
Based on our experience, after this support library is added, it works normally in most cases. For some devices, such as the Kindle Fire, application startup is much slower than before. Loading all classes takes a lot of time when the application starts. This may cause a black screen for a period of time, or even cause ANR.
For more recommended solutions, click here
Conclusion
This can solve the problem of DEX 64 K in most cases, but it should be reserved for use. Before you try to use it, try to delete unnecessary dependencies and use ProGuard for obfuscation. If you must use this solution. Make sure the test is performed on the old device.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.