Search code examples
androidunity-game-engineandroid-multidex

Unable to instantiate application: Didn't find class on path: DexPathList


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

  1. Adding multiDexKeepFile.txt (multiDexKeepFile file('multiDexKeepFile.txt'))
  2. Adding multiDexKeepProguard.pro (multiDexKeepProguard file('multiDexKeepProguard.pro'))
  • Note: the issue occurs regardless of whether proguard is enabled or disabled.

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:

  1. Using an IPostprocessBuildWithReport to manually add the multiDexKeep files.
  2. Using an IPreprocessBuildWithReport to manually add the multiDexKeep files.
  3. Making an Android Library that contains the multiDexKeep files.

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

Solution

  • 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.