Table of contents

java.lang.NoClassDefFoundError: Landroidx/localbroadcastmanager/content/LocalBroadcastManager

Android Jul 08, 2020 Viewed 877 Comments 0

Issue

In the androidx project, I write these code as bellow.

LocalBroadcastManager mgr = LocalBroadcastManager.getInstance(context);

After running on the phone, it throws an exception.

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) 

Solution

Add the dependency of  LocalBroadCastManager from AndroidX to the build.gradle dependencies:

dependencies {
    ...
    implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
}
Updated Jul 08, 2020