Anyone who has used a dependency injection (DI) framework on a project can likely speak to its benefits when creating complex systems. Relying on it can help an engineer to design and implement clean, testable software.
Anyone who has tried to write Android applications using a DI framework has likely been disappointed in some degree with current offerings. DI frameworks in Java have largely been written with server architectures in mind and as such are either generally too resource heavy to work on a mobile device. There have been some success stories, but they come with issues of their own. RoboGuice, the Android DI go to, is built around Google’s Guice. Roboguice is easy to set up and very powerful, but it comes with a cost. Because it uses run-time validation of the dependency tree, any application that uses it incurs a startup cost the first time it requests injection. On simple projects with few injected dependencies, this may be less than half a second, but I have seen it go as high as 1.5 on the latest hardware with more complex apps. There are ways to delay this cost or minimize the impact it has on the user experience (another post for another time), but the cost won’t go away.
Enter dagger: a dependency injection framework from the guys at Square (mobile payments system for Android & iPhone). Dagger provides much of the benefit of Guice, without the cost of validating at run-time. They’ve moved that step to build-time, all but eliminating any start up overhead. There doesn’t currently exist an Android wrapper like RoboGuice, and it’s lacking some of the simple features Guice provides (you need to write provider methods as opposed to just binding an interface to an implementation), but if time to meaningful paint is important, that’s ok.
I’ve thrown together a couple of test applications, one implemented with RoboGuice and one with dagger to compare just how much time each need to accomplish the same thing. It turns out that the difference is quite large. What RoboGuice needs ~400ms to do, dagger could accomplish in ~30ms. Details can be found in the README, but it seems to me that dagger may take Guice’s place as the go to DI library for Android in the not to distant future.