Android studio uses Android support multidex to solve various 64K exceptions

Various exceptions of 64K

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

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

perhaps

UNEXPECTED TOP-LEVEL EXCEPTION: java. lang.IllegalArgumentException: method ID not in [0,0xffff]: 65536 at com. android. dx. merge. DexMerger$6. updateIndex(DexMerger.java:501) at com. android. dx. merge. DexMerger$IdMerger. mergeSorted(DexMerger.java:282) at com. android. dx. merge. DexMerger. mergeMethodIds(DexMerger.java:490) at com. android. dx. merge. DexMerger. mergeDexes(DexMerger.java:167) at com. android. dx. merge. DexMerger. merge(DexMerger.java:188) at com. android. dx. command. dexer. Main. mergeLibraryDexBuffers(Main.java:439) at com. android. dx. command. dexer. Main. runMonoDex(Main.java:287) at com. android. dx. command. dexer. Main. run(Main.java:230) at com. android. dx. command. dexer. Main. main(Main.java:199) at com. android. dx. command. Main. main(Main.java:103)

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

perhaps

Error:The number of method references in a . dex file cannot exceed 64K.

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

About 64 K reference restrictions

Android application (APK) contains an executable bytecode file (DEX) file in the form of Dalvik executable file, which contains compiled code to run your application. Dalvik executable specification limits that a DEX file contains 65536 methods: including the total number of Android framework methods, library methods, and your own code methods. Because 65536 equals 64 × 1024, this restriction is called "64K reference restriction". This limit requires us to configure the construction process of the application and generate multiple DEX files, so it is called multidex configuration.

Analyze causes and precautions

The solutions are divided into Android 5.0 and above systems and systems below 5.0. My guest, don't worry. First look at me and analyze the reasons one by one. After all, I want to install the force ha ha.

1、 Android versions below 5.0

Systems before Android 5.0 (API leave 21) used Dalvik to execute application code. By default, Dalvik limits an APK to only one DEX file. In order to bypass this restriction, we can use the multidex support library, which becomes a part of our APK's main DEX files and is responsible for managing our APK's access to other DEX files and code.

Note: if our project minsdkversion is 20 or lower, you need to disable the immediate running of Android studio when running on Android 4.4 (API leve 20) or lower devices

2、 Android 5.0 and later

Android 5.0 (API leve 21) and higher systems use runtime as art, and natively support loading multiple DEX files from the APK file of the application. Art precompiles the application when installing the application and scans multiple classes (.. n) The DEX file is compiled into a Oat file. More Android 5 For more information about the 0 runtime, see instant run.

Note: if you use instant run, Android studio automatically configures your application, and your application's minsdkversion should be set to 21 or higher. Because instant only works on the debug version of your app, you still need to configure your release version to use multidex to avoid the 64K limit.

Try to avoid 64K restrictions

Before configuring our app to enable 64K or more method references, we can reduce the total number of scheduling in the application code, including our own application methods and third-party libraries. The following strategies may help you:

• check the direct and indirect over dependence of your app: sometimes when we use some methods or functions of libaray, the library is very large. Reducing this dependence may be very effective to avoid 64K problems. • During the formal packaging and construction, the code obfuscator Proguard is used to remove the unused code, that is, the unused code is not packaged into our APK.

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

Here we recommend the plug-in development framework of the next student Yugang: https://github.com/singwhatiwanna/dynamic-load-apk , CO participating developers: Tian Xiao, song Siyu.

Solve 64K problems

Use Android plugin gradle in the build tool of Android SDK build tools 21.1 or later. Make sure you update the 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.exe of the main module Gradle file

Rely on multidex in gradle and enable multidexenable:

Step 2: inherit Android support. multidex. Multidexapplication class

Two cases

In the first case, if our app has not rewritten the application class, we directly inherit the multidexapplication, and then in the manifest Just register application in XML.

In the second case, if we have overridden the application class, override the attachbasecontext (context) method and call multidex install(this); You can:

Because I went through the source code of multidexapplication and just rewritten this method. Ha ha:

Register application

Some limitations of the multidex Library

• the process of installing the DEX file into the device is very complex. If the second DEX file is too large, the application may not respond. In this case, you should use Proguard to reduce the size of the DEX file. • Due to the bug of Dalvik linearalloc, the app may not start before Android 4.0. If your app wants to support these versions, you need to perform more tests. • Similarly, due to the limitation of Dalvik linearalloc, if a large amount of memory is requested, it may cause a crash. Dalvik linearalloc is a fixed size buffer. During the installation of the application, the system will run a program named dexopt to prepare the application for running in the current model. Dexopt uses linearalloc to store application method information. The buffer of Android 2.2 and 2.3 is only 5MB, and Android 4.4 X increased to 8MB or 16MB. When the buffer size is exceeded due to too many methods, dexopt will crash. • The multidex build tool does not support specifying which classes must be included in the first DEX file, so some class libraries (such as a class library that needs to access java code from native code) may not be available.

Build optimization after using multidex

1、 Therefore, if the lirary project is included in the application, the following errors may occur:

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

At this time, we need to disable precompiling:

2、 If the following errors are encountered during operation:

UNEXPECTED TOP-LEVEL ERROR: java. lang.OutOfMemoryError: Java heap space

We need to increase the Java heap memory size:

maxProcessCount 4 // this is the default value javaMaxHeapSize "2g"

3、 Increase running speed

On Android leave 21 or later SDK version. Using art supported format to generate multidex output is faster and saves us time, so we don't have to be compatible with less than 5.0 when debugging, so we make the following compatibility when configuring the minimum version:

What if the Android plugin gradle version is lower than 1.1

You need to add the following dependent multidex instrumentation:

Eclipse jar package download: http://xiazai.jb51.net/201609/yuanma/androidsupportmultidex (jb51.net). rar

Official reference documents: https://developer.android.com/tools/building/multidex.html

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>