1.在app的build.gradle,在dependencies中添加最新的EventBus依赖:
compile 'org.greenrobot:eventbus:3.0.0'
2.使用索引加速,首先在项目gradle的dependencies中引入apt编译插件:
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
然后在app的build.gradle中应用apt插件,并设置apt生成的索引的包名和类名:
apply plugin: 'com.neenbedankt.android-apt'
apt {
arguments {
eventBusIndex "com.study.sangerzhong.studyapp.MyEventBusIndex"
}
}
在app的build.gradle,在dependencies中添加
apt 'org.greenrobot:eventbus-annotation-processor:3.0.1'
此时需要我们先编译一次,生成索引类。编译成功之后,就会发现在\ProjectName\app\build\generated\source\apt\PakageName\
下看到通过注解分析生成的索引类,这样我们便可以在初始化EventBus时应用我们生成的索引了。
添加index
EventBus mEventBus = EventBus.builder().addIndex(new MyEventBusIndex()).build();
如果想把自定义的设置应用到EventBus默认的单例中,则可以用installDefaultEventBus()
方法:
EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();