Android's Options for Dependency Injection: Dagger, RoboGuice, and ButterKnife
There are a number of choices out there when it comes to libraries for Dependency Injection (DI) on Android, but how do you know which is for you?
There are a number of choices out there when it comes to libraries for Dependency Injection (DI) on Android, but how do you know which is for you? When the differences are subtle, it can be helpful to look to the opinions of people who are excited about one solution or another:
In the first part of his new series on DI, Antonio Leiva argues in favor of a framework called Dagger. He begins with an overview of DI in general - what it is, the problems it solves, and why one might want to take advantage of it - and offers a concise way of looking at dependency injectors:
We can consider it [to be] another module in our app that is in charge of providing instances of the rest of modules and inject their dependencies. That is basically its duty. The creation of modules is localized in a single point in our app, and we have full control over it.
Dagger in particular, Leiva argues, is ideal for low-end devices because of its use of a reflection-avoiding pre-compiler, given that the reflection most DI relies on is taxing and time consuming. Dagger, then, may not be the ideal powerhouse of dependency injectors, but it is, according to Leiva, the most efficient.
Paresh Mayani's article on how to become a lazy but productive Android developer takes an in-depth look DI libraries for Android - Dagger, as well as ButterKnife and Android Annotation (AA) - but only fully explores RoboGuice, based on Google's Guice library. Mayani provides a number of quick and useful resources for RoboGuice, including:
- Annotations to demonstrate usage
- Installation tips
- A complete example Activity
- Usage tips
- Benefits of RoboGuice
- ActionBarSherlock integration
Overall, Mayani portrays RoboGuice as a huge time-saver. Fewer lines of code means fewer bugs, and fewer lines of boiler plate means more time spent on the core logic of your app.
Obviously, Jake Wharton loves ButterKnife. But for an outside opinion, Harsha Vardhan takes a look at ButterKnife compared to RoboGuice. Vardhan provides a helpful table to compare the features of each, such as:
- Minimum Jars required
- ActionBarSherlock compatibility
- Injection for click listeners
- POJO injection
Vardhan doesn't come down too firmly in favor one solution or another. Instead, he recommends careful consideration of every project's needs:
Which is the best Dependency Injection Framework?
It’s purely depending on the usage of your project.
Also, Vardhan warns of DI frameworks not optimized for mobile environments - again, all things have drawbacks.
As is the case with most development solutions, there are pros and cons to every choice, and individual needs must be addressed. Leiva, Mayani, and Vardhan provide a look at three DI solutions for Android developers (and hint at a few more), so if you're looking for a starting point, there you go.
For those brand new to DI, by the way, all three bloggers provide their own individual perspectives on DI and define it in their own voices - it's a useful resource for anyone looking for a more conversational explanation.