Various abnormalities of 64k
When your application and library references reach a certain scale, you encounter a build error that shows that your application has reached the limit of an Android application building 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
or
UNEXPECTED TOP-LEVEL EXCEPTION:
: method ID not in [0, 0xffff]: 65536
at $(:501)
at $(:282)
at (:490)
at (:167)
at (:188)
at (:439)
at (:287)
at (:230)
at (:199)
at (:103)
The latest version of Android build system shows a different error, but the same problem:
trouble writing output: Too many field references: 131000; max is 65536. You may try using --multi-dex option.
or
Error:The number of method references in a .dex file cannot exceed 64K.
The error above shows a common number: 65536. This number is important, and it represents the total number of references that can be executable (Dex) bytecode files in a single call. If this error occurs in your Android application, congratulations, your code has reached a certain amount! This article explains how to resolve this limitation and continue building the application.
About 64k citation restrictions
Android Applications (APKs) contain executable bytecode file (DEX) files in the form of Dalvik executables that contain compiled code to run your application. Dalvik executable specification limits a Dex file containing 65536 methods: including Android framework methods, Library methods, and your own code methods. Because 65536 equals 64×1024, this limit is called "64k reference limit".
This limit requires us to configure the application construction process, which requires generation of multiple DEX files, so it is called multidex configuration.
Analysis of causes and precautions
How to do Android 5.0 and above systems and 5.0 or below systems. Guests, please don’t worry. Let’s see me to analyze the reasons one by one. After all, I have to pretend to be a slut.
1. Android 5.0 or below
Systems before Android 5.0 (API leve 21) used Dalvik to execute application code. By default, Dalvik limits one apk to only one Dex file. To bypass this limitation, we can use the multidex support library, which becomes part of our APK's main DEX file, 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 AndroidStudio's instant run when running on Android 4.4 (API leve 20) or lower.
2. Android 5.0 and higher
Android 5.0 (API leve 21) and higher systems use runtime is ART, natively supports loading multiple DEX files from the app's apk files. ART precompils the application when installing the application and scans multiple classes(..N).dex files to compile into a .oat file. For more information about Android 5.0 runtime, see Instant-run.
Note: If you use instant run, AndroidStudio automatically configures your application, 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 limit
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 relationship of your APP: Sometimes when we use several methods or functions of a Libaray, this library is very large, and reducing this dependency may be very effective in avoiding 64k problems.
•When formally packaged and build, use the code obfuscation ProGuard to obfuscate and remove unused code, that is, do not package unused code into our apk.
Using the above method can help us avoid generating too many useless methods in the application and reducing the size of our apk, which is very helpful for students who use their own server to update and upgrade apps.
Here I recommend the plug-in development framework for next student Ren Yugang:/singwhatiwanna/dynamic-load-apk, Jointly participated developers: Tian Xiao, Song Siyu.
Solve 64k problems
Use Android plugin gradle in Android SDK Build Tools 21.1 or later build tools. 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.
first step, modify the file of the main module
Rely on multidex in gradle and enable multiDexEnable:
android { compileSdkVersion 21 buildToolsVersion defaultConfig { ... minSdkVersion 14 targetSdkVersion 21 ... // Enabling multidex support. multiDexEnabled true } ... } dependencies { compile ':multidex:1.0.1' }
Step 2, inheritance class
Two situations
In the first case, if our APP has not rewrite the Application class, we can directly inherit the MultiDexApplication and register the Application in it.
In the second case, if we have rewrite the Application class, rewrite the attachBaseContext(Context) method, and call (this);
protected void attachBaseContext(Context base) { (base); (this);
Because I have flipped through the source code of MultiDexApplication, I just rewrite this method haha:
public class MultiDexApplication extends Application { public MultiDexApplication() { } protected void attachBaseContext(Context base) { (base); (this); } }
Register for Application
<?xml version= encoding=?> <manifest xmlns:android= package=> <application ... android:name="The full class name of the Application I just rewritten"> ... </application> </manifest>
Some limitations of multidex library
• The process of installing the DEX file to the device is very complicated. If the second DEX file is too large, it may cause the application to be unresponsive. At this time, you should use ProGuard to reduce the size of the DEX file.
• Due to the bug in Dalvik linearAlloc, the application may not be able to start in versions before Android 4.0. If your application wants to support these versions, you need to perform more tests.
• Also due to the limitations 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 called dexopt to prepare the application to run in the current model. dexopt uses LinearAlloc to store application method information. Android 2.2 and 2.3 have only 5MB buffers, and Android has increased to 8MB or 16MB. When too many methods cause the buffer size to exceed the size, dexopt will crash.
•The Multidex build tool does not yet support specifying which classes must be included in the first DEX file, which may cause some class libraries (such as a class library that requires access to Java code from native code) to be unusable.
Build optimization after using multidex
1. Therefore, if the application contains lirary projects, the following error may occur:
UNEXPECTED TOP-LEVEL EXCEPTION:
: Library dex files are not supported in multi-dex mode
At this time, we need to disable precompilation:
android { ... dexOptions { preDexLibraries = false } ... }
2. If you encounter the following error during operation:
UNEXPECTED TOP-LEVEL ERROR:
: Java heap space
We need to increase the size of the java heap memory:
maxProcessCount 4 // this is the default value
javaMaxHeapSize "2g"
3. Improve operation speed
In Android leve 21 or higher SDK version. Generating multidex output using the ART-supported format is faster, saving us time, so we don’t have to be compatible with debugging and below 5.0, so when we configure the lowest version, we make the following compatible:
android { productFlavors { // Customize preferences. dev { // Compile faster in Android leve 21 or higher minSdkVersion 21 } prod { // A real production environment. minSdkVersion 14 } } ... } dependencies { compile ':multidex:1.0.1' }
What to do if the Android plugin Gradle version is lower than 1.1
You need to add the following dependency multidex-instrumentation:
dependencies { androidTestCompile(':multidex-instrumentation:1.0.1') { exclude group: '', module: 'multidex' } }
Eclipse Jar package download:http://xiazai./201609/yuanma/androidsupportmultidex().rar
Official reference documents:/tools/building/
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.