Summary of restrictions on the Android Dex method, androiddex

Source: Internet
Author: User

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:

  1. The Android Asset Packaging Tool (aapt in the figure) in IDE compiles the resource files in the application, includingAndroidManifest.xmlFile, the XML file defined for the Activity, and so on. In this compilation process,R.javaFile so that you can reference these resources in your Java code.
  2. The aidl tool will.aidlConverts an interface to a Java interface.
  3. All Java code in the project, includingR.javaAnd.aidlFiles will be compiled by the Java compiler and then output the. class file.
  4. The dex tool then converts the. class file generated in the previous step into the Dalvik bytecode, that is.dexFile. All third-party class libraries and. class files contained in the project will also be converted.dexFile to facilitate the next step to be packaged as the final.apkFile.
  5. All uncompiled resources (such as comparison files), compiled resource files, and. dex files will be packaged into one by apkbuilder..apkFile.
  6. Once.apkAfter 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.
  7. Finally, if the application has been signed as the release mode apk, you also need to use the aipalign tool.apkAlignment 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,.dexFile. Android can execute this function from Dalvik to the default ART runtime environment of Android 5.0..dexFile, 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.dexThe 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:

  1. As described above, the limitation of the Android Dex method is that.dexFile, so we can use multiple.dexFile? 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:

  1. Use the configuration script to clear methods in third-party libraries;
  2. 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.dexFile. 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

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.