Java – GreenRobot EventBus, ClassNotFoundException persists after using the Subscriber Index

GreenRobot EventBus, ClassNotFoundException persists after using the Subscriber Index… here is a solution to the problem.

GreenRobot EventBus, ClassNotFoundException persists after using the Subscriber Index

I have the common “ClassNotFoundException”issue with 4.4.2 EventBus of the device,
It even had problems in FAQ

I

first double-check that I’m not using any unused lifecycle methods, but I don’t. Then I updated to EventBus 3.0.0 and used the “subscriber index”. But the error is still there, and I can still see in my stack trace that the builder is trying to use reflexivity (findUsingReflectionInSingleClass):

Caused by: java.lang.ClassNotFoundException: Didn't find class "android.os.PersistableBundle" on path: DexPathList[[zip file "/data/app/com.m360.android.debug-6.apk"], nativeLibraryDirectories=[/data/app-lib/com.m360.android.debug-6, /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 java.lang.Class.getDeclaredMethods(Native Method) 
    at java.lang.Class.getPublicMethodsRecursive(Class.java:894) 
    at java.lang.Class.getMethods(Class.java:877) 
    at org.greenrobot.eventbus.SubscriberMethodFinder.findUsingReflectionInSingleClass(SubscriberMethodFinder.java:157) 
    at org.greenrobot.eventbus.SubscriberMethodFinder.findUsingInfo(SubscriberMethodFinder.java:88) 
    at org.greenrobot.eventbus.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:64) 
    at org.greenrobot.eventbus.EventBus.register(EventBus.java:136) 
    at com.m360.android.activity.PlayerActivity.onStart(PlayerActivity.java:977) 
    at ... 

In the player activity:

@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(QuestionFragment.EnableNextEvent event) {

/* ...... */

}

@Override
protected void onStart() {
    super.onStart();
    EventBus.builder().addIndex(new MyEventBusIndex()).build().register(this);
}

@Override
protected void onStop() {
    super.onStop();
    EventBus.getDefault().unregister(this);
}

MyEventBusIndex is also available, so I guess my build file is fine.

Please let me know if you find a problem.

Solution

I have the same error on my Galaxy s3 (android 4.3). It happens on Android with API levels lower than 21, and you can directly reference PersistableBundle .

“EventBus 3 with subscriber index” didn’t help me (for you).
So I found a workaround here .

More errors/reasons here .

P.S. Event Bus 3.0.0

Related Problems and Solutions