Eventbus: NoClassDefFoundError android/os/PersistableBundle

Created on 23 Feb 2015  Â·  27Comments  Â·  Source: greenrobot/EventBus

When I install my app on Android 4.4 or less I receive this error:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: android/os/PersistableBundle
at java.lang.Class.getDeclaredMethods(Native Method)
at java.lang.Class.getDeclaredMethods(Class.java:703)
at de.greenrobot.event.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:75)
at de.greenrobot.event.EventBus.register(EventBus.java:163)
at de.greenrobot.event.EventBus.register(EventBus.java:133)
at com.myapp.Domain.Login.Activities.LoginActivity.onCreate(LoginActivity.java:101)

On LoginActivity.java:101 I just have

EventBus.getDefault().register(this);

Most helpful comment

the same problem with use this "public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState)" right is "public void onCreate(Bundle savedInstanceState)" so be carefull!

All 27 comments

Same problem here

Caused by: java.lang.ClassNotFoundException: Didn't find class "android.os.PersistableBundle" on path: DexPathList
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
            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.getDeclaredMethods(Class.java:656)
            at de.greenrobot.event.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:75)
            at de.greenrobot.event.EventBus.register(EventBus.java:163)
            at de.greenrobot.event.EventBus.register(EventBus.java:133)

I know that it was caused by my overriding of
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) in a FragmentActivity

So what? I cant use onSaveInstanceState on any activity that use this?

Same problem, a workaround is to use a inner class singleton as a Event receiver

Oh, we can use public void onSaveInstanceState(Bundle outState) (WITHOUT PersistableBundle parameter)

the same problem with use this "public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState)" right is "public void onCreate(Bundle savedInstanceState)" so be carefull!

great,sovle my probleam

Still happening with compile 'de.greenrobot:eventbus:2.4.0'

This is my BusHelper:

public class BusHelper {

    public static void registerSticky(Object subscriber) {
        try {
            if (!EventBus.getDefault().isRegistered(subscriber)) {
                EventBus.getDefault().registerSticky(subscriber);
            }
        } catch (NoClassDefFoundError e) {
            Timber.e(e, "Subscribing to event bus");
        }
    }

    public static void register(Object subscriber) {
        try {
            if (!EventBus.getDefault().isRegistered(subscriber)) {
                EventBus.getDefault().register(subscriber);
            }
        } catch (NoClassDefFoundError e) {
            Timber.e(e, "Subscribing to event bus");
        }
    }

    public static void unregister(Object subscriber) {
        try { // It throws that exception in icecream
            EventBus.getDefault().unregister(subscriber);
        } catch (NoClassDefFoundError e) {
            Timber.e(e, "Unsubscribing to event bus");
        }
    }

    public static void post(Object event) {
        EventBus.getDefault().post(event);
    }

    public static void postSticky(Object event) {
        EventBus.getDefault().postSticky(event);

        if (event instanceof OrderUpdatedEvent) {
            OrderUpdatedEvent orderEvent = (OrderUpdatedEvent) event;
            if (orderEvent.getOrder() == null) {
                EventBus.getDefault().removeStickyEvent(event);
            }
        }
    }

}

And the error:

07-30 12:49:03.542  11808-11808/com.pickupnow.customer E/BusHelper﹕ Subscribing to event bus
    java.lang.NoClassDefFoundError: android/os/PersistableBundle
            at java.lang.Class.getDeclaredMethods(Native Method)
            at java.lang.Class.getDeclaredMethods(Class.java:656)
            at de.greenrobot.event.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:75)
            at de.greenrobot.event.EventBus.register(EventBus.java:163)
            at de.greenrobot.event.EventBus.registerSticky(EventBus.java:151)
            at com.pickupnow.customer.model.utils.helpers.BusHelper.registerSticky(BusHelper.java:16)
            at com.pickupnow.customer.activity.MainDrawerActivity.onResume(MainDrawerActivity.java:113)
            at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1198)
            at android.app.Activity.performResume(Activity.java:5620)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3142)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5694)
            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:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.os.PersistableBundle" on path: DexPathList[[zip file "/data/app/com.pickupnow.customer-19.apk"],nativeLibraryDirectories=[/data/app-lib/com.pickupnow.customer-19, /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)
            at java.lang.Class.getDeclaredMethods(Native Method)
            at java.lang.Class.getDeclaredMethods(Class.java:656)
            at de.greenrobot.event.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:75)
            at de.greenrobot.event.EventBus.register(EventBus.java:163)
            at de.greenrobot.event.EventBus.registerSticky(EventBus.java:151)
            at com.pickupnow.customer.model.utils.helpers.BusHelper.registerSticky(BusHelper.java:16)
            at com.pickupnow.customer.activity.MainDrawerActivity.onResume(MainDrawerActivity.java:113)
            at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1198)
            at android.app.Activity.performResume(Activity.java:5620)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3142)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5694)
            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:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)

