4.3, Android Studio breaks through the 64K method limit

When the application code and library code exceed the 64K limit, the earlier version of the build system will display the following prompt:

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

The latest version of the build system will appear as follows, indicating the same error:

trouble writing output:
Too many field references: 131000; max is 65536.
You may try using --multi-dex option.

In fact, they are all the same problem, but the prompt method is different.

Regarding the 64K limit, the
APK file contains an executable bytecode file composed of DEX files, including the compiled code for running the app. DEX files have a limit of 65535 methods. If this limit is exceeded, an error will be reported during construction. These methods include Android Framework methods, library methods and methods written by yourself. Because 65535=64*1024, this limit is also called the 64K limit.

Previous Android5.0 version supports multi-dex
before Android5.0, to execute application code that uses Dalvik runtime. By default, in each APK, Dalvik limits one classes.dex bytecode file. In order to overcome this limitation, you can use the multidex support library.
Note: If your project is configured with a multi-dex minimum SDK version of 20 or lower, Android Stuido will disable Instant Run.

Multidex of Android5.0 and above supports
Android5.0 (API level 21) or higher, and it executes application code when using ART runtime. Natively supports loading multiple dex files directly from APK files.

Avoid the 64K limit
The following methods can help you reduce restrictions dex methods:
1, viewing directly or indirectly dependent on your app are:
minimizing unnecessary references dependent.
2. Reduce unused code
through ProGuard: Enable ProGuard by configuring ProGuard to ensure that you enable compression when you release the version.

Use Gradle to configure the multidex of your application.
In Android SDK Build Toos 21.1 or higher, Android's Gradle plugin provides multidex support in your build configuration. Before attempting to configure your multidex, make sure your Android SDK Build Tools and Android Support Repository are upgraded to the latest version.

To set up your development app to use multidex, you need to make some changes to your development project, follow the steps below:
1. Change your Gradle build configuration to enable multidex.
2. Change your manifest to add the MultiDexApplication class.

Change your build.gradle file configuration to add support libraries and enable multidex output, as follows:

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'
}
在AndroidManifest.xml中,添加MultiDexApplication类:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.multidex.myapplication">
    <application
        ...
        android:name="android.support.multidex.MultiDexApplication">
        ...
    </application>
</manifest>

When you add the above configuration to your app, the Android build tool generates a main dex file (classes.dex) and supports multiple (classes2.dex, classes3.dex). The build system packages them into an APK for release.

Limitations of the
Multidex Support Library The Multidex support library has some limitations that you need to understand:
1. Under certain circumstances, it may cause ANR problems, if the second dex file is too large. To prevent this problem from happening, you need to use ProGuard to compress.
2. There will be some problems in the very early versions of Android 4.0.
3. The applied multidex configuration will require a larger memory allocation, which will cause a crash in the Dalvik virtual machine.
4. Other complicated situations.

Optimizing Multidex development and building
Multidex configuration will significantly increase the build time, because the build system must measure which classes are placed in the Dex file and those classes are placed in the second dex file. This means that it will slow down the development process.

In order to effectively slow down the time to build multidex output, you need to use the Android plug-in to create two variants:
a development flavor and a production flavor.

as follows:

android {
    productFlavors {
        // Define separate dev and prod product flavors.
        dev {
            // dev utilizes minSDKVersion = 21 to allow the Android gradle plugin
            // to pre-dex each module and produce an APK that can be tested on
            // Android Lollipop without time consuming dex merging processes.
            minSdkVersion 21
        }
        prod {
            // The actual minSdkVersion for the application.
            minSdkVersion 14
        }
    }
          ...
    buildTypes {
        release {
            runProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                                                 'proguard-rules.pro'
        }
    }
}
dependencies {
  compile 'com.android.support:multidex:1.0.0'
}

Author: Song Zhihui
personal micro-blog: Click to enter

Guess you like

Origin blog.csdn.net/song19891121/article/details/51782040