One of our Unity applications recently ran into a problem on Android with the Application class not being in the main dex. The result is the application builds, but crashes on launch.
This is an existing Android application, with multidex enabled, and the Application extends MultiDexApplication. I followed the instructions to Declare classes required in the primary DEX file, but the same Exception is thrown.
java.lang.RuntimeException: Unable to instantiate application com.example.Application: java.lang.ClassNotFoundException: Didn't find class "com.example.Application" on path: DexPathList
Similarly, the MultiDexApplication class is not found in the main dex, as seen further in the stack trace:
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.multidex.MultiDexApplication" on path: DexPathList
I have tried
multiDexKeepFile file('multiDexKeepFile.txt')
)multiDexKeepProguard file('multiDexKeepProguard.pro')
)When I add the multiDexKeep files to Assets/Plugins/Android/, where the gradle file is located, Unity seems to ignore them, even when I export the project. So I have also tried:
Unity does not seem to pick up on the multiDexKeep files on its own, but it is able to export them, with either process. However, even when I export the project and build the project in Android Studio, the classes still do not appear in the main dex.
Has anyone solved this issue?
multiDexKeepFile.txt:
androidx/multidex/MultiDexApplication.class
com/example/Application.class
multiDexKeepProguard.pro:
-keep class androidx.multidex.MultiDexApplication
-keep class com.example.Application
I was able to resolve this. It seems that Unity recognizes the files if they are inside an .androidlib.
I also found references to an sdk that we are no longer using inside of the main Activity. Removing the code and the sdk also seems to have appeased the dex gods.