在EventBus 3.0版本中引入了 EventBusAnnotationProcessor(注解分析生成索引)技术,大大提高了EventBus的运行效率,可根据实际情况自行决定要不要添加
Android Studio 3.0之前添加方式:
1. 在项目gradle的dependencies中引入apt编译插件:
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
2. 在App的build.gradle中应用apt插件,并设置apt生成的索引的包名和类名:
apply plugin: 'com.neenbedankt.android-apt'
apt {
arguments {
eventBusIndex "*****.MyEventBusIndex"
}
}
3. 在App的dependencies中引入EventBusAnnotationProcessor:
apt 'org.greenrobot:eventbus-annotation-processor:3.0.1'
但是Android Studio 3.0后Gradle与其兼容,会报错:
具体解决方案:
1. 注释掉项目gradle中的:
//classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
2. 注释掉App的build.gradle中的:
//apply plugin: 'com.neenbedankt.android-apt'
//apt {
// arguments {
// eventBusIndex "****.MyEventBusIndex"
// }
//}
3. 修改dependencies中的apt 为 annotationProcessor:
annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.0.1'
4. defaultConfig中添加
javaCompileOptions {
annotationProcessorOptions {
arguments = [ eventBusIndex : '****.MyEventBusIndex' ]
}
}