Dependency Injection: Dagger / Roboguice / Butterknife

My first question here was “dependency injection, what is that again?”. So after some Googling and some interesting Youtube videos, I stumbled across this explanation which I think sums it up nicely:

Any nontrivial application is made up of two or more classes that collaborate with each other to perform some business logic. Traditionally, each object is responsible for obtaining its own references to the objects it collaborates with (its dependencies). When applying DI, the objects are given their dependencies at creation time by some external entity that coordinates each object in the system. In other words, dependencies are injected into objects.

Now that’s cleared up, why would I want to make use of dependency injection in my Android app? Well I want my classes to be loosely coupled! This is advantageous because classes won’t depend on each other, modules are easier tested since they’re independent and overall maintenance will be simplified as one change shouldn’t have an adverse effect on another class. The next issue is to pick a suitable framework. The following libraries are, from what I can see, the most popular DI frameworks:

  • Dagger
  • Roboguice
  • Butterknife

Strictly speaking, Butterknife is not a DI framework, rather a view injection framework but I’m still including it as removes a huge amount of boilerplate code associated with view binding in Android.

 

Share This