安卓 java.lang.NoClassDefFoundError: Landroidx/localbroadcastmanager/content/LocalBroadcastManager
问题描述
在 androidx 项目中引用以下代码。
LocalBroadcastManager mgr = LocalBroadcastManager.getInstance(context);
在手机上运行之后,报错了。
2020-07-07 11:59:13.992 32146-32146/com.example E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example, PID: 32146
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/localbroadcastmanager/content/LocalBroadcastManager;
at top.cpming.rn.push.vivo.PushMessageReceiverImpl.broadcastMessage(PushMessageReceiverImpl.java:47)
at top.cpming.rn.push.vivo.PushMessageReceiverImpl.onReceiveRegId(PushMessageReceiverImpl.java:30)
at com.vivo.push.c.d$1.run(OnBindAppReceiveTask.java:33)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:243)
at android.app.ActivityThread.main(ActivityThread.java:7094)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:536)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.localbroadcastmanager.content.LocalBroadcastManager" on path: DexPathList[[zip file "/data/app/com.example-gzVRwRvtLyWUbO_BklZN4Q==/base.apk"],nativeLibraryDirectories=[/data/app/com.example-gzVRwRvtLyWUbO_BklZN4Q==/lib/arm64, /data/app/com.example-gzVRwRvtLyWUbO_BklZN4Q==/base.apk!/lib/arm64-v8a, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at top.cpming.rn.push.vivo.PushMessageReceiverImpl.broadcastMessage(PushMessageReceiverImpl.java:47)
at top.cpming.rn.push.vivo.PushMessageReceiverImpl.onReceiveRegId(PushMessageReceiverImpl.java:30)
at com.vivo.push.c.d$1.run(OnBindAppReceiveTask.java:33)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:243)
at android.app.ActivityThread.main(ActivityThread.java:7094)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:536)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)
解决方法
由于是 ClassNotFoundException
,类不存在的异常,在 app/build.gradle
中增加 LocalBroadCastManager
依赖,重新编译。
dependencies {
...
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
}