square/otto

java.lang.NoClassDefFoundError for android/os/PersistableBundle during registering activity on Bus

Den-Rimus opened this issue · 11 comments

01-16 00:36:40.009  27061-27061/net.mobindustry.shopaholic 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 com.squareup.otto.AnnotatedHandlerFinder.loadAnnotatedMethods(AnnotatedHandlerFinder.java:52)
            at com.squareup.otto.AnnotatedHandlerFinder.findAllProducers(AnnotatedHandlerFinder.java:126)
            at com.squareup.otto.HandlerFinder$1.findAllProducers(HandlerFinder.java:33)
            at com.squareup.otto.Bus.register(Bus.java:191)

I've seen this issue. But I just don't get - we get this crash on all pre21 API devices with our app - during OttoBusProvider.getInstance().register(this); in onPause().
Looking at the number of forks and start I assume that lots of people and teams are using this library - how comes they don't get the crash?

Is there any possible workaround here?

P. S.: before we tried EventBus lib from greenrobot - same error, identical stack, only package name differs.

Same thing for us.

I don't think it has anythink to do with Otto.
android.os.PersistableBundle is a class introduced in Lollipop for a new mechanism of "resurecting" apps after i.e. system restart. See [Activity.onCreate(Bundle, PersistableBundle)](http://developer.android.com/reference/android/app/Activity.html#onCreate%28android.os.Bundle, android.os.PersistableBundle%29).
I saw this error comming from RoboGuice library when injecting an Activity, but unfortunately I did not find the proper cause yet...

We had a (similar) issue when using Bus in a class which referenced 'Objects' type.
Resolved when we removed the use of this specific type.

I have the same issue after deploy successfully on a Lollipop device and try to deploy on a 4.4.4 device.
When I try to re deploy on Android 5.0, it works.
Trying to set targetSDK to 19 doesn't work.
So, what can we do to correct this ?

So How can I manage it?

It's said in the Stackoverflow Topic.

But what If I need to use onSaveInstanceState ? so how can I manage the save on my actual activity state?

Maybe you mean this function : Activity : protected void onSaveInstanceState (Bundle outState)
It's different than onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState)

Otherwise, you need to check if your on API 21 and over or not.

Opa, yeah! thanks!!

I fix it! @CorentinPacaud @xsveda is right! change function to Activity.onCreate(Bundle) or onSaveInstanceState (Bundle outState) . it's work fine. :)