android ClassNotFoundException: 未找到 class 错误

android ClassNotFoundException: Didn't find class error

我收到与 java.lang.ClassNotFoundException 相同的错误:未找到 class,但仅限于版本 4.4

我尝试了各种方法都无法解决

有没有办法解决这个问题?

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.access0(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.access0(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) 

在 android 4.4 中,我们面临以下问题:在主 DEX 文件中声明需要 classes。此处提到:(https://developer.android.com/studio/build/multidex#keep)

从 Gradle 2.2.0(2016 年 9 月发布)开始,您可以使用 multiDexKeepFile api

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

}

其中 multidex_keep_file.txt 是每行单个 class 的文件,需要显式添加到主 dex

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

另一种方法是:使用 multiDexKeepProguard 保留整个包

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