Original address: http://greenrobot.me/devpost/about-android-dex-method-number-limit/
The Android compilation process
Before we get to the point, let's take a look at the Android app compilation process:
- androidmanifest.xml file, XML file defined for activity, and so on. In this compilation process will also produce a
r.java
files so you can reference these resources in your Java code.
- aidl tool will style= all
.aidl
interface into 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.
- Then the Dex tool will turn the. class file from the previous step into Dalvik bytecode, which is
.dex
files. All third-party class libraries and. class files included in the project are also converted to .dex
file, so that the next step is packaged into the final .apk
files.
- All resources that cannot be compiled (compared to slices, and so on), compiled resource files, and. dex files are packaged into a single file by the Apkbuilder tool
.apk
.
- Once the
.apk
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 already been signed into the release mode apk, you also need to use the Aipalign tool to
.apk
align the optimizations. This can reduce the memory consumption of the application on the device.
Why this Dex method restricts internal causes:
We note that in the fourth step, there will be a .dex
files. Android from the previous Dalvik to now Android 5.0 the default art runtime environment is able to perform this .dex
files, they also use the same set of instructions, the dalvik instruction set. In this introductory article on the Android instruction set format, I can tell 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}, [email protected]b:method reference Index (BITS)
This means that a single .dex
file on Android can refer to 65,536 methods in the most ways, and the method behind it cannot be referenced. This is why the Android Dex method restricts exceptions, and because art and Dalvik use the same set of instructions, this restriction also exists in the Art runtime environment.
External reasons:
The third-party library contains too many methods. Take the Google Play service and guava here for example. Many Android developers will use the Google Play Service library and the Guava library, and do you know how much they offer? Google Play Service 5.0 contains almost the 20k+ approach, and guava provides nearly 14k of methods. This two library is nearly half of the 65536 of the method limit.
So how to solve the Android Dex method limit this problem? Old method
For internal reasons:
- From the above description we know that the Android Dex method limit is present in a single
.dex
file, then we can use multiple .dex
file? Yes, the Android official blog gives the program. (Before Android5.0, because most of the Dalvik runtime environment is used, the Dalvik runtime environment restricts an APK to include only one Classes.dex file.) )
for external reasons:
- Use configuration scripts to purge methods in third-party libraries;
- use proguard clears the useless method in the project, but the effect is not as good as above.
New Method (Official action)
Main idea: Using the Multidex Support Library allows Android5.0 versions to include multiple files in an apk .dex
. Please refer to this article for details of how to use it.
Not only did Google make improvements on the tools, but it also made changes to its Google Play service library--starting with Google Play service 6.5 to support finer-grained dependency management, which means you only need Google Drive API, without the support of a Google Game,maps or wallet API, you can simply introduce the Google Drive API. This can greatly reduce the chance that the Dex method will limit the occurrence.
Reference Links:
- 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
Some summary of limitations on Android Dex methods