android ClassNotFoundException:找不到类错误

时间:2019-09-06 03:25:28

标签: android classnotfoundexception

我收到与java.lang.ClassNotFoundException相同的错误:找不到类,但仅在版本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.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) 

1 个答案:

答案 0 :(得分:3)

在android 4.4中,我们面临以下问题:声明主要DEX文件中所需的类。在这里提到:(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是每行具有单个类的文件,需要将其显式添加到主dex

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

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

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