Android Division Dex (MULTIDEX)

Source: Internet
Author: User

The reasons for the need for Dex must be known to all. It was in the previous Android system that the Dex file was stored with a short type of data for the method index. The maximum value for short is 65535, so when the project is large enough to contain more than 65535 of the methods (including all the methods in the referenced external Lib), when you run the app, you get the following error message.

inch [00xffff65536 in [00xffff65536

After this fatal bug appeared, Android officially wrote a famous blog (this article I read five or six times, even the author's Google + photos I have seen two or three times, I still do not fully understand how to walkaround this serious problem).

Later, slowly developed, there are a lot of talented people to play their own creativity, wrote Open source Lib on GitHub to share to other Android developers to use. Hellomultidex, Secondary-dex-gradle and so on.

But it is also necessary to ring people, this systematic serious fatal bug, of course, Android official solution is the most reassuring. Finally, we still wait for you, Multidex offical solution.

I recommend the official documentation and this GitHub article to see a better understanding, less detours.

Let's talk about the steps used:

1. Modify Gradle, import ' com.android.support:multidex:1.0.0 ', open multidexenabled;

Android {compilesdkversion +buildtoolsversion"21.1.0"defaultconfig {... minsdkversion -targetsdkversion +        ...        //enabling Multidex support.Multidexenabledtrue    }    ...} dependencies {Compile'com.android.support:multidex:1.0.0'}

2. Modify Application. Two ways:

1) Replace the application directly with the Multidexapplication

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.android.multidex.myapplication">    <Application. .. android:name= "Android.support.multidex.MultiDexApplication">        ...    </Application></Manifest>

2) Modify the call Multidex in the original application . Install(this);

 public  class     Hellomultidexapplication extends   application { @Override  public  void   OnCreate () { super  .oncreate (); } @Override  protected  void   Attachbasecontext (Context base) { super  .attachbasecontext (base);    Multidex.install ( this  ); }}

In fact, the Multidex configuration has been completed.

However, in fact, the following are some of the issues that are worth our attention.

1. Some classes (such as static variable classes) that may be called before the two-level Dex load, must be placed in the main Dex. Otherwise it will classnotfounderror.

By modifying the Gradle, you can display some classes in main Dex.

Afterevaluate {tasks.matching {it.name.startsWith ('Dex')}.each {dx-if(Dx.additionalparameters = =NULL) {dx.additionalparameters=[]} dx.additionalparameters+='--multi-dex'dx.additionalparameters+="--main-dex-list= $projectDir/<filename>". toString ()}}

Above is the modified Gradle, where <filename> is the file name of a text file, which is stored in the same level of file directory as this gradle script.

The content of this text file is as follows. The real thing is to list the classes that need to be put in main Dex.

android/support/multidex/buildconfig/classAndroid/support/multidex/multidex$v14/classAndroid/support/multidex/multidex$v19/classAndroid/support/multidex/multidex$v4/classAndroid/support/multidex/multidex/classAndroid/support/multidex/multidexapplication/classAndroid/support/multidex/multidexextractor$1/classAndroid/support/multidex/multidexextractor/classAndroid/support/multidex/ziputil$centraldirectory/classAndroid/support/multidex/ziputil/class

2. If using other lib, make sure the Lib is not predex, otherwise the following exception may be thrown

Unexpected top-levelEXCEPTION:    com.android.dex.DexException:Library dex filesis not supported in multi- dex mode at        Com.android.dx.command.dexer.Main.runMultiDex (main.java:337)        at Com.android.dx.command.dexer.Main.run (Main.java:243) at        Com.android.dx.command.dexer.Main.main ( Main.java:214) at        com.android.dx.command.Main.main (Main.java:106)

Encounter this exception, need to modify in Gradle, let it do not predexing to Lib

Android {//  ...     dexoptions {        false    }}

3. If you open the Multidex compiled version every time, it will take more time than usual (this is easy to understand, after all, did a lot of things)

The official Android document also gives us a small suggestion to build two flavor using Gradle. A minsdkversion is set to 21, which is in the DEX format supported by Art, Avoid the overhead of multidex. And the other flavor is the smallest sdkversion that was originally supported. Usually the development Time debugging program, with the former flavor, release version packaging with the latter flavor.

Android {productflavors {//Define separate dev and prod product flavors.Dev {//Dev utilizes minsdkversion = $ to allow the Android gradle plugin//To pre-dex each module and produce a APK that can is tested on//Android Lollipop without time consuming Dex merging processes.Minsdkversion 21} PROD {//The actual minsdkversion for the application.Minsdkversion 14}} ... buildtypes {release {Runproguardtrueproguardfiles Getdefaultproguardfile (' Proguard-android.txt '),                                                 ' Proguard-rules.pro '}}}dependencies {compile' com.android.support:multidex:1.0.0 '}

With Android's official support, Multidex is a lot simpler than the original solution. Mom doesn't have to worry anymore. We're going to split Dex!

Android Division Dex (MULTIDEX)

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.