working great if just use public void onCreate(Bundle savedInstanceState) on any KitKat device.

Same problem here when overriding
AppCompatActivity#onPostCreate(Bundle savedInstanceState)
(tested with Android KitKat)

Duplicate of #149: Could someone confirm that be04a2d fixed the issue? Will do a 2.4.1 release once confirmed.

why i just use public void onCreate(Bundle savedInstanceState), it happened too!
It is really confused me, in the other activities, I use it with the same way, works well, but this makes me crazy!

@zhoudailiang Did you use version 2.4.1?

@greenrobot I tried to use version 2.4.1, but nothing changed!
this line
// Workaround for java.lang.NoClassDefFoundError, see https://github.com/greenrobot/EventBus/issues/149
Method[] methods = subscriberClass.getMethods();
would throw

Caused by: java.lang.ClassNotFoundException: Didn't find class "android.os.PersistableBundle" on path: DexPathList[[zip file "/data/app/com.hele.buyer-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.hele.buyer-2, /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:901) 
at java.lang.Class.getMethods(Class.java:884) 
at de.greenrobot.event.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:81) 
at de.greenrobot.event.EventBus.register(EventBus.java:164) 
at de.greenrobot.event.EventBus.register(EventBus.java:134) 

I am so sorry, I found i had override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState).when i removed that, everything looks like well.

This problem happened on SumSung Tab3(4.2.2),other devices works well. It was strange.Sitll now not solve.

removed onRestoreInstanceState from my activity and problem solved

Why it is closed? 2.4.1 fix does NOT work:

Caused by: java.lang.ClassNotFoundException: android.os.PersistableBundle
                                                   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
                                                   at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
                                                   at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
                                                   at java.lang.Class.getDeclaredMethods(Native Method) 
                                                   at java.lang.Class.getPublicMethodsRecursive(Class.java:955) 
                                                   at java.lang.Class.getMethods(Class.java:938) 
                                                   at de.greenrobot.event.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:81) 
                                                   at de.greenrobot.event.EventBus.register(EventBus.java:164) 
                                                   at de.greenrobot.event.EventBus.register(EventBus.java:134) 
                                                   at com.app.BaseActivity.onStart(BaseActivity.java:60) 

I can confirm that I get the same exception when using 2.4.1 when registering an object with a method that has a PersistableBundle argument. It only happens on API20 and lower where the class is unavailable.

But to be honest I wouldn't blame EventBus for this. After all it is my code that references a class that is unavailable on some devices, EventBus just tries to enumerate all methods on my object.

The workaround suggested above by sedonaFR (using an inner class to register for events) works fine for me with either version of EventBus (2.4.0 and 2.4.1).

Good, I meet the same issue, and now solve it by doc http://greenrobot.org/eventbus/documentation/faq/
thanks so much.

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}

just public -> protected. and my problem solved.

very good ~

http://greenrobot.org/eventbus/documentation/faq/
I have encountered this problem, this document helped me solve

In my case all activities extends from BaseActivity, BaseActivity had a register and unregister methods of EventBus, moving these methods to each children solves my problem.

@ChoingHyun have you solved the problem, i have the same with you, regard for your relpy~

I solved this problem,this is my (kotlin) code:

class MyActivity : Activity {

    val register = EventBusRegister()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        register.register()
    }
    override fun onDestroy() {
        register.unregister()
        super.onDestroy()
    }

     inner class EventBusRegister {
        fun register() {
            EventBus.getDefault().register(this)
        }

        fun unregister() {
            EventBus.getDefault().unregister(this)
        }

        @Subscribe(threadMode = ThreadMode.MAIN)
        fun onEventMainThread(event: EventModel) {
            onEvent(event)
        }
    }

    fun onEvent(event: EventModel) {
          //do something
    }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

oky2abbas picture oky2abbas  Â·  6Comments

cckroets picture cckroets  Â·  16Comments

DavidEdwards picture DavidEdwards  Â·  11Comments

nschwermann picture nschwermann  Â·  4Comments

pankajchandiitm picture pankajchandiitm  Â·  22Comments