Search code examples
androidclassnotfoundexception

android ClassNotFoundException: Didn't find class error


I get the same error as java.lang.ClassNotFoundException: Didn't find class, but only in version 4.4

I tried various ways but couldn't solve it

Is there a way to solve this problem?

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.sinwho.timer/com.sinwho.timer.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.sinwho.timer.MainActivity" on path: DexPathList[[zip file "/data/app/com.sinwho.timer-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.sinwho.timer-1, /vendor/lib, /system/lib]]
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2131)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
        at android.app.ActivityThread.access$800(ActivityThread.java:142)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5118)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.sinwho.timer.MainActivity" on path: DexPathList[[zip file "/data/app/com.sinwho.timer-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.sinwho.timer-1, /vendor/lib, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2122)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255) 
        at android.app.ActivityThread.access$800(ActivityThread.java:142) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:136) 
        at android.app.ActivityThread.main(ActivityThread.java:5118) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606) 
        at dalvik.system.NativeStart.main(Native Method) 

Solution

  • In android 4.4, we are facing the issue about: Declare classes required in the primary DEX file. mentioned in here: (https://developer.android.com/studio/build/multidex#keep)

    From Gradle 2.2.0 (Released in September 2016) you can use multiDexKeepFile api

    android {
    buildTypes {
        debug {
            ...
            multiDexEnabled true
            multiDexKeepFile file('multidex_keep_file.txt')
        }
    }
    

    }

    Where multidex_keep_file.txt is file with single class per line which needs to be explicitly added to the main dex

     com/test/TestClass.class
     com/test/TestClass.class
    

    Another approach is: use multiDexKeepProguard to keep whole package

    -keep class com.test.** { *; }