MultiDex issue: java.lang.ClassNotFoundException: Didn't find class on path: DexPathList

5.6k Views Asked by At

I am facing an issue with my app: If I deactivate within Android Studio Instant Run, my app crashes upon startup with the stacktrace:

12-29 13:36:37.876 4549-4549/com.mycoolapp.debug E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.mycoolapp.debug, PID: 4549
                                                                              java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mycoolapp.debug/com.myapp.SplashScreenActivity}: java.lang.ClassNotFoundException: Didn't find class "com.myapp.SplashScreenActivity" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.mycoolapp.debug-2/base.apk"],nativeLibraryDirectories=[/data/app/com.mycoolapp.debug-2/lib/arm, /vendor/lib, /system/lib]]
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2216)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:148)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:135)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5272)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
                                                                               Caused by: java.lang.ClassNotFoundException: Didn't find class "com.myapp.SplashScreenActivity" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.mycoolapp.debug-2/base.apk"],nativeLibraryDirectories=[/data/app/com.mycoolapp.debug-2/lib/arm, /vendor/lib, /system/lib]]
                                                                                  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                                  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                                                  at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                                                  at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2206)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365) 
                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:148) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:135) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5272) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) 
                                                                                Suppressed: java.lang.NoClassDefFoundError: com.myapp.SplashScreenActivity
                                                                                  at dalvik.system.DexFile.defineClassNative(Native Method)
                                                                                  at dalvik.system.DexFile.defineClass(DexFile.java:226)
                                                                                  at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
                                                                                  at dalvik.system.DexPathList.findClass(DexPathList.java:321)
                                                                                  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
                                                                                        ... 14 more
                                                                                Suppressed: java.lang.ClassNotFoundException: com.myapp.SplashScreenActivity
                                                                                  at java.lang.Class.classForName(Native Method)
                                                                                  at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                                                                  at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                                                                  at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                                                                                        ... 13 more
                                                                               Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

As far as I have researched this belongs to multidex: https://developer.android.com/studio/build/multidex.html

Even if I add the multiDexEnabled property to my gradle config the app crashes. Only if I enable InstantRun (which enables multidex in background), the app runs... If I create an apk the installation works, but the app crashes on startup with the same error.

Since I have as minSDK API Level 21, there should be no other change except of the multiDexEnabled property necessary, right?

4

There are 4 best solutions below

3
On BEST ANSWER

So last but not least my answer: All my configuration for multidex was correct... I don't know why, but after a complete OS restart everything works fine now... :)

1
On

Just create new phone on AVD and try to install app on that phone. Hope it will work.

0
On

The accepted answer was only a temporary fix for me, after altering code and a few builds the error came back.

After alot of trial and error the real problem was the jdk location my project was using, instead of using the embedded jdk in android studio i was using an installed version from oracle. After going to File > Project Structure > SDK Location and checking Use embedded JDK the problem was fixed for good.

0
On

Check if your classpath

'com.android.tools.build:gradle:3.1.0-alpha05'

is up-to-date, had same problem and solved it with a full update.