Use Android-support-multidex to solve problems with Android methods over 65535

Source: Internet
Author: User

As the application continues to update and the business continues to increase, the application may need to integrate a variety of third-party libraries, I believe many people have encountered the following similar errors:

Unexpected top-level EXCEPTION:java.lang.IllegalArgumentException:Method ID notinch[0,0xFFFF]:65536Atcom. Android. DX. Merge. Dexmerger$6. Updateindex(Dexmerger. Java:501) atcom. Android. DX. Merge. Dexmerger$IdMerger. mergesorted(Dexmerger. Java:282) atcom. Android. DX. Merge. Dexmerger. Mergemethodids(Dexmerger. Java:490) atcom. Android. DX. Merge. Dexmerger. Mergedexes(Dexmerger. Java:167) atcom. Android. DX. Merge. Dexmerger. Merge(Dexmerger. Java:188) atcom. Android. DX. Command. Dexer. Main. Mergelibrarydexbuffers(Main. Java:439) atcom. Android. DX. Command. Dexer. Main. Runmonodex(Main. Java:287) atcom. Android. DX. Command. Dexer. Main. Run(Main. Java: the) atcom. Android. DX. Command. Dexer. Main. Main(Main. Java:199) atcom. Android. DX. Command. Main. Main(Main. Java:103)

This is how the number of methods in your app is more than 65535, so what is the specific reason for this problem?
In an Android system, all the code for an app is inside a dex file. Dex is a jar-like archive that stores many Java-compiled bytecode. Because the Android system uses the Dalvik virtual machine, you need to convert the class file that was compiled with Java compiler into a class file that Dalvik can execute. The point here is that Dex, like the jar, is an archive file, which is still a bytecode file for Java code. When the Android system launches an application, one step is to optimize for Dex, a process that has a dedicated tool to handle, called dexopt. The execution of the dexopt is performed when the Dex file is loaded for the first time. This process generates a Odex file, which is optimised Dex. Executing Odex is much more efficient than directly executing the Dex file. But in the early days of Android, Dexopt had a problem, which is what this article wants to illustrate and solve. Dexopt will retrieve the method ID of each class, and there is a list structure inside. However, the length of the list is saved with a short type, resulting in no more than 65,536 method IDs. When a project is large enough, it is clear that the upper limit of this method is not enough. Although dexopt fixed this issue in the new version of Android, we still need to be compatible with the low-version Android system.

The following is the specific configuration using Android-support-multidex

    1. Open Multidex Support in Build.gradle
android {      23      "25"      defaultConfig {          ...          18          20          ...          // Enable multidex support.          multiDexEnabled true      }      ...  }

2. Introduction of Multidex Library dependencies

{    compile ‘com.android.support:multidex:1.0.0‘  }

3. Configure application, choose one of the following two ways

    • Inherit Mutidexapplication
publicclass BsApplication extends MutiDexApplication {}
    • Overwrite the Attachbasecontext () method in application
publicclass BsApplication extends Application {    @Override    protectedvoidattachBaseContext(Context base) {        super.attachBaseContext(base);        MultiDex.install(this);    }}

FAQ :
1. dexexception:library dex files is not supported in Multi-dex mode error

Error:execution failed forTask': App:dexdebug '. > com.android.ide.common.internal.LoggedErrorException:Failed toRun command:$ANDROID _sdk/build-tools/android-4.4W/dx--dex--num-threads=4--multi-dex... Error Code:2output:unexpected top-level EXCEPTION:com.android.dex.DexException:Library DexFilesIs notSupportedinchMulti-dex mode atCom.android.dx.command.dexer.Main.runMultiDex (Main.java:322) atCom.android.dx.command.dexer.Main.run (Main.java:228) atCom.android.dx.command.dexer.Main.main (Main.java:199) atCom.android.dx.command.Main.main (Main.java:103)

This is because Dex's multi-dex option settings conflict with the precompiled Library project, so if your app contains a referenced lirary project, you need to set the precompilation to false:

android {       dexOptions {          false      }  

2.outofmemoryerror:java Heap Space Error

ERROR:  space

This is due to insufficient stack memory to increase the Java stack memory size in dexoptions

android {       dexOptions {          "2g"      }  }  

Use Android-support-multidex to solve problems with Android methods over 65535

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.