AndroidAnnotations and EventBus

I have annotated activity in a library that is a subscriber of an EventBus event from the same library. It looks something like this: greatly simplified:

@EActivity(resName = "activity_foo") public class Foo extends Activity { public void onEvent(BarEvent event){ doSomething(); } } 

It should work in accordance with this:

http://timnew.me/blog/2014/09/14/otto-and-android-annotations-compatibility-issue-analysis/

But actually it returns with this error:

Cannot start activity ... de.greenrobot.event.EventBusException: Subscriber class com.foo.bar.activities.Foo_ does not have public methods called onEvent

EventBus doesn't seem to look in the parent class. I think the @Subscribe annotation everyone is talking about is only in Guava and Otto, but not in EventBus. No one talks about compatibility issues between AA and Eventbus on the network, so I am missing something.

How can I do this job?

EventBus: 2.4

AA: 3.2

EDIT

Following WonderCsabo's answer, I upgraded EventBus to version 3.0 (including subscription annotations) and AA to 3.3.1, and the problem went away, but there is one more:

  java.lang.NoSuchFieldError at libcore.reflect.AnnotationAccess.decodeValue(AnnotationAccess.java:688) at libcore.reflect.AnnotationAccess.getDefaultValue(AnnotationAccess.java:361) at java.lang.reflect.Method.getDefaultValue(Method.java:327) at libcore.reflect.AnnotationFactory.getElementsDescription(AnnotationFactory.java:75) at libcore.reflect.AnnotationFactory.<init>(AnnotationFactory.java:112) at libcore.reflect.AnnotationFactory.createAnnotation(AnnotationFactory.java:94) at libcore.reflect.AnnotationAccess.toAnnotationInstance(AnnotationAccess.java:666) at libcore.reflect.AnnotationAccess.toAnnotationInstance(AnnotationAccess.java:641) at libcore.reflect.AnnotationAccess.getDeclaredAnnotation(AnnotationAccess.java:170) at java.lang.reflect.Method.getAnnotation(Method.java:301) at de.greenrobot.event.nb(SourceFile:133) at de.greenrobot.event.na(SourceFile:79) at de.greenrobot.event.ca(SourceFile:135) at com.babestudios.lib.lq.activities.f.onStart(SourceFile:515) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236) at android.app.Activity.performStart(Activity.java:6006) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.NoSuchFieldException: PostThread at java.lang.Class.getDeclaredField(Class.java:890) at libcore.reflect.AnnotationAccess.decodeValue(AnnotationAccess.java:685)            at libcore.reflect.AnnotationAccess.getDefaultValue(AnnotationAccess.java:361)            at java.lang.reflect.Method.getDefaultValue(Method.java:327)            at libcore.reflect.AnnotationFactory.getElementsDescription(AnnotationFactory.java:75)            at libcore.reflect.AnnotationFactory.<init>(AnnotationFactory.java:112)            at libcore.reflect.AnnotationFactory.createAnnotation(AnnotationFactory.java:94)            at libcore.reflect.AnnotationAccess.toAnnotationInstance(AnnotationAccess.java:666)            at libcore.reflect.AnnotationAccess.toAnnotationInstance(AnnotationAccess.java:641)            at libcore.reflect.AnnotationAccess.getDeclaredAnnotation(AnnotationAccess.java:170)            at java.lang.reflect.Method.getAnnotation(Method.java:301)            at de.greenrobot.event.nb(SourceFile:133)            at de.greenrobot.event.na(SourceFile:79)            at de.greenrobot.event.ca(SourceFile:135)            at com.babestudios.lib.lq.activities.f.onStart(SourceFile:515)            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)            at android.app.Activity.performStart(Activity.java:6006)            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)            at android.app.ActivityThread.access$800(ActivityThread.java:151)            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)            at android.os.Handler.dispatchMessage(Handler.java:102)            at android.os.Looper.loop(Looper.java:135)            at android.app.ActivityThread.main(ActivityThread.java:5254)            at java.lang.reflect.Method.invoke(Native Method)            at java.lang.reflect.Method.invoke(Method.java:372)            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

And I noticed that both problems (missing onEvent and now PostThread is only a problem in the release version. I use ProGuard with recommended exceptions for EventBus and AA).

EDIT 2 :

I added

 -keep class de.greenrobot.** {*;} 

and it seems to work.

+5
source share
4 answers

You have three options:

By the way, you should upgrade to the latest AndroidAnnotations, 3.3.1.

+2
source

I have EventBus annotations working with:

 # Ensure annotations are kept for runtime use. -keepattributes *Annotation* # Don't remove any GreenRobot classes -keep class de.greenrobot.** {*;} # Don't remove any methods that have the @Subscribe annotation -keepclassmembers class ** { @de.greenrobot.event.Subscribe <methods>; } 

Note that this also ensures that your method names will still be confused.

+5
source

In case someone still encounters this error, with EventBus 3.0 the package has been renamed ( org instead of de and eventbus instead of event ), therefore the correct proguard configuration:

 ## GreenRobot EventBus specific rules ## # http://greenrobot.org/eventbus/documentation/proguard/ -keepattributes *Annotation* -keepclassmembers class ** { @org.greenrobot.eventbus.Subscribe <methods>; } -keep enum org.greenrobot.eventbus.ThreadMode { *; } # Only required if you use AsyncExecutor -keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent { <init>(java.lang.Throwable); } 

as described on the site

+4
source

UPDATE

This seems to be the wrong answer. The exception is anyway.

Original answer

As of EventBus 3, you can disable the subscriber exception by throwSubscriberException(false) .

By invoking this in your application class, you can disable this exception for your default event bus.

 EventBus.builder().throwSubscriberException(false).installDefaultEventBus(); 
0
source

All Articles