2015-06-22 5 views
5

Ich habe eine annotierte Aktivität in einer Bibliothek, die ein Abonnent eines EventBus-Ereignisses aus derselben Bibliothek ist. Es sieht so etwas wie dieses, stark vereinfacht:AndroidAnnotations und EventBus

@EActivity(resName = "activity_foo") 
public class Foo extends Activity { 

    public void onEvent(BarEvent event){ 
     doSomething(); 
    } 
} 

Es sollte nach dieser Arbeit:

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

Aber in der Tat gibt es mit diesem Fehler:

Unable to start activity ... de.greenrobot.event.EventBusException: Subscriber class com.foo.bar.activities.Foo_ has no public methods called onEvent

Es scheint, EventBus sucht nicht in der übergeordneten Klasse. Ich denke, die @ Subscribe Annotation, über die alle reden, ist nur in Guava und Otto, aber nicht in EventBus. Niemand redet von Kompatibilitätsproblemen zwischen AA und Eventbus im Netz, daher muss ich etwas vermissen.

Wie kann ich das schaffen?

EventBus: 2.4

AA: 3,2

EDIT:

Nach WonderCsabo Antwort I EventBus bis 3.0 beta aktualisiert (einschließlich Anmerkungen Subscribe) und AA bis 3.3.1 und das Problem ist, weg, aber da ist noch eins:

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.n.b(SourceFile:133) 
      at de.greenrobot.event.n.a(SourceFile:79) 
      at de.greenrobot.event.c.a(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.n.b(SourceFile:133) 
            at de.greenrobot.event.n.a(SourceFile:79) 
            at de.greenrobot.event.c.a(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) 

Und mir ist aufgefallen, dass beide Probleme (das fehlende onE Vent und jetzt ist der PostThread nur ein Problem in der Release-Version. Ich verwende ProGuard mit den empfohlenen Ausnahmen für EventBus und AA).

EDIT 2:

hinzugefügt I

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

und es scheint zu funktionieren.

+0

sieht aus wie Sie versuchen, einen Anruf 'Foo_' Klasse anstelle von' Foo' Klasse. –

+1

Ja, das ist der Punkt in AA. Es ist eine generierte Unterklasse von Foo. – Herrbert74

+0

danke es funktioniert –

Antwort

2

Sie haben drei Möglichkeiten:

  • Verwendung Otto. Es hat auch das gleiche Problem, was Sie mit EventBus konfrontiert, aber AndroidAnnotations hat spezifische Otto integration, die das Problem löst.
  • Wenn Sie mit EventBus bleiben möchten, können Sie die experimental version ausprobieren, die das Problem nicht als 2.4.0 hat. Es ist auch schöner, da es auf Annotationen statt der Benennung der Methode basiert, nicht auf dem enormen Leistungsschub durch den optionalen EventBus-Annotationsprozessor. Aber es ist immer noch in der Betaversion.
  • Oder Sie können EventBus 2.3.0 verwenden, die nicht das Problem hat, was Sie mit 2.4.0 haben.

Übrigens sollten Sie auf die neuesten AndroidAnnotations aktualisieren, 3.3.1.

5

Ich habe EventBus Anmerkungen arbeiten mit:

# 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>; 
} 

Beachten Sie, dass dies auch sicher, dass Ihr noch Methodennamen verschleiert erhalten.

4

Falls jemand diesen Fehler steht, mit EventBus 3.0 das Paket umbenannt wurde (org statt de und eventbus statt event), so dass die richtige proguard Konfiguration ist:

## 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); 
} 

wie dokumentiert in ihrer site

0

UPDATE

Es scheint dies nicht der richtige answe ist r. Die Ausnahme wird trotzdem ausgelöst.

Original-Antwort

Ab EventBus 3 können Sie die Teilnehmer Ausnahme throwSubscriberException(false) durch den Aufruf deaktivieren.

Wenn Sie dies in Ihrer Anwendungsklasse aufrufen, können Sie diese Ausnahme für Ihren Standard-Event-Bus deaktivieren.

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