Androidstudio using Android-support-multidex to solve 65536 problem 64k problem

Source: Internet
Author: User

Androidstudio using Android-support-multidex to solve 65536 problem 64k problem

Copyright NOTICE: Reprint Please specify this article to the blog from Zhangjie: http://blog.csdn.net/yanzhenjie1003

64k of various anomalies

Error:the number of method references in a. dex file cannot exceed 64K.
Learn how to resolve the issue at https://developer.android.com/tools/building/multidex.html

Or

Finished with Non-zero exit value 2

When your application and library references reach a certain scale, you encounter build errors that show that your app has reached the limits of an Android application building architecture. Earlier versions of the build system reported this error as follows:

to Dalvik format failed:Unable to execute dex: method ID not in [0, 0xffff]: 65536
    • 1
    • 2

Or

Unexpected top-level EXCEPTION:Java.lang.IllegalArgumentException:method ID notin [0,0XFFFF]:65536 atCom. 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.run (Main.java: At com.android.command.dexer .main (Main.java: 199) at com.android.command.main (Main.java: 103)                 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

The latest version of the Android build system shows a different error, but it is the same problem:

131000; max is 65536.You may try using --multi-dex option.
    • 1
    • 2
    • 3

Or

of method references in a .dex file cannot exceed 64K.
    • 1

The above error shows a common number: 65536. This number is important and it represents the total number of references that can be called in a single code Dalvik executable (DEX) bytecode file. If this error happens to your Android app, congratulations, your code has reached a certain amount! This article explains how to resolve this limitation and continue building the application.

About the K reference limit

The Android app (APK) in the form of the Dalvik executable contains executable bytecode file (DEX) files that contain compiled code to run your application. Dalvik executable specifications Restricting a Dex file contains 65,536 methods: including the Android framework method, the total number of library methods, and the total number of your own code methods. Since 65536 equals 64x1024, this restriction is referred to as the "64k reference limit".
This limit requires us to configure the application build process and to generate multiple Dex files, so it is called a multidex configuration.

Analysis reasons and Precautions

Solution for Android 5.0 and above system and 5.0 below system how to do. Sir do not worry, first look at my analysis of reasons, after all, I want to install forced haha.

One, Android version 5.0 or less

Systems prior to Android 5.0 (API leve 21) Use Dalvik to execute application code. By default, Dalvik restricts an apk to only one Dex file. To circumvent this limitation, we can use the Multidex support library, which becomes part of our apk's main dex file and is responsible for managing our APK access to other Dex files and code.

Note: If our project minsdkversion is 20 or lower, you need to disable Androidstudio on-the-fly when running to Android 4.4 (API leve 20) or a lower version of the device

Second, Android 5.0 and later

Android 5.0 (API leve 21) and a higher system using runtime are art, native support for loading multiple Dex files from an app apk file. Art pre-compiles applications when installing an app, and scans multiple classes (.. N). dex files are compiled into a. oat file. For more information on Android5.0 runtime, see Running-instant-run on the fly.

Note: If you use instant running, Androidstudio automatically configures your application, your application's minsdkversion should be set to 21 or higher. Since the instant only works in the debug version of your app, you will need to configure your release version to use Multidex to avoid the 64k limit.

Try to avoid 64k limitations

Before we configure our app to enable 64k or more method references, we can reduce the total number of schedules in our app code, including our own methods of application and third-party libraries, and some of the following strategies may help you:

  • Check your app for direct and indirect over-reliance: sometimes we use a certain number of methods or functions of a libaray, the library is very large, and reducing this dependency can be very effective in avoiding 64k problems.
  • In a formal package build, use the code obfuscation Proguard to remove unused code, that is, not to package unused code into our apk.

Using the above method can help us avoid generating too many useless methods in the application and reduce the size of our apk, which is very helpful for the students to upgrade their own server to update the app.

Here to recommend the next Ningyugang classmate's plug-in development framework: HTTPS://GITHUB.COM/SINGWHATIWANNA/DYNAMIC-LOAD-APK, co-developer: Tian Xiao, Song Siyu.

Solve 64k problems

Used in or later build Android SDK Build Tools 21.1 tools Android plugin gradle . Make sure that you update Android SDK build tools and Android support to the latest version, and then configure the application with Multidex. We have to do two steps.

The first step is to modify the Build.gradle file of the main module

Dependent in Gradle multidex , and enabled multiDexEnable :

android {    21    buildToolsVersion     defaultConfig {        ...        minSdkVersion 14        targetSdkVersion 21        ... // Enabling multidex support. multiDexEnabled true } ...}dependencies { compile ‘com.android.support:multidex:1.0.1‘}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
Step two, inherit android.support.multidex.MultiDexApplicationTwo types of cases

In the first case, if our app has not rewritten the application class, we inherit directly and MultiDexApplication then register the application in Manifest.xml.

In the second case, if we have rewritten the application class, override attachBaseContext(Context) the method and call it MultiDex.install(this); :

protected void attachBaseContext(Context base) {super.attachBaseContext(base);MultiDex.install(this);
    • 1
    • 2
    • 3

Because I have turned over MultiDexApplication the source, the inside is rewriting this method just haha:

public class MultiDexApplication extends Application { public MultiDexApplication() { } protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); }}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
Register Application
<?xml version= encoding=?><manifest xmlns:android=    package=> <application ... android:name="刚才重写的Application全类名"> ... </application></manifest>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
Some limiting factors of Multidex library
    • Dex files are very complex to install to a device, and if the second Dex file is too large, it can cause the app to be unresponsive. You should use Proguard to reduce the size of the Dex file.
    • Due to Dalvik linearalloc bugs, apps may not start in versions prior to Android 4.0, and if your app wants to support these versions, it will run more tests.
    • Also because of the limitations of Dalvik Linearalloc, requests for large amounts of memory can cause crashes. Dalvik Linearalloc is a fixed-size buffer. During the installation of the app, a program called Dexopt is run to prepare the app for running in the current model. Dexopt uses Linearalloc to store the application's method information. Android 2.2 and 2.3 buffers only 5mb,android 4.x increased to 8MB or 16MB. Dexopt crashes when too many methods result in exceeding the buffer size.
    • The Multidex build tool also does not support specifying which classes must be included in the first Dex file, so it is possible that some class libraries (such as a class library that needs to access Java code from native code) cannot be used.
Build optimization after using Multidex

One, so if the application contains Lirary engineering, the following error may occur:

UNEXPECTED TOP-LEVEL EXCEPTION:  com.android.dex.DexException: Library dex files are not supported in multi-dex mode 
    • 1
    • 2

At this point we need to disable precompilation:

android {      ...      dexOptions {          preDexLibraries = false      }      ...}  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

Second, if you encounter the following error when running:

ERROR:  java.lang.OutOfMemoryError: Java heap space 
    • 1
    • 2

We need to increase the Java heap memory size:

4 // this is the default valuejavaMaxHeapSize "2g"
    • 1
    • 2

Third, improve the speed of operation
In Android Leve 21 or higher SDK version. Generating Multidex output using the art-supported format is faster and saves us time, so we don't have to be compatible with debugging at 5.0, so we'll do the following when we configure the minimum version:

android {    productFlavors {        // 自定义偏好设置.        dev {            21或更高版本编译更快            minSdkVersion 21        }        prod {            // 真正的生产环境.            minSdkVersion 14        }    }    ...}dependencies {  compile ‘com.android.support:multidex:1.0.1‘}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
Android plugin Gradle version less than 1.1 what to do

You need to add the following dependency multidex-instrumentation:

dependencies {    androidTestCompile(‘com.android.support:multidex-instrumentation:1.0.1‘) {         exclude group: ‘com.android.support‘, module: ‘multidex‘    }}
    • 1
    • 2
    • 3
    • 4
    • 5

Eclipse jar Pack Download: http://download.csdn.net/detail/yanzhenjie1003/9566518
Official Reference Document: Https://developer.android.com/tools/building/multidex.html

Androidstudio using Android-support-multidex to solve 65536 problem 64k 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.