Detailed analysis of the Multidex framework in the Java Class Library
Detailed analysis of the Multidex framework in the Java Class Library
During the development of Android, when the number of applications exceeds the maximum limit of the DEX file (65536 methods), when the DEX file cannot be generated, we need to use the Multidex framework to solve this problem.This article will analyze the Multidex framework in the Java class library, including its principles, usage methods, and code examples.
1. The principle of the Multidex framework
When the number of applications exceeds the maximum limit of the DEX file, the compiler will report an error, and the number of methods of the prompt is more than the limit.This is because the DEX file format uses 16 digits to represent the index of the method, so the maximum can only indicate 65,536 methods.However, as the application function continues to increase, the number of methods is inevitable.To solve this problem, Google has developed the Multidex framework.
The principle of the Multidex framework is to disassemble all classes of the application into multiple dev files and use DexClassLoader to load these DEX files.In this way, the application can make full use of the maximum number of DEX files to achieve dynamic increase in the number of methods.
During the specific implementation, the Multidex framework will generate the DEX file through the compilation of the processor during the construction process.The generated DEX file will be packaged into the app's APK file.When the application starts, the Multidex framework uses DEXClassLoader to load these dex files and merge it with the main dex file into a dexpathlist to use the supply program.
2. How to use the Multidex framework
In order to use the Multidex framework, we need to configure the following configuration in the project's built.gradle file:
groovy
android {
defaultConfig {
...
multiDexEnabled true
}
...
}
dependencies {
implementation 'androidx.multidex:multidex:2.0.1'
}
Rewrite the AttachbaseContext () method in the Application class, and add the Multidex.install () method to call::
public class MyApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
...
}
After the above step configuration is complete, you can use the Multidex framework in the application.
Third, code example of the Multidex framework
Suppose we have an application that contains multiple classes, which exceeds the number of methods of the DEX file.In order to use the Multidex framework, the configuration of the project's built.gradle file is as follows:
groovy
android {
defaultConfig {
...
multiDexEnabled true
}
...
}
dependencies {
implementation 'androidx.multidex:multidex:2.0.1'
}
Then, add Multidex.install () method to the Application class: call:
public class MyApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
...
}
In this way, the configuration of the Multidex framework is completed.After that, we can write other code of the application in a normal way, and dynamically increase the number of methods during runtime.
In summary, this article detailed analysis of the Multidex framework in the Java library, including its principles, usage methods, and code examples.By using the Multidex framework, we can solve the problem of the number of applications exceeding the DEX file limit, so as to realize the dynamic increase in the number of methods.