Subscriber Index

The subscriber index is a new feature of EventBus 3. It is an optional optimization to speed up initial subscriber registration.

The subscriber index can be created during build time using the EventBus annotation processor. While it is not required to use an index, it is recommended on Android for best performance.

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.

When EventBus cannot use an index, it will automatically fallback to reflection at run time. Thus it will still work, just a bit slower.

How to generate the index

Using annotationProcessor

If you are not using Android Gradle Plugin version 2.2.0 or higher, use the configuration with android-apt.

To enable index generation, you need to add the EventBus annotation processor to your build using the annotationProcessor property. Also set an argument eventBusIndex to specify the fully qualified class of the index you want to generate. So for example, add the following sections to your Gradle build script:

Using android-apt

If the above does not work for you, you can add the EventBus annotation processor to your build using the android-apt Gradle plugin. Add the following sections to your Gradle build script:

When you build the project the next time (without errors that is), the class specified with eventBusIndex will be generated for you. Then when setting up EventBus pass it like this:

Or, if you want to use the default instance throughout your app:

Indexing your Libraries

You can apply the same principle to code that is part of a library (and not the final application). This way, you may have multiple index classes, which you can all add during the EventBus setup, for example:

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •