Android apps more than 64k workaround: Split Dex

Source: Internet
Author: User
Tags gc overhead limit exceeded

Your Android project is very powerful, docking a lot of third-party open source libraries, the more complete the project, the more the code hit more and more cool. But suddenly one day the newspaper was abnormal.
Error: The number of method references in a. dex file cannot exceed 64K.
The compiler reminds you that your project has more than 64k of methods.

Androidstudio will remind you:
Learn how to resolve the issue at https://developer.android.com/tools/building/multidex.html

If you feel tired of reading English documents, then use the sub-package strategy provided by Google website (that is: Split Dex) To solve the problem of 64k limit.
But what about the eclipse compiler? It looks like you're going to use Google's official website solution to move the project to Androidstudio first.
"Maybe there's a better solution on the web, like using a plug-in framework: https://github.com/singwhatiwanna/dynamic-load-apk"

I still think Google's solutions are more reliable, so let's start.

The first step: Introducing the Multidex Library (com.android.support:multidex:1.0.1) provided by Google

Step Two: turn on multidexenabled (that is, add the attribute in Defaultconfig: multidexenabled true)

Step Three: change the project original application to Multidexapplication

There are two scenarios:

1> If your project does not rewrite the application class, you only need to change the application tag of the Androidmanifest.xml file, that is, add the attribute under the Application tab: Android:name= " Android.support.multidex.MultiDexApplication "can.

2> If your project overrides the application class, Then you need to remove your application class from the inherited Android.app.Application, inherit the Android.support.multidex.MultiDexApplication class instead, and then rewrite the Attachbasecontext (Context base), method, and initialize the Multidex. As follows:

public class MyApplication extends MultiDexApplication { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); // 初始化 }}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

This time the subcontract is finished.

Please also note: Out of memory issues

1> for projects with many dependencies, compilation may be interrupted by the following error

Error:Execution failed for task ‘:app:dexDebug‘. Error Code:3Output: UNEXPECTED TOP-LEVEL ERROR: java.lang.OutOfMemoryError: GC overhead limit exceeded at...
    • 1
    • 2
    • 3
    • 4
    • 5

Workaround:
To edit the Build.gralde file under the app, add the following properties under Android:

dexOptions {    true    javaMaxHeapSize ‘2g‘}
    • 1
    • 2
    • 3
    • 4

2> If your project has some dependent library (Module) projects, you may want to add properties to the Defaultconfig of its Build.gralde file: multidexenabled True

at this point, the subcontracting strategy is finished. Can basically solve the problem of more than 64k limit of the number of methods. But there are also some side effects, such as causing the project to start very slowly or even ANR.

Android apps more than 64k workaround: Split Dex

Related Article

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.