I encountered a multiDex problem when quoting the autobahn framework. After looking for relevant information and browsing the explanations of the great gods on the Internet, I solved this problem. Record the problem solving process here.
First of all, let’s roughly talk about why this problem occurs. The android project will compile java files into class files during the compilation process, and then package the class files into dex files. By default, only one dex file is packaged during the packaging process, but a single dex file. The number of methods in the file cannot exceed 65536. When we reference a bunch of dependencies and third-party libraries, after the number of methods exceeds, packaged into a single dex file will compile an error, so it needs to be packaged into multiple dex files, that is multiDexEnable.
Step 1: introduce dependencies
implementation 'com.android.support:multidex:1.0.3'
Or
implementation 'androidx.multidex:multidex:2.0.0'
Step 2: Add configuration items
Add the configuration item multiDexEnable true under android-->defaultConfig of the main project, as shown in the following figure:
defaultConfig {
applicationId ""
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.1"
multiDexEnabled true
}
Step 3. Introduce the Application class
If there is no Application class in the original project, create a new Application class to inherit the MultiDexApplication class, and then reference this Application in the Manifest file.
If there is already an Application class in the original project, which has already inherited other classes, due to java's single inheritance mode, MultiDexApplication can no longer be inherited, and it can be solved by rewriting the attachBaseContext method
@Override
protected void attachBaseContext(android.content.Context base) {
super.attachBaseContext(base);
android.support.multidex.MultiDex.install(this);
}
Some netizens reported that after the above steps, there is no error when running on version 5.0 or higher, but it will crash in version 4.4. Here are the relevant suggestions (only recorded, not tested, no android4.4 device at hand)
Add configuration in build.gradle file, same level directory as buildTypes and defaultConfig configuration
dexOptions{
preDexLibraries = false
}
ReplyDeleteThanks for this information. it is helpfull and worthy
Laptop Repair Center offers quality service for your laptop at a reasonable cost. We offer doorstep support, 24*7 onsite support, repair all brand laptops, have an expert team for onsite support, and much more. for more contact us on 7291903784
laptop repair center in Delhi