[Turn] android solves the problem that the number of apk compilation methods exceeds 64k

If you're an android developer, you've at least heard of Dalvik's egg-paining 64K method limit. In a nutshell, in a DEX file, you can call many methods, but you can only call the first 65,536 of them, because that's all the space in the method call set. If your source code and methods in the third-party library exceed this limit. Just 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)


To solve this problem, some people in the Android development community have come up with solutions, such as this one from dmarcato, and this one from casidiablo. They are all feasible, but require some strict conditions.

Eventually, Google decided to provide an official solution, releasing the MultiDex support library on October 14th, and gradle in v0.14.0 a few weeks later.

Using the MultiDex Support Library

If you are using Android Studio, this is easy to use. If not, it is strongly recommended that you migrate over. Because Google will soon be ignorant of Eclipse plugins and the way old Ant-based systems are built.

Step 1
Add dependencies to your build.gradle to support MultiDex library

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

Step 2 Enable multiDexEnabled
in buildType or productFlavor.

defaultConfig { ... multiDexEnabled true ... }

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

If you don't create your own Application class, configure android.support.multidex in your manifest file AndroidManifest.xml. MultiDexApplication will do just fine.

....android:name="android.support.multidex.MultiDexApplication" ...

If you have your own Application class, make it inherit android.support.multidex.MultiDexApplication instead of android.app.Application
If your Application inherits other classes, and you don't want to change or can't change it. Rewrite attachBaseContext() as follows

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

Whichever you choose above, will create Multiple dex files of similar size replace a single huge dex file. When running, go back to my colleagues to load all these dex files.

When compiling an app back in the day, Gradle would generate a number of dex files and an apk file that you could run on a device or emulator.

enter image description here
you can see the above effect from this project

Note

Out of memory issue
For projects with many dependencies, compilation may break with 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 under the build.gralde android tag to solve

dexOptions { incremental true javaMaxHeapSize "4g" }

slow application startup
According to our experience, after adding this support library, most The situation is normal. On some devices, such as the Kindle Fire, the application launch will be much slower than before. Loading all the classes can take a lot of time when the application starts. This will lead to a black screen for a period of time, and even lead to ANR.


Conclusion Although

this can solve the problem of DEX 64K most of the time, it should be reserved for use. Before you try to use it, try removing unneeded dependencies and obfuscating with ProGuard if you must. Make sure to test on an older device.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326610098&siteId=291194637