Search code examples
androidadb

ClassNotFoundException is occured for Application class when installing apk via adb install, but working fine when running through android studio


I'm using Android studio 3.0 beta 9 to build apk. But recently I encountered that when I run the apk via adb install either on emulator or on real device, it pops ClassNotFoundException, Application Class not found in dexPathList.....

But when I run via Android Studio, it's working fine. Any idea what is the cause?

Here is the error log:-

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.demo, PID: 27637
                  java.lang.RuntimeException: Unable to instantiate application
com.example.demo.MyApplication:java.lang.ClassNotFoundException: Didn't find class "com.example.demo.MyApplication" on path: DexPathList[[zip file "/data/app/com.example.demo-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.demo-1/lib/arm, /data/app/com.example.demo-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]
at android.app.LoadedApk.makeApplication(LoadedApk.java:680)
                                                                         at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6429)
                                                                         at android.app.ActivityThread.access$1800(ActivityThread.java:229)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1898)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:148)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:7402)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                      Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.demo.MyApplication" on path: DexPathList[[zip file "/data/app/com.example.demo-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.demo-1/lib/arm, /data/app/com.example.demo-1/base.apk!/lib/armeabi-v7a, /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.newApplication(Instrumentation.java:1005)
                                                                         at android.app.LoadedApk.makeApplication(LoadedApk.java:670)
                                                                         at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6429) 
                                                                         at android.app.ActivityThread.access$1800(ActivityThread.java:229) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1898) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:148) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:7402) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
                                                                        Suppressed: java.lang.ClassNotFoundException: com.example.demo.MyApplication
                                                                         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)
                                                                                ... 12 more
                                                                      Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

Solution

  • I finally got the answer, there are two problem, first one is mentioned here, where I was trying to adb install on apk which was built by Android Studio when I was directly running the app on android device.

    Second problem is mentioned here

    So when I try to build apk via build gradle task, then it was showing error :-

    Error:Failed to complete Gradle execution.
    
    Cause:
    The version of Gradle you are using (3.3) does not support the forTasks() method on BuildActionExecuter. Support for this is available in Gradle 3.5 and all later versions.
    

    Hence I upgraded the Gradle version to latest once and problem resolved.