Android NoClassDefFoundError 之 PersistableBundle

今天测试反馈,在应用在华为麦芒上出现了闪退,找了台类似的低端机,复现了问题

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: android/os/PersistableBundle
at java.lang.Class.getDeclaredMethods(Native Method)
at java.lang.Class.getPublicMethodsRecursive(Class.java:955)
at java.lang.Class.getMethods(Class.java:938)
at org.greenrobot.eventbus.SubscriberMethodFinder.findUsingReflectionInSingleClass(SubscriberMethodFinder.java:157)
at org.greenrobot.eventbus.SubscriberMethodFinder.findUsingInfo(SubscriberMethodFinder.java:88)
at org.greenrobot.eventbus.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:64)
at org.greenrobot.eventbus.EventBus.register(EventBus.java:136)
at com.mx.engine.event.EventProxy.register(EventProxy.java:44)
at com.mx.framework2.view.ui.BaseActivity.init(BaseActivity.java:139)
at com.mx.framework2.view.ui.BaseActivity.onCreate(BaseActivity.java:130)
at com.gome.common.base.GBaseActivity.onCreate(GBaseActivity.kt:27)
at android.app.Activity.performCreate(Activity.java:5043)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2094)
at android.app.ActivityThread.access$600(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4777)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:764)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:531)
at dalvik.system.NativeStart.main(Native Method)

在错误信息中,出现了EventBus的身影,因此直奔github eventbus,在issues搜索PersistableBundle关键词,找到了网友提交的类似情形 https://github.com/greenrobot/EventBus/issues/156
官方解释了该问题出现的原因(http://greenrobot.org/eventbus/documentation/faq/)

class PersistableBundle was added in API level 21. Along with the new class some new life cycle methods were introduced in the class Activity having PersistableBundle as a parameter, for example onCreate (Bundle savedInstanceState, PersistableBundle persistentState). Now, if you override this method and you try to register this Activity to EventBus on a older device, we have exactly the scenario described to cause to bug. Understanding why this happens will help to resolve the issue easilyclass PersistableBundle was added in API level 21. Along with the new class some new life cycle methods were introduced in the class Activity having PersistableBundle as a parameter, for example [onCreate (Bundle savedInstanceState, PersistableBundle persistentState)](http://developer.android.com/intl/in/reference/android/app/Activity.html#onCreate(android.os.Bundle, android.os.PersistableBundle)). Now, if you override this method and you try to register this Activity to EventBus on a older device, we have exactly the scenario described to cause to bug. Understanding why this happens will help to resolve the issue easily

在android5.0中引入了PersistableBundle,在Activity的一些生命周期的方法中,增加了PersistableBundle类型形参,比如

protected void onCreate(@Nullable Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
}

public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
      onCreate(savedInstanceState);
}

protected void onRestoreInstanceState(Bundle savedInstanceState) {
      if (mWindow != null) {
            Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
            if (windowState != null) {
                   mWindow.restoreHierarchyState(windowState);
            }
      }
 }

public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState) {
      if (savedInstanceState != null) {
             onRestoreInstanceState(savedInstanceState);
      }
 }

对比可以发现,相同名称的生命周期方法,带PersistableBundle的方法修饰符为public,而我们常重载的为protected。EventBus通过反射来获取注册类所有的订阅方法,考虑到效率问题,会调用Class.getDeclaredMethods(),也是就public方法,如果重载了含有PersistableBundle的方法,EventBus在类初始化的时候会访问该方法,在系统版本低于Android5.0的手机上,就会因为找不到PersistableBundle.class而抛出异常
仔细检查了项目代码,果然重载了父类的带PersistableBundle的生命周期方法,去掉PersistableBundle,crash消失了

EventBus register方法代码片段

修改之前代码

修改之后代码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,049评论 25 707
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,360评论 2 44
  • 江水三千里,家书十五行。行行无别语,只道早还乡。
    李尔王阅读 219评论 1 1
  • 打开手机发现微信和QQ里面每天都有很多刷世界杯的一些“球迷们”,在感叹和惋惜中迎来了半决赛,我也和朋友们随...
    zcmorlen阅读 153评论 0 1