How to avoid java.lang.NoClassDefFoundError: android / os / PersistableBundle on pre-L?

I use the TinyBus library to dispatch events in my application. MinSDK - 15, compilation SDK - 23.

On devices under 21, I run into a problem that seems to affect many applications that use reflection. As soon as I try to register on the bus in my base class Activity (which inherits from AppCompatActivity ), I get the following crash log:

 E/AndroidRuntime: java.lang.NoClassDefFoundError: android/os/PersistableBundle E/AndroidRuntime: at java.lang.Class.getDeclaredMethods(Native Method) E/AndroidRuntime: at java.lang.Class.getPublicMethodsRecursive(Class.java:955) E/AndroidRuntime: at java.lang.Class.getMethods(Class.java:938) E/AndroidRuntime: at de.halfbit.tinybus.impl.ObjectsMeta.<init>(ObjectsMeta.java:58) E/AndroidRuntime: at de.halfbit.tinybus.TinyBus.processQueue(TinyBus.java:346) E/AndroidRuntime: at de.halfbit.tinybus.TinyBus.register(TinyBus.java:178) E/AndroidRuntime: at com.package.name.activities.InitializedActivity.onStart(InitializedActivity.java:62) E/AndroidRuntime: at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1166) E/AndroidRuntime: at android.app.Activity.performStart(Activity.java:5264) E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2047) E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2099) E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:138) E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137) E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4929) E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511) E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798) E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565) E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method) 

I know that this error is caused by the fact that dalvik was unable to initialize a class that it cannot find in the dex file. There is a lot of information and questions related to this problem (as it also affects Otto and others), but so far I have only seen one fix: remove the use of PersistableBundle from the application. However, I do not reference the PersistableBundle anywhere in my code, but the system seems to be doing this.

Is there any other known solution to the problem?

+8
java android android-reflection
source share
2 answers

Well, the problem is solved now. After going through all my code (which I had to do many years ago), I found out that I have links to PersistableBundle . They were located in the methods that I allowed the IDE to generate as onPostCreate(PersistableBundle bundle) and did not pay attention to it. Replacing all PersistableBundle with a Bundle resolves the issue.

For users who have a similar problem, I can only offer to carefully study the created material when using the new API.

+6
source share

If you are using Android Studio to create a project, try adding the method below the Application class:

 protected void attachBaseContext(Context base){ super.attachBaseContext(base); MultiDex.install(this); } 

Remember, remember to add the multiDexEnabled true file to gradle . Help him out. Good luck

-one
source share

All Articles