我在库中有一个带注释的活动,它是来自同一个库的EventBus事件的订阅者。它看起来像这样,大大简化了:
@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,问题就解决了,但还有一个问题:
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
我加了
-keep class de.greenrobot.** {*;}而且它似乎起作用了。
发布于 2015-06-22 22:43:34
你有三个选择:
顺便提一下,您应该更新到最新的AndroidAnnotations 3.3.1。
发布于 2015-07-22 15:26:04
我使用了EventBus注释:
# 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>;
}请注意,这也确保了您的方法名仍然被混淆。
发布于 2016-02-19 08:14:57
如果仍然有人面临此错误,则在EventBus 3.0中重新命名包(org而不是de,eventbus代替event),因此正确的保护配置是:
## 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);
}如他们的站点中所记载的
https://stackoverflow.com/questions/30990480
复制相似问题