Android uses Android-support-multidex to solve the limit of Dex out of method number

Source: Internet
Author: User

as applications continue to iterate, lines of business expand, and applications become larger (such as the integration of various third-party SDKs or publicly supported JAR packages, project coupling is high, the number of repetitive classes is increasing), I believe many people have encountered the following errors:

  1. Unexpected top-level EXCEPTION:
  2. Java.lang.IllegalArgumentException:method ID not in [0, 0xFFFF]: 65536
  3. At com.android.dx.merge.dexmerger$6.updateIndex (Dexmerger.java:501)
  4. At com.android.dx.merge.dexmerger$idmerger.mergesorted (Dexmerger.java:282)
  5. At Com.android.dx.merge.DexMerger.mergeMethodIds (Dexmerger.java:490)
  6. At Com.android.dx.merge.DexMerger.mergeDexes (Dexmerger.java:167)
  7. At Com.android.dx.merge.DexMerger.merge (Dexmerger.java:188)
  8. At Com.android.dx.command.dexer.Main.mergeLibraryDexBuffers (Main.java:439)
  9. At Com.android.dx.command.dexer.Main.runMonoDex (Main.java:287)
  10. At Com.android.dx.command.dexer.Main.run (Main.java:)
  11. At Com.android.dx.command.dexer.Main.main (Main.java:199)
  12. At Com.android.dx.command.Main.main (Main.java:103)

Yes, the number of Dex file methods in your app exceeds the maximum of 65536, so simply put, the app is bursting.

in the Android system, all the code for a App is in a Dex file. Dex is a jar-like archive that stores multiple Java compiled bytecode. Because the Android system uses the Dalvik virtual machine, you need to use Java Compiler after compiling the The class file is converted to a Dalvik executable class file. It is emphasized here that Dex and Jar are the same archive file, which is still a bytecode file corresponding to the Java code. When the Android system launches an app, one step is to optimize Dex , a process that has a special tool to handle, called dexopt . The execution of dexopt is performed the first time the Dex file is loaded. This process generates a ODEX file, which is optimised Dex . Performing ODex is much more efficient than executing Dex files directly.   But in earlier Android systems, there was a limit to the linearalloc of dexopt :  android 2.2 and 2.3 had only 5mb,android 4 buffers. x increased to 8MB or 16MB. When the number of methods exceeds the buffer size, dexopt crashes, causing the installation to fail.  

Of course,Google seems to be aware of the current number of application methods of the problem, now in the API has provided a common solution, that is Android-support-multidex.jar . This jar package can support up to API 4 version ( Mutidexis supported by default onAndroid L and above).

Specific integration:
Add the following configuration to the project Build.gradle

    1. Android {
    2. Defaultconfig {
    3. //enabling Multidex support.
    4. Multidexenabled True
    5. }
    6. }
    7. dependencies {compile ' com.google.android:multidex:0.1 '}

The next integration has two steps:

I. Import Android-support-multidex.jar into the project from the Sdk\extras\android\support\multidex\library\libs directory

Two. If your project already contains the application class, then let it inherit the Android.support.multidex.MultiDexApplication class,

If your application has inherited other classes and does not want to make changes, there is another way to use it, overwrite the Attachbasecontext () method:

    1. Public class MyApplication extends Fooapplication {
    2. @Override
    3. protected void Attachbasecontext (Context base) {
    4. Super.attachbasecontext (base);
    5. Multidex.install (this);
    6. }
    7. }

Finally, the complete configuration in Build.gradle is given:

  1. Android {
  2. Compilesdkversion
  3. Buildtoolsversion "21.1.0"
  4. Defaultconfig {
  5. ...
  6. Minsdkversion
  7. Targetsdkversion
  8. ...
  9. //enabling Multidex support.
  10. Multidexenabled True
  11. }
  12. ...
  13. }
  14. dependencies {
  15. Compile ' com.android.support:multidex:1.0.0 '
  16. }

Android uses Android-support-multidex to solve the limit of Dex out of method number

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.