Multidex principle Analysis and Noclassdeffounderror solution of Android 65K problem

Source: Internet
Author: User


Android 65K problem believe that a lot of people confused, although as the out can be through the division Dex High-speed solution 65K problem, but also easy because some code is not packaged into the maindex caused byNoclassdeffounderror。

With the launch of 5.0, Android also released the Multidex Support library to solve the problem.

The Multidex Support Library is able to directly subcontract 65K issues. And there will be no noclassdeffounderror of the situation.
1. Use the words. First join the dependent library://Subcontracting multidexenabled must join the dependency
Compile' com.android.support:multidex:1.0.1 '

2. Turn on the multidex switch: buildtypes {
Release {
minifyenabledfalse
        //Sub-package
        multidexenabledtrue
       ProguardfilesGetdefaultproguardfile(' Proguard-android.txt '),' Proguard-rules.pro '
   }
}

3. The Java heap memory error may be reported at this time, so it is best to join: dexoptions {
javamaxheapsize"4g"
    Incrementaltrue
}

4. If you have your own application, change the application to inherit the multidexapplication:Public classMyApplicationextendsmultidexapplication {

...
}

Let's say your application is unfortunate enough to have inherited the other application, leading to the inability to inherit multidexapplication. It is also possible to just replicate the method and add the code:@Override
protected voidAttachbasecontext(Context Base) {
Super. Attachbasecontext (Base);
   Multidex.Install(base);
}
at this point you will be able to run, in detail to see the official documents, after all, the official documents are clearly written.


/span> Below we mainly Multidex implementation method < Span style= "Color:rgb (68,68,68)", the following part of the source: http://blog.waynell.com/2015/04/19/ Analysis of android-multidex/ .
multidex
multidex is implemented by the principle that the class is Translated into different classes.dex files. Under normal circumstances. An APK file contains only one Classes.dex file.

After subcontracting there is a master classes.dex, multiple pairs of classes2.dex,classes3.dex ... When you want to start a program, Android will first be loaded into the main classes.dex. Then, after the program starts, it loads the other Vice's Dex. Which class should be compiled into the classes.dex of the Lord? The
First looks at the Multidex compilation process, which consists of three different gradle tasks:

1
Collect{variant}multidexcomponents Task

buidl/intermediates/multi-dex/${variant.dirname}/manifest_keep.txt

1
Shrink{variant}multidexcomponents Task

buidl/intermediates/multi-dex/${variant.dirname}/componentclasses.jar

1
Create{variant}maindexclasslist Task

This task will be based on the Componentclasses.jar generated in the previous step to find the class that is dependent on each class literal in the class, for example, there is a member variable X. Then x is the dependent Class,componentclasses.jar all class and dependent class path will be written to the file, the buidl/intermediates/multi-dex/${variant.dirName}/maindexlist.txt class will be compiled into the main classes.dex. (See Classreferencelistbuilder Implementation source code for details)

Noclassdeffounderror

Multidex is good, no longer for the number of methods more than 65536 and distressed.

But sometimes it often brings unexpected bugs. For example Noclassdeffounderror. I've had this problem in the project before. A startup program is crash, looking at log because a class cannot find the cause.


Through the above analysis, we have learned the principle of multidex, so to solve a startup program on the Noclassdeffounderror problem only need to determine whether the class is correctly compiled into the main classes.dex. If not, just change the Maindexlist.txt file. You can just add this class. Because maindexlist.txt this file is generated on its own initiative each time it is compiled, it is useless to manually change it. So we can add a new task to the Gradle compilation, create{variant}MainDexClassListAfter this task is finished, change the Maindexlist.txt file to add the missing class.

Multidex principle Analysis and Noclassdeffounderror solution of Android 65K problem

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.