问题
I'm using 'androidx.multidex:multidex:2.0.0'
and I'm pointing to the MultiDexApplication class from the manifest using:
<application
android:name="androidx.multidex.MultiDexApplication"
...
On the build.gradle I also added the follwoing entry:
defaultConfig {
...
multiDexEnabled true
...
When running the application in Android versions older than 5.0 (API 21) I get the following exception:
java.lang.RuntimeException: Unable to instantiate application androidx.multidex.MultiDexApplication: java.lang.ClassNotFoundException: Didn't find class "androidx.multidex.MultiDexApplication" on path [...]
I used the APK browser and I saw that the MultiDexApplication class and the other MultiDex helper classes where in the classes2.dex file. That's probably why it does'nt find the class.
Must I force those classes to be on the main classes.dex file? How can this be fixed?
回答1:
I had the same issue after migrating to AndroidX, though the application class in question was our custom Application class. I tried to clean the project from Android Studio but that didn't work, so eventually I manually removed all the build folders and for good measure did "Invalidate caches and restart" for Android studio as well and that seems to have done the trick for me at least.
回答2:
Use the multiDexKeepFile option to require that MultiDexApplication
ends up in the main dex file.
来源:https://stackoverflow.com/questions/53203590/multidexapplication-class-in-secondary-dex-file-not-found-when-using-androidx