首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AndroidAnnotations和EventBus

AndroidAnnotations和EventBus
EN

Stack Overflow用户
提问于 2015-06-22 21:53:17
回答 4查看 4.1K关注 0票数 5

我在库中有一个带注释的活动,它是来自同一个库的EventBus事件的订阅者。它看起来像这样,大大简化了:

代码语言:javascript
复制
@EActivity(resName = "activity_foo")
public class Foo extends Activity {

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

它应根据这一点发挥作用:

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

但实际上,它返回时出现了以下错误:

无法开始活动..。de.greenrobot.event.EventBusException:订户类com.foo.bar.activities.Foo_没有称为onEvent的公共方法

EventBus似乎没有在父类中查找。我想每个人都在谈论的@ Guava注释只在番石榴和Otto中,而不是在EventBus中。没有人在网上谈论AA和Eventbus之间的兼容性问题,所以我肯定遗漏了什么。

我怎么才能把这事做好?

EventBus: 2.4

AA: 3.2

编辑

在WonderCsabo的回答之后,我将EventBus更新为3.0测试版(包括订阅注释),将AA更新到3.3.1,问题就解决了,但还有一个问题:

代码语言:javascript
复制
   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)

我注意到这两个问题(缺少的onEvent和现在的PostThread只是发布版本上的一个问题。我使用ProGuard ( EventBus和AA的推荐例外)。

编辑2

我加了

代码语言:javascript
复制
-keep class de.greenrobot.** {*;}

而且它似乎起作用了。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-06-22 22:43:34

你有三个选择:

  • 利用奥托。它也面临着与EventBus相同的问题,但是AndroidAnnotations有特定的奥托积分来解决这个问题。
  • 如果您想坚持使用EventBus,可以尝试实验版,它在2.4.0中没有问题。它也更好,因为它是基于注释而不是方法命名的,而不是可选的EventBus注释处理器带来的巨大性能提升。但它仍处于测试阶段。
  • 或者您可以使用EventBus 2.3.0,它与2.4.0没有问题。

顺便提一下,您应该更新到最新的AndroidAnnotations 3.3.1。

票数 2
EN

Stack Overflow用户

发布于 2015-07-22 15:26:04

我使用了EventBus注释:

代码语言:javascript
复制
# 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>;
}

请注意,这也确保了您的方法名仍然被混淆。

票数 5
EN

Stack Overflow用户

发布于 2016-02-19 08:14:57

如果仍然有人面临此错误,则在EventBus 3.0中重新命名包(org而不是deeventbus代替event),因此正确的保护配置是:

代码语言:javascript
复制
## 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);
}

如他们的站点中所记载的

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30990480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档