Completely solve the problem of Android application method can not exceed 65K

Source: Internet
Author: User
<span id="Label3"></p><p><p></p></p><p><p>As an Android developer, I believe you should have heard about the limit of Android methods to 65K, and as the application function continues to enrich, someday you will encounter an exception:</p></p><p><p></p></p><p><p>Conversion to Dalvik format failed:unable toexecute dex:method ID not in [0, 0xffff]: 65536</p></p><p><p>Some students may say that to solve this problem is very simple, we only need to configure a sentence in the Project.proterty ok,</p></p><p><p><span lang="EN-US">Dex.force.jumbo=true<br></span></p></p><p><p><span lang="EN-US">yes, by adding this phrase, you can actually make your app compile, but it's easy to appear on some 2.3-system machines</span></p></p><p><p><span lang="EN-US"><span lang="EN-US">Install_failed_dexopt exception<br></span></span></p></p><p><p><span lang="EN-US"><span lang="EN-US"></span></span></p></p><p><p><span lang="EN-US"><span lang="EN-US">For the above two exceptions, let's start by analyzing the Reasons:</span></span></p></p><p><p><span lang="EN-US"><span lang="EN-US">1, the Android system, a Dex file stored method ID with the short type of data, so that your Dex in the method can not exceed 65k</span></span></p></p><p><p><span lang="EN-US"><span lang="EN-US">2, before the 2.3 system, the virtual machine memory is allocated only 5M</span></span></p></p><p><p><span lang="EN-US"><span lang="EN-US"></span></span></p></p><p><p>Know the reason, we have to solve the above problem, first of all, for the 65k problem, we can not change the structure of the Android system in the application layer, so we can not change the data type from short to int or other type, In other words, the number of methods in a Dex cannot exceed 65k, and we can only reduce the number of methods in one dex, and the easiest scenario is to get rid of some useless jar packages and set some properties to public so that the Get/set method is Removed. This method can only temporarily solve the problem, with the passage of time, one day there will be more than 65k of methods, after all, an application is generally in addition function, will not reduce Functionality.</p></p><p><p></p></p><p><p></p></p><p><p>Let me introduce you to the two main solutions, one is to represent, some functions into plug-ins, dynamic loading, The other is a facebook-represented subcontracting scheme, the Dex file in an APK is divided into multiple Dex files, and then dynamically load the Dex File. In fact, the core idea of the two programs is the same, the plug-in is the future to develop new features into the APK and Dex dynamic loading, and the subcontracting is to have completed the function of multiple Dex file load dynamically, in fact, I personally feel that the plug-in scheme score package solution to better solve the problem of 65k, Because the plug-in solution can not only solve the 65k problem, but also let our application volume reduce, and sub-package can only solve the problem of 65k.</p></p><p><p></p></p><p><p>On the plug-in development, made dynamic loading, I wrote in an earlier article on its basic ideas, interested students can see</p></p><p><p></p></p><p><p>"implementing android Dynamic loading apk (Fragment or activity implementation)"</p></p><p><p>http://blog.csdn.net/yuanzeyao/article/details/38565345</p></p><p><p></p></p><p><p>Below we highlight the subcontracting mechanism</p></p><p><p>We know that an APK file has a Dex file inside, the Dex file is an optimized class file, so-called subcontracting, is to say a Dex file into multiple Dex files, Here we agreed that the first Dex called main.dex, The second is called second.dex, usually at the time of subcontracting, we need to put the application to use the launch of the class into the main.dex, not immediately need to use the class into the second.dex, for the Android system, he will only load the default main.dex, second.dex to For him it might just be a resource file, it's not going to load second.dex, so I'm going to need to create a class loader for Second.dex during the app launch, so that I can load the class in Second.dex when I'm using the class.</p></p><p><p></p></p><p><p>about how to load Second.dex also have a lot of practices, with more than a few of the main</p></p><p><p>1, The simplest way is to use Dexclassloader to load, and the Dexclassloader's parent loader is set to Pathclassloader</p></p><p><p>2. Use Dexclassloader to load, and set Dexclassloader's parent loader to Pathclassloader's parent loader, set Pahtclassloader's parent loader to dexclassloader, Take a closer look at the difference between 1 and 2</p></p><p><p>3. Put the Second.dex path into the Pathclassloader load path</p></p><p><p></p></p><p><p>For the 2nd program, in one case it is not available, For example, when Second.dex is loaded by dexclassloader, but a class is used in the second.dex, this class is in main.dex, this time throws the class cannot find the exception, so this scheme can only have second.dex not use Main.dex class when</p></p><p><p></p></p><p><p>These are all theories, let's do the actual combat.</p></p><p><p>I'll show you two scenarios, one that builds Android projects based on gradle, and one that builds Android projects based on Ant</p></p><p><p>Scenario One: build Android projects based on Gradle and implement subcontracting</p></p><p><p>Environmental requirements: AndroidStudio0.9 above, Gradle plug-in 0.14.2 or more</p></p><p><p></p></p><p><p>1. If your project is in eclipse, then you need to import the project into android, you need to upgrade adt22 above</p></p><p><p>2. Open your Project's build.gradle file and check if the Gradle plugin is 0.14.2 after the Gradle plug-in is supported by the 0.14.2 after the Sub-package</p></p><p><p></p></p><p><p></p></p><p><p>3, Open the project under a moudle build.gradle file, add a dependency on the Android-support-multidex.jar</p></p><p><p></p></p><p><p>4. Remove duplicate classes from Third-party jar packages</p></p><p><p></p></p><p><p></p></p><p><p>5. Set the virtual machine heap memory space size to avoid oom during compilation</p></p><p><p></p></p><p><p></p></p><p><p>6, Gradle Construction project, It seems that the default is not to join the so library project, so in order to avoid this situation, we need to develop so library directory, for the project from eclipse, also need to develop SRC and resource file path</p></p><p><p></p></p><p><p></p></p><p><p>7. If your project relies on other libraries, add multidexenabled = True and Jnilibs.srcdirs =[' Libs '] Two configurations in each library project respectively</p></p><p><p>8, If your project does not have custom application, then you can use Multidexapplication in androidmanifest.xml, if your project has custom application, And is the inheritance is application, then only need to inherit multidexapplication can, if your project inherits the other application, then you need to rewrite</p></p><p><p>Attachbasecontext</p></p><p><p></p></p><strong><strong>[java]</strong></strong>View plaincopyprint? <ol class="dp-j" start="1"> <ol class="dp-j" start="1"> <li class="alt"><span class="annotation">@Override</span></li> <li><span class="keyword">protected <span class="keyword">void Attachbasecontext (Context Base) {</span></span></li> <li class="alt"><span class="keyword">Super.attachbasecontext (base); </span></li> <li>Multidex.install (this<span class="keyword">); </span></li> <li class="alt">}</li> </ol> </ol><p><p><br>With the above configuration, your project should have been successfully subcontracting. If the subcontracting succeeds, then you unzip your apk file and will find that there are two Dex files, through the above configuration process, We find that we have no control over which classes are in main.dex, and which classes are in second.dex, which are compatible with api4-api20 by configuring subcontracting in this scenario. it loads Second.dex using 3 of the above scenario</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Let's take a look at the ant-based build Android project, and implement the subcontracting process</p></p><p><p>In the above scenario, since we can't see the script for the Gradle build project, we can't control which classes are in the first dex, which classes are in the second dex, and in this scenario we're using ant build, ant is allowing users to define their own build scenarios, such as we can customize the build scenario, Put some third-party jar packages in the project into second.dex, see the Open Source project for information on how this is implemented</p></p><p><p>Https://github.com/mmin18/Dex65536.git</p></p><p><p>Since the project is loaded with the Scenario 2 above, for example, some third-party jar packages in Second.dex depend on some classes in the main.dex, this scenario will be implemented, so I'm going to take this scenario out of the way, and replace the Second.dex with scenario 3, which is to set the path of the Second.dex to path ClassLoader in the Load path</p></p><p><p>I only give the solution in Android 4.4, the other systems are similar</p></p><p><p>Load Second.dex method</p></p><p><p></p></p><strong><strong>[java]</strong></strong>View plaincopyprint? <ol class="dp-j" start="1"> <li class="alt"><li class="alt"><span class="comment">/**</span></li></li> <li><li><span class="comment">@param loader</span></li></li> <li class="alt"><li class="alt"><span class="comment">Pathclassloader</span></li></li> <li><li><span class="comment">@additionalClassPathEntries</span></li></li> <li class="alt"><li class="alt"><span class="comment">To be loaded by the Dex file, here is our Second.dex</span></li></li> <li><li><span class="comment">@optimizedDirectory</span></li></li> <li class="alt"><li class="alt"><span class="comment">is the directory where Dex files are extracted</span></li></li> <li><li><span class="comment">*/</span></li></li> <li class="alt"><li class="alt"><span class="keyword">private <span class="keyword">static <span class="keyword">void install (ClassLoader loader, list<file> additionalclasspathentries,</span> </span> </span></li></li> <li><li>File Optimizeddirectory)</li></li> <li class="alt"><li class="alt"><span class="keyword">throws illegalargumentexception, illegalaccessexception,</span></li></li> <li><li>nosuchfieldexception, invocationtargetexception, Nosuchmethodexception {</li></li> <li class="alt"><li class="alt">/ <span class="comment">* The patched class loader is expected to be a descendant</span> of</li></li> <li><li><span class="comment">* Dalvik.system.BaseDexClassLoader. We modify its</span></li></li> <li class="alt"><li class="alt"><span class="comment">* dalvik.system.DexPathList pathList field to append additional DEX</span></li></li> <li><li><span class="comment">* File Entries.</span></li></li> <li class="alt"><li class="alt"><span class="comment">*/</span></li></li> <li><li><span class="comment">//find The value of PathList by reflection</span></li></li> <li class="alt"><li class="alt">Field Pathlistfield = FindField (loader, <span class="string">"pathList"); </span></li></li> <li><li>Object dexpathlist = Pathlistfield.get (loader);</li></li> <li class="alt"><li class="alt">arraylist<ioexception> suppressedexceptions = <span class="keyword">new arraylist<ioexception> (); </span></li></li> <li><li><span class="comment">//add second.dex to pathclassloader's load path</span></li></li> <li class="alt"><li class="alt">Expandfieldarray (dexpathlist, <span class="string">"dexelements", makedexelements (dexpathlist,</span></li></li> <li><li><span class="keyword">new arraylist<file> (additionalclasspathentries), optimizeddirectory,</span></li></li> <li class="alt"><li class="alt">suppressedexceptions));</li></li> <li><li><span class="keyword">if (suppressedexceptions.size () > <span class="number">0) {</span> </span></li></li> <li class="alt"><li class="alt">for <span class="keyword">(ioexception E:suppressedexceptions) {</span></li></li> <li><li>LOG.W (TAG, <span class="string">"Exception in makedexelement", e); </span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li>Field Suppressedexceptionsfield =</li></li> <li class="alt"><li class="alt">FindField (loader, <span class="string">"dexelementssuppressedexceptions"); </span></li></li> <li><li>ioexception[] dexelementssuppressedexceptions =</li></li> <li class="alt"><li class="alt">(ioexception[]) Suppressedexceptionsfield.get (loader);</li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">if (dexelementssuppressedexceptions = = <span class="keyword">Null) {</span> </span></li></li> <li><li>Dexelementssuppressedexceptions =</li></li> <li class="alt"><li class="alt">Suppressedexceptions.toarray (</li></li> <li><li><span class="keyword">New Ioexception[suppressedexceptions.size ()]); </span></li></li> <li class="alt"><li class="alt">} <span class="keyword">Else {</span></li></li> <li><li>ioexception[] combined =</li></li> <li class="alt"><li class="alt"><span class="keyword">new Ioexception[suppressedexceptions.size () +</span></li></li> <li><li>dexelementssuppressedexceptions.length];</li></li> <li class="alt"><li class="alt">Suppressedexceptions.toarray (combined);</li></li> <li><li>System.arraycopy (dexelementssuppressedexceptions, <span class="number">0, combined,</span></li></li> <li class="alt"><li class="alt">Suppressedexceptions.size (), dexelementssuppressedexceptions.length);</li></li> <li><li>Dexelementssuppressedexceptions = combined;</li></li> <li class="alt"><li class="alt">}</li></li> <li><li></li></li> <li class="alt"><li class="alt">Suppressedexceptionsfield.set (loader, dexelementssuppressedexceptions);</li></li> <li><li>}</li></li> <li class="alt"><li class="alt">}</li></li> </ol><p><p></p></p><p><p>After the successful sub-package, unzip the apk file into the Assert folder, we see the following structure, libs.apk is the third party jar compiled after the Dex file</p></p><p><p></p></p><p><p>For the second problem mentioned above, the root cause is that the memory of the Dalvik virtual machine is only 5 m before the 2.3 version, so whether it is a plug-in scenario or a subcontracting scheme on some phones, the problem is Install_failed_dexopt. After all, we are only reducing the number of packages per dex, but the total number of methods is not reduced, so the fundamental way to solve this problem is to modify the virtual machine memory to 8M, this requirement is not implemented in the Java layer, but can be implemented in the C layer, the implementation process can refer to open source Projects:</p></p><p><p>Https://github.com/viilaismonster/LinearAllocFix.git</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>As for the method used in some of the methods, can be <span lang="EN-US">found in the android-support-multidex.jar, here is not all posted out, if there is no clear, welcome message to discuss</span> ...</p></p><p><p></p></p><p><p></p></p><p><p><strong>original:</strong>http://blog.csdn.net/yuanzeyao/article/details/41809423</p></p><p><p></p></p><p><p>Completely solve the problem of Android application method can not exceed 65K</p></p></span>

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.