Greenrobot Android Eventbus - 没有选项eventbusindex传递给注释处理器

时间:2017-08-02 06:13:27

标签: android subscribe event-bus greenrobot-eventbus greenrobot-eventbus-3.0

我尝试使用Greenrobot的Eventbus在我的Android应用程序中设置一个简单的订阅者,但是我收到了一个gradle构建错误。我在下面显示了我的代码。

活动类

public final class OffersProcessedEvent {}

基本片段

public class BaseFragment extends Fragment {
    private boolean registered;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
        registered = true;
   }

    @Override
    public void onResume() {
        super.onResume();
        if (!registered) {
        EventBus.getDefault().register(this);
        registered = true;
    }
}

    @Override
    public void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
        registered = false;
    }

    public AppCompatActivity getAppCompatActivity() {
        return (AppCompatActivity) getActivity();
    }
}

活动发布'

EventBus.getDefault().post(new OffersProcessedEvent());

订阅代码

@Subscribe
    public void onMessageEvent(OffersProcessedEvent event){
        *do whatever*
    }

以下是我的错误

构建Gradle错误

  

错误:没有选项eventBusIndex传递给注释处理器

     

错误:任务':app:compileDebugJavaWithJavac'执行失败。

     

编译失败;有关详细信息,请参阅编译器错误输出。

Gradle依赖

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support:design:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'com.facebook.android:facebook-android-sdk:4.5.0'
    compile 'com.jakewharton:butterknife:8.6.0'
    compile 'com.jakewharton:butterknife-compiler:8.6.0'
    compile 'com.jakewharton:butterknife:8.7.0'
    compile 'com.jakewharton:butterknife-compiler:8.7.0'
    compile 'com.github.bumptech.glide:glide:3.5.2'
    compile 'com.android.support:cardview-v7:26.0.0-alpha1'
    compile 'com.google.code.gson:gson:2.8.1'
    compile 'org.greenrobot:eventbus:3.0.0'
    compile 'org.greenrobot:eventbus-annotation-processor:3.0.1'
}

2 个答案:

答案 0 :(得分:2)

apply plugin: 'kotlin-kapt' // ensure kapt plugin is applied

dependencies {
    implementation 'org.greenrobot:eventbus:3.1.1'
    kapt 'org.greenrobot:eventbus-annotation-processor:3.1.1'
}

kapt {
    arguments {
        arg('eventBusIndex', 'com.example.myapp.MyEventBusIndex')
    }
}

答案 1 :(得分:1)

基于此链接http://greenrobot.org/eventbus/documentation/faq/

  

在设置构建脚本时,您没有添加任何脚本,就会发生这种情况   代码注释。没有任何@Subscribe注释,   用于索引的注释处理器将不会触发以消耗   “ eventBusIndex”选项。因此,只需添加一些@Subscribe注释,   你应该没事的。

因此您必须添加

@Subscribe
fun onEvent(event: SomeEvent) {

}

但是你是不是会得到Error:No option eventBusIndex passed to annotation processor

将此添加到您的app.gradle

如果使用 annotationProcessor

defaultConfig {
    //other code

   javaCompileOptions {
       annotationProcessorOptions {
           arguments = [eventBusIndex: "com.example.myapp.MyEventBusIndex"]
       }
   }
}

如果使用 kapt

dependencies {
//other codes
}
kapt {
    arguments {
        arg('eventBusIndex', 'com.example.myapp.MyEventBusIndex')
    }
}

如果使用 android-apt

dependencies {
//other codes
}

apt {
    arguments {
        eventBusIndex "com.example.myapp.MyEventBusIndex"
    }
}

此文件将由EventBus生成

/** This class is generated by EventBus, do not edit. */
public class MyEventBusIndex implements SubscriberInfoIndex {
    private static final Map<Class<?>, SubscriberInfo> SUBSCRIBER_INDEX;

    static {
        SUBSCRIBER_INDEX = new HashMap<Class<?>, SubscriberInfo>();

        putIndex(new SimpleSubscriberInfo(MainActivity.class, true, new SubscriberMethodInfo[] {
            new SubscriberMethodInfo("onBleClick", com.sepanodp.babytemp_sepano.Events.BleClickListener.class),
        }));

    }

    private static void putIndex(SubscriberInfo info) {
        SUBSCRIBER_INDEX.put(info.getSubscriberClass(), info);
    }

    @Override
    public SubscriberInfo getSubscriberInfo(Class<?> subscriberClass) {
        SubscriberInfo info = SUBSCRIBER_INDEX.get(subscriberClass);
        if (info != null) {
            return info;
        } else {
            return null;
        }
    }
}

希望有帮助