Android applications limit the number of 65 k methods, and android applications limit the number of 65 k

Source: Internet
Author: User
Tags android sdk manager

Android applications limit the number of 65 k methods, and android applications limit the number of 65 k

Recently, Android Developers announced a new Multidex support library on Google +, providing official support for Android applications whose total number of methods exceeds K.

If you are a lucky Android application developer who is developing a promising application, constantly adding new functions and adding new class libraries, one day, you will unfortunately encounter this error:

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

This error is caused by the total number of methods in the Android app. The Java Virtual Machine Dalvik on the Android platform uses the native short to index the methods in DEX files when executing Java applications in DEX format. This means that the total number of methods that can be referenced by a single DEX file is limited to 65536. Generally, an APK contains a classes. dex file. Therefore, the total number of methods of an Android application cannot exceed this value. This includes the Android framework, class library, and self-developed code.

This problem can be solved by splitting a DEX file into multiple DEX files. Facebook introduced the Dalvik patch for Android apps. The Android Developers blog introduced how to solve this problem through the custom class loading process. However, these methods are complex and not elegant.

With the release of the new MultiDex support library, Google officially provides official support to solve this problem. This document describes how to use Gradle to build a multi-DEX application.

First, upgrade Android SDK Manager to the latest Android SDK Build Tools and Android Support Library R21. Perform the following two steps:

1. Modify the Gradle configuration file, enable MultiDex, and include MultiDex support:

Android {compileSdkVersion 21 buildToolsVersion "21.1.0"

DefaultConfig {

...

MinSdkVersion 14

TargetSdkVersion 21

...

// Enabling multidex support.

MultiDexEnabled true

}

...

}

Dependencies {compile 'com. android. support: multidex: 1.0.0 '}

2. Allow the application to support multiple DEX files. Three optional methods are described in MultiDexApplication JavaDoc:

Declare android. support. multidex. MultiDexApplication in the application of AndroidManifest. xml;

If you already have your own Application class, let it inherit the MultiDexApplication;

If your Application class has inherited from other classes and you do not want to modify it, You can override the attachBaseContext () method:

@ Override

Protected void attachBaseContext (Context base ){

Super. attachBaseContext (base); MultiDex. install (this );

}

After the preceding steps, your application can implement multiple DEX files. When an application is built, the build tool analyzes which classes must be placed in the first DEX file and which classes can be placed in the attached DEX file. When it creates the first DEX file (classes. dex), if necessary, it will continue to create additional DEX files, such as classes2.dex and classes3.dex. The Multidex Support class library will be included in the first DEX file of the application to help access other DEX files.

This article also introduces how to improve development efficiency and test multiple DEX applications by setting productFlavors.

Android 5.0 and later versions use a runtime named ART, which supports loading multiple DEX files from the APK file. During application installation, it will execute pre-compilation, scan the classes (. N). dex file, and compile it into a single. oat file for execution. Understanding

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.