Android Studio项目会经常遇到这个错误,然后就各种Clean Project和Rebuild Project有时候有用有时候没用,摸不着头脑
错误代码如下:
12-18 17:46:01.512 4978-4978/app.dawnling.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: app.dawnling.app, PID: 4978
java.lang.RuntimeException: Unable to instantiate application app.dawnling.app.app.MyApplication: java.lang.ClassNotFoundException: Didn't find class "app.dawnling.app.app.MyApplication" on path: DexPathList[[zip file "/data/app/app.dawnling.app-2/base.apk"],nativeLibraryDirectories=[/data/app/app.dawnling.app-2/lib/x86, /system/lib, /vendor/lib]]
at android.app.LoadedApk.makeApplication(LoadedApk.java:802)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5377)
at android.app.ActivityThread.-wrap2(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.ClassNotFoundException: Didn't find class "app.dawnling.app.app.MyApplication" on path: DexPathList[[zip file "/data/app/app.dawnling.app-2/base.apk"],nativeLibraryDirectories=[/data/app/app.dawnling.app-2/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.Instrumentation.newApplication(Instrumentation.java:992)
at android.app.LoadedApk.makeApplication(LoadedApk.java:796)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5377)
at android.app.ActivityThread.-wrap2(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
原因猜测:
之所以 Didn't find class 找不到相关报错的类,可能是由于类在编译打包的过程中,没有全部一起打包上,漏掉了。
解决方法:
1.先删除项目结构中的 .gradle 文件夹,然后Build->Rebuild Project->Clean Project->Build APK重新运行安装,即可正常运行
2.如果失败,请尝试再次删除.gradle 和.idea 文件,重启 Android Studio ,然后再尝试。