Android android 4.4.2 上的 NoClassDefFoundError

Android NoClassDefFoundError on android 4.4.2

当我在 Android 4.4.2 上尝试 运行 我的应用程序时,它给我这个错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: my.package, PID: 24775
              java.lang.NoClassDefFoundError: my/package/models/Transaction
                  at java.lang.Class.getDeclaredFields(Native Method)
                  at java.lang.Class.getDeclaredFields(Class.java:610)
                  at com.activeandroid.util.ReflectionUtils.getDeclaredColumnFields(ReflectionUtils.java:73)
                  at com.activeandroid.TableInfo.<init>(TableInfo.java:66)
                  at com.activeandroid.ModelInfo.loadModelFromMetaData(ModelInfo.java:101)
                  at com.activeandroid.ModelInfo.<init>(ModelInfo.java:61)
                  at com.activeandroid.Cache.initialize(Cache.java:66)
                  at com.activeandroid.ActiveAndroid.initialize(ActiveAndroid.java:44)
                  at com.activeandroid.ActiveAndroid.initialize(ActiveAndroid.java:34)
                  at com.activeandroid.content.ContentProvider.onCreate(ContentProvider.java:39)
                  at android.content.ContentProvider.attachInfo(ContentProvider.java:1591)
                  at android.content.ContentProvider.attachInfo(ContentProvider.java:1562)
                  at android.app.ActivityThread.installProvider(ActivityThread.java:5233)
                  at android.app.ActivityThread.installContentProviders(ActivityThread.java:4827)
                  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4710)
                  at android.app.ActivityThread.access00(ActivityThread.java:174)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1367)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:146)
                  at android.app.ActivityThread.main(ActivityThread.java:5593)
                  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:1283)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                  at dalvik.system.NativeStart.main(Native Method)
               Caused by: java.lang.ClassNotFoundException: Didn't find class "my.package.models.Transaction" on path: DexPathList[[zip file "/data/app/my.package-1.apk"],nativeLibraryDirectories=[/data/app-lib/my.package-1, /vendor/lib, /system/lib]]
                  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:457)

但是,对于 Android 5.0 及更高版本,它可以正常运行,没有任何错误。

这是什么原因,我该如何解决?

注意:Whosebug 上有类似的问题,但没有任何答案。

试试这个

只需去禁用 Instant 运行 去

File -> Settings -> Build,Execution, Deployment -> Instant Run -> Uncheck the checkbox for instant run

运行 你的应用一次。

您可以在这里找到这个问题的答案:

  1. NoClassDefFoundError with Android Studio on Android 4
  2. https://developer.android.com/studio/build/multidex