我们的 Android 应用程序当前使用 Otto EventBus,它使用反射。我们希望避免反射的开销,但保持灵活性。 是Guava's event bus使用反射?怎么样GreenRobot的?
如果他们不这样做,他们会使用代码生成或类似的东西吗?
最佳答案
Otto 从来没有像 GreenRobot 的 EventBus 那样功能丰富 - 例如,没有线程模式,所以最好摆脱它。 Otto 被弃用,转而支持 RxJava - 这对于许多项目来说是巨大的杀伤力(个人观点)。
但是为了减少反射的使用,GreenRobot EventBus 3.x is able to build an index in compilation time using APT而不是运行时反射。
http://greenrobot.org/eventbus/documentation/subscriber-index/
Index Preconditions: Note that only @Subscriber methods can be indexed for which the subscriber AND event class are public. Also, due to technical limitations of Java’s annotation processing itself, @Subscribe annotations are not recognized inside of anonymous classes.
buildscript {
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
apply plugin: 'com.neenbedankt.android-apt'
dependencies {
compile 'org.greenrobot:eventbus:3.0.0'
apt 'org.greenrobot:eventbus-annotation-processor:3.0.1'
}
apt {
arguments {
eventBusIndex "com.example.myapp.MyEventBusIndex"
}
}
And
EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();
// Now the default instance uses the given index. Use it like this:
EventBus eventBus = EventBus.getDefault();
关于java - GreenRobot 和 Guava 的 EventBus 使用反射吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40743304/