EventBus - Subscriber class and its super classes have no public methods with the @subscribe annotation

最后都变了- 提交于 2019-12-03 04:30:40

i think it is because onEvent inside MapClass.java has no parameter. Could you try with the expected parameter?

Please ensure these lines are in your proguard config file if you are using proguard for your builds.

-keepattributes *Annotation*
-keepclassmembers class ** {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }

I faced the same issue and after a long research got the solution for every case. This problem is due to absence of @Subscribe public method onEvent() inside the class which you are trying to register Event bus as EventBus.getDefault().register(this). Presence of this function is mandatory if you register a class with Event bus

This can be in two situations

  1. using progruad : progruad may modify name of method onEvent() due to which event bus is not able to find it. Put these lines inide your progruad rules

    -keepattributes Annotation

    -keepclassmembers class ** {

    @org.greenrobot.eventbus.Subscribe ;

    }

    -keep enum org.greenrobot.eventbus.ThreadMode { *;

}

  1. if you are not using progruard then definitely your class is missing the method onEvent() with @Subscribe annotation. This annotation with method is mandatory with EventBus version 3.0.0 so double check presence of this method inside your class.

In my situation,I got this error for I did't write @Subscribe on the class where i register EventBus.

Just in case your code is like mine :p

I had to set the method as public because it's currently private.

Gevorg Gharibyan

ProGuard

ProGuard obfuscates method names and may remove methods, which are not called (dead code removal). Because Subscriber methods are not directly called, ProGuard assumes them to be unused. So if you enable ProGuard minification, you must tell ProGuard to keep those Subscriber methods.

Use the following rules in your ProGuard configuration file (proguard.cfg) to prevent Subscribers from being removed:

-keepattributes *Annotation*
-keepclassmembers class * {
   @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }

# Only required if you usenter code heree AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
    <init>(java.lang.Throwable);
}

In my case onEvent() was private and placed in child class.

But register() and unregister() were called in parent class.

Solution was to made onEvent() public.

On a sidenote, I got the same error after switching from Google's implementation of the EventBus to this one. This error drove me crazy, because Google's EventBus also has a @Subscribe annotation and I was using that one instead of the one provided by greenrobot.

OK, it's a very silly error on my part, but if I can help even 1 person like me, I'm happy.

Just incase it would help some one, in my case I forgot to pass arguments to the receiving method, everything else was fine. When there is no argument passed to the receiving function/method, in that case this exception is thrown.

If you are using proguard, You will not face this problem in debug mode. I faced this problem in release version. after adding this below code in proguard-rules.pro files, solved my problem.

-keepattributes *Annotation*
-keepclassmembers class ** {
    @org.greenrobot.eventbus.Subscribe <methods>;
 }
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!