How to solve the problem of Multidex framework conflict in the Java class library
How to solve the problem of Multidex framework conflict in the Java class library
Overview:
Multidex is a class library used to handle the number of method restrictions in the Android application package.When the application contains too much method, there will be errors in the number of methods.To solve this problem, Google provides Multidex framework.However, when using the Java library, the conflict between the Multidex framework and other libraries may occur.This article will introduce how to solve the problem of the Multidex framework in the Java library and provide the corresponding Java code example.
solution:
1. Conflict on using Google Play Services Library
Google Play Services Library often conflicts with the Multidex framework.In order to solve this problem, you can follow the steps below:
-In the APP's Build. Gradle file, add the following dependencies:
implementation 'com.android.support:multidex:1.0.3'
-In the application class, inherit the `MultidexApplication`, and call the method of` multidex.install (this).For example:
public class MyApp extends MultiDexApplication {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
-Ann the name of the application class in the manifest file:
<application
android:name=".MyApp"
...
</application>
-At conflict, you can try the module that eliminate the conflict from the Google Play Services library.For example, add in the build.gradle file:
implementation ('com.google.android.gms:play-services-maps:17.0.0') {
exclude group: 'com.android.support', module: 'multidex'
}
2. Use the conflict of the third -party library
If a third -party library conflicts with the Multidex framework, you can try the following solutions:
-In the APP's Build. Gradle file, add the following dependencies:
implementation 'com.android.support:multidex:1.0.3'
-In the application class, inherit the `MultidexApplication`, and call the method of` multidex.install (this).
-Doamed the name of the application class in the Manifest file.
-If if there is still a conflict, you can try the module that eliminate the conflict from the third party library.
3. Custom conflict solution
If the above method cannot solve the conflict problem, you can try to customize the solution.The following is an example:
-Colon a `mymultidexinstaller` class, inherit the` multidexinstaller`, and rewrite the `Install` method.
public class MyMultiDexInstaller extends MultiDexInstaller {
@Override
protected void install(Context context) {
// Execute custom solutions
// ...
// Call the basic Multidex.Install method
super.install(context);
}
}
-In the application class, inherit the custom `MultidexApplication` class, and call the custom` mymultidexinstaller`.
-S update the name of the application class in the Manifest file.
<application
android:name=".MyApp"
...
</application>
Summarize:
By adding Multidex dependencies and inherit the `MultidexApplication` class, you can solve the problem of conflict between the Multidex framework in the Java library.If you encounter conflicts with Google Play Services or third -party libraries, you can try to solve the problem by excluding conflict modules or custom solutions.Please select the appropriate solution according to the specific situation, and customize the corresponding solution according to the needs.
How to solve some suggestions and example code of the Multidex framework conflict in the Java library.I hope this article can help you solve related issues.