10

I use the TinyBus library to dispatch events in my app. MinSDK is 15, compile SDK is 23.

On pre-21 devices, I'm facing the issue which seems to affect many apps that use reflection. As soon as I try to register on the bus in my base Activity class (which inherits from the 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 dalvik failing to initialize a class it can't find in the dex file. There is a lot of info and questions related to this issue (since it also affects Otto and others), but so far I have only seen one fix: remove the usage of PersistableBundle from the app. However, I do not reference PersistableBundle anywhere in my code, but the system apparently does.

Is there any other known fix to the problem?

Droidman
  • 11,485
  • 17
  • 93
  • 141
  • Do you use proguard? – vmtrue Dec 22 '15 at 16:41
  • @fisher3421 sry for wasting your time, already found the (pretty much of a dumb) mistake (see my answer). Yes, I've been messing around with proguard config for quite a lot, the issue was not related to that, though – Droidman Dec 22 '15 at 16:59
  • http://stackoverflow.com/questions/17973970/how-to-solve-java-lang-noclassdeffounderror/41669190#41669190 search my answer to this post. – Michael Jan 16 '17 at 04:15

2 Answers2

6

Well, the issue is resolved now. After going through all of my code (which I should have done ages ago) I found out that I actually had references to the PersistableBundle. Those were located in methods I let the IDE generate like onPostCreate(PersistableBundle bundle) and wasn't paying attention to. Replacing all PersistableBundle with Bundle fixed the problem.

To folks having similar issue I can only suggest looking closely at the generated stuff when using newer API.

Droidman
  • 11,485
  • 17
  • 93
  • 141
0

If you use Android Studio for create project, try add method below to Application class:

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

Remember don't forget add multiDexEnabled true to gradle file. Help It help you. Good luck!.

Sinh Phan
  • 1,180
  • 3
  • 16
  • 36
  • The purpose of multidex is to enable an app to reference more than `0xFFFF` methods. I cannot think of how it can help to resolve the issue described in the question. – Droidman Dec 04 '15 at 10:50
  • Oh. My problem like you, when i running in device have version < 21. After I add method `attachBaseContext` in Application class, my prolem has resolve. – Sinh Phan Dec 04 '15 at 11:08
  • well I tried to enable mdex support, but magic didn't happen and the issue still persists. – Droidman Dec 04 '15 at 11:28