Search code examples
javaandroidroboguice

RoboGuice: Using full reflection. Try using RoboGuice annotation processor for better performance


When I start my RoboGuice android app, the following message appears in logs:

roboguice.RoboGuice﹕ Using full reflection. Try using RoboGuice annotation processor for better performance.

What does it means?

All my activities (including my MainActivity) extend from classes of RoboGuice. Maybe I'm missing something and there's a way to improve RoboGuice performance that I'm not currently doing.

Note: I'm using RoboBlender


Solution

  • RoboGuice is based on Google Guice library, which is a reflection-based dependency injection framework. Unfortunately, Guice doesn't perform well on Android. For example, in my latest project it took from 1 to 1.5 seconds to initialize an injector.

    At some moment the RoboGuice developer decided to improve performance of his library by doing some work at compile time and that's what RoboBlender is about. It creates an annotation database at compile time and RoboGuice uses it this database at runtime for searching injection points. You can read more about RoboBlender here. This approach required a lot of changes in Guice so the RoboGuice author had to fork it and even sent a pull request to Guice. But this PR was rejected mainly because it contradicts the idea of the Guice library (to be a runtime DI framework).

    Sorry for such a long intro and here's an answer to your question. When calling RoboGuice.setUseAnnotationDatabases(false) you ask RoboGuice to handle everything at runtime via reflection. In other words RoboGuice works just like original Guice and that's not very fast. When you're trying to use annotation databases and your application crashes it may happen because annotation databases cannot be found. Maybe you forgot to add <meta-data android:name="roboguice.annotations.packages" android:value="..."/> to AndroidManifest.xml?

    Finally, I wouldn't recommend you to use Guice or RoboGuice on Android, because the former is extremely slow and the latter is broken by design. It's much better to use a compile time DI library such as Dagger and it shouldn't take a lot of time to switch to it.