ClassNotFoundException: Didn't find class "android.os.PersistableBundle" Otto Android 5.0
Asked Answered
A

4

20

I have a strange issue. I have an app which I deployed on an Android 4.4 device and use Otto library. I deployed the app on an Android 5.0 device. It still works. I retried on the 4.4 and the app won't launched.

Apparently, it tries to use PersistableBundle.class which a API 21 class. Here my log :

    Caused by: java.lang.ClassNotFoundException: Didn't find class "android.os.PersistableBundle" on path: DexPathList[[zip file "/data/app/fr.myapp.apk"],nativeLibraryDirectories=[/data/app-lib/fr.myapp, /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.getDeclaredMethods(Class.java:656)
            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)
Alarcon answered 4/3, 2015 at 15:9 Comment(3)
I am not sure if you are the one who commented on the issue, but there is an outstanding issue on this.Accretion
Yep it's me. I just found the solution below.Alarcon
updating android os to 5.0 solved the issue for mePartridge
A
28

I find the "solution". Just remove this function from your activity :

@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    super.onSaveInstanceState(outState, outPersistentState);
}
Alarcon answered 4/3, 2015 at 15:31 Comment(4)
Hmmm... while that probably works, that's really a workaround more than a "solution". Developers might want to use that method on the Android 5.0 and higher devices. Overriding a method that an older API level does not call normally is not a problem. Otto, as it is iterating over methods, should have exception handling in place to deal with this scenario, IMHO.Accretion
It's a Api 21 Method. So if you're under 21, your app crash because it can't be executed. developer.android.com/reference/android/app/…, android.os.PersistableBundle)Alarcon
Just use public void onSaveInstanceState(Bundle outState)not ` onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState)`Rosaceous
Same error was happening to me with onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState). Changed the method to onPostCreate(Bundle savedInstanceState) and error was "resolved".Disabled
P
0

Change targetVersion to the API version of the device you are running.

I had targetVersion = 21 when my device had 4.4 (API 19), changing the value to 19 solved the issue.

Proud answered 15/2, 2017 at 8:21 Comment(0)
O
0

I had a same problem on Samsung Galaxy 3mini and Local operator phones.But i fixed with override activity method.i hope it works for you.

 @Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    try {
        BusApplication.getInstance().register(this);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Outmarch answered 8/4, 2017 at 21:39 Comment(0)
S
0

Base on FAG

Some Android versions seem to have a bug with reflection when calling getDeclaredMethods or getMethods

in this case PersistableBundle was added in API level 21 and when we use it in lifecycle methods like onCreate or onSaveInstanceState this error happens.

If you use of any of following methods in your code

@Override
public void onCreate(Bundle savedInstanceState,PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
}

@Override
public void onPostCreate(Bundle savedInstanceState,PersistableBundle persistentState) {
    super.onPostCreate(savedInstanceState, persistentState);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onRestoreInstanceState(savedInstanceState, persistentState);
}

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

Use following methods instead (or remove above methods if useless)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
}

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

In my case i use this method in baseActivity and made the crash

override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState) 
}

Simply i replace this with

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

And problem solved.

Sobriety answered 23/2, 2019 at 12:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.