Can't run app on device when it installed from debug apk file

It has been a long time since I last post, today I want to share for you an occurred problem recently when I start a new Android project.

First, let us examine the outline what i do before find solution for problem.

  • I create a new Android project with AndroidStudio 2.3
  • Some code added to this project
  • Run project on emulator
  • I must test application with real device but I can't enable USB Debugging on this device. Then I copy debug apk file to this device and install.
  • Install successfully. I open this application and has occurred an error. This application open with a crash :(
  • I using a Logcat app on Android to catch error and see the message
E/AndroidRuntime: FATAL EXCEPTION: main                                                                             Process: net.awpspace.testinstantrun, PID: 31788                                      java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{net.awpspace.testinstantrun/net.awpspace.testinstantrun.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "net.awpspace.testinstantrun.MainActivity" on path: DexPathList[[zip file "/data/app/net.awpspace.testinstantrun-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2439)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
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:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
                                                                              Caused by: java.lang.ClassNotFoundException: Didn't find class "net.awpspace.testinstantrun.MainActivity" on path: DexPathList[[zip file "/data/app/net.awpspace.testinstantrun-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
                                                                                 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:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608) 
at android.app.ActivityThread.access$800(ActivityThread.java:178) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) 
at android.os.Handler.dispatchMessage(Handler.java:111) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5637) 
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:959) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 
Suppressed: java.lang.ClassNotFoundException: net.awpspace.testinstantrun.MainActivity
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
Solution for this error

The first, you can use signed apk file. The error won't occurred.

If you still want to use debug apk file, you must disable Instant Run on Android studio. The error won't occurred. Below is how to disable Instant Run on AndroidStudio.

  1. Open the Settings or Preferences dialog.
  2. Navigate to Build, Execution, Deployment > Instant Run.
  3. Uncheck the box next to Enable Instant Run.
Find a explication for this error

Research about Instant Run of AndroidStudio I find a notion is HotSwap. This is the fastest type of swap and makes changes visible much more quickly. Your application keeps running and a stub method with the new implementation is used the next time the method is called.
When takes to update your app with code and resource changes AndroidStudio push certain code and resource changes to your running app without building a new APK.

You can find more details about Instant Run of AndroidStudio at here

Thank you for reading my article!