The application and principles of the Multidex framework in Android development
The application and principles of the Multidex framework in Android development
During the development of Android, sometimes applications have failed to build failure due to excessive methods.This is because the Android platform has limited the number of methods in the Dalvik virtual machine. The number of methods contained in an application exceeds 6,5536, which will cause the construction failure.To solve this problem, Google has launched a framework called Multidex that allows developers to use multiple DEX files in one application.
Application of the Multidex framework
The MULTIDEX framework is mainly used to solve the problem of excessive number of solutions, allowing the code of the application to disperse the code into multiple DEX files.When the number of methods of the application exceeds 6,5536, the code of the application can be split by using the Multidex framework, put some code in the main DEX, and place the remaining code in the additional DEX file.In this way, the construction tool of the application will automatically handle the loading and running of the DEX file.
Before using the Multidex framework, developers need to configure the corresponding configuration in the application built.gradle file.First, you need to add dependencies to introduce Multidex libraries:
dependencies {
implementation 'com.android.support:multidex:1.0.3'
}
Then, in the Application class of the application, you need to rewrite the AttachbaseContext method and call the Multidex.install method:
public class MyApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
Finally, replaced the Application class of the application into a custom Application class in the Manifest file:
<application
android:name=".MyApplication"
...
</application>
Through the above steps, Multidex can be applied to the application.
Principle of Multidex framework
The working principle of the Multidex framework is realized by dynamically loading class during runtime.When a certain class needs to be loaded, the Multidex framework will be found from the main DEX file. If you can't find it, you will find other DEX files in turn until you find the required class.In this way, even if the number of methods of the application exceeds the limit of the Dalvik virtual machine, it can be loaded and run normally.
The implementation principle of the Multidex framework can be simply summarized as the following steps:
1. When the application starts, the Multidex framework will detect whether the current device supports the loading of multiple DEX files.If it is not supported, it will throw an abnormality and terminate the start of the application.
2. If the device supports the loading of multiple DEX files, the Multidex framework will be loaded from the main dex file.The main dex file refers to the first dex file generated during the application compilation process.
3. When a certain class needs to be loaded, the Multidex framework will be found in the main name of the class according to the full name of the class.If you can't find it, you will find other DEX files in order until you find the required class.
4. The Multidex framework will calm down the loaded class so that the next time it is loaded, it will be obtained directly from the cache.This can improve the loading speed of the class.
Summarize
The Multidex framework is a solution launched to solve the problem of excessive number of Android applications.By using the Multidex framework, developers can use multiple dev files in one application to overcome the number of methods for the Dalvik virtual machine method.Through the above application and principles, developers can better understand and apply the Multidex framework to improve the construction capacity and performance of Android applications.
Reference materials:
- [https://developer.android.com/studio/build/multidex](https://developer.android.com/studio/build/multidex)