Summary of restrictions on the Android Dex method, androiddex
Address: http://greenrobot.me/devpost/about-android-dex-method-number-limit/
Android compilation process
Before learning about this issue, let's take a look at the Android app compilation process:
- The Android Asset Packaging Tool (aapt in the figure) in IDE compiles the resource files in the application, including
AndroidManifest.xml
File, the XML file defined for the Activity, and so on. In this compilation process,R.java
File so that you can reference these resources in your Java code.
- The aidl tool will
.aidl
Converts an interface to a Java interface.
- All Java code in the project, including
R.java
And.aidl
Files will be compiled by the Java compiler and then output the. class file.
- The dex tool then converts the. class file generated in the previous step into the Dalvik bytecode, that is
.dex
File. All third-party class libraries and. class files contained in the project will also be converted.dex
File to facilitate the next step to be packaged as the final.apk
File.
- All uncompiled resources (such as comparison files), compiled resource files, and. dex files will be packaged into one by apkbuilder.
.apk
File.
- Once
.apk
After the file is built, if you want to install it on the device, it must use a debug or release key to sign the apk file.
- Finally, if the application has been signed as the release mode apk, you also need to use the aipalign tool
.apk
Alignment Optimization. This can reduce the memory consumption of applications on the device.
Why is this Dex method restricted internally:
We noticed that in Step 4,.dex
File. Android can execute this function from Dalvik to the default ART runtime environment of Android 5.0..dex
File, they also use the same set of instruction sets, that is, the Dalvik instruction set. In this introduction to the Android instruction set format, I know that the Dalvik Instruction Set uses 16-bit registers to save all method references in the project, including third-party methods:
invoke-kind {vC, vD, vE, vF, vG}, meth@BBBBB: method reference index (16 bits)
This means that a single Android.dex
The file can reference a maximum of 65536 methods, and subsequent methods cannot be referenced. This is why the Android Dex method restricts exceptions. Because ART and Dalvik use the same set of instruction sets, this restriction also exists in the runtime environment of ART.
External reasons:
Third-party libraries contain too many methods. Here we will take Google Play Service and Guava as examples. Many Android Developers use the Google Play Service library and Guava library. Do you know how many methods they provide? Google Play Service 5.0 contains almost 20 k + methods, and Guava provides nearly 14 k methods. These two databases account for nearly half of the limit on the number of methods.
So how can we solve the problem of restrictions on the Android Dex method? Old Method
For internal reasons:
- As described above, the limitation of the Android Dex method is that
.dex
File, so we can use multiple.dex
File? Yes. This solution is provided by the official Android blog. (Before Android5.0, because most of the environments used when Dalvik was running, the Dalvik runtime environment restricted a single apk to contain only one classes. dex file .)
For external reasons:
- Use the configuration script to clear methods in third-party libraries;
- Use ProGuard to clear useless methods in the project, but the effect is not as good as above.
New method (official action)
Main Idea: Use the multidex support library to allow versions earlier than Android to include multiple.dex
File. For more information, see this article.
Google not only improved the tool, but also made some changes to its Google Play Service library-Support for fine-grained dependency management since Google Play Service 6.5, that is to say, you only need the Google Drive api, instead of the google game, maps, wallet, and other api support. Then you can only introduce the Google Drive api. This can greatly reduce the chances of the Dex method restriction.
Reference link:
- Http://developer.android.com/tools/building/index.html
- Http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html
- Https://medium.com/@rotxed/dex-skys-the-limit-no-65k-methods-is-28e6cb40cf71
- Http://developer.android.com/tools/building/multidex.html
- Jakewharton.com/play-services-is-a-monolith/
- Http://www.keysolutions.com/blogs/kenyee.nsf/d6plinks/KKYE-9LP5ND
- Http://www.alittlemadness.com/2010/06/07/understanding-the-android-build-process/
- Http://source.android.com/devices/tech/dalvik/dalvik-bytecode.html
- Http://stackoverflow.com/questions/21490382/does-the-android-art-runtime-have-the-same-method-limit-limitations-as-dalvik/21492160#21492160
- Https://android.googlesource.com/platform/dalvik/+/froyo/vm/analysis/ReduceConstants.c