5

同じライブラリからの 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 は親クラスを参照していないようです。みんなが話題にしている @Subscribe アノテーションは Guava と Otto だけにあると思いますが、EventBus にはありません。ネット上で AA と Eventbus の互換性の問題について話している人は誰もいないので、何かが欠けているに違いありません。

どうすればこれを機能させることができますか?

イベントバス: 2.4

AA: 3.2

編集

WonderCsabo の回答の後、EventBus を 3.0 ベータ版 (Subscribe アノテーションを含む) に、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 は、リリース バージョンの問題のみです。私は、EventBus と AA の推奨される例外と共に ProGuard を使用しています)。

編集2

追加した

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

そしてそれは働いているようです。

4

4 に答える 4

4

誰かがまだこのエラーに直面している場合、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);
}

彼らのサイトに記載されているように

于 2016-02-19T08:14:57.210 に答える
2

次の 3 つのオプションがあります。

  • オットーを使用。EventBus で直面したのと同じ問題もありますが、AndroidAnnotations には、その問題を解決する特定のOtto 統合があります。
  • EventBus を使い続けたい場合は、2.4.0 として問題のない実験的バージョンを試すことができます。オプションの EventBus 注釈プロセッサによる大幅なパフォーマンスの向上については言うまでもなく、メソッドの命名ではなく注釈に基づいているため、より優れています。しかし、まだベータ版です。
  • または、EventBus 2.3.0 を使用できます。これには、2.4.0 で発生した問題はありません。

ちなみに、最新の AndroidAnnotations 3.3.1 にアップデートする必要があります。

于 2015-06-22T22:43:34.977 に答える