Thursday, January 12, 2012

Dependency Injection for Android with Google’s Roboguice

For most developers, writing Android apps means writing a ton of Java. On large projects, trying to keep patterns straight can be pretty difficult. Useful frameworks for Android are few and far between, which leaves the programmer to pick up a lot of the details of the system. Far too many bugs are created this way.

The other day, I came across Roboguice, a port of Google's Guice framework over to Android. If you've never heard of it, it’s a simple framework to streamline Dependency Injection within Android. The framework still seems to be a hidden gem. I couldn’t find many developers that even knew it existed.

What is Dependency Injection, really?


In short, Dependency Injection is a way to decouple specific types of objects in your code. It allows your program to be far more modular with minimal extra effort. If you have ever heard of the Factory, then you have at least a faint idea of what Dependency Injection is.

What is Roboguice… and what’s up with the name?


Roboguice is actually just an implementation of Google's Guice within Android – sorry, I don’t have a clue why they picked the name. It lets an application have all the benefits of Dependency Injection with just a handful of lines of code. In Google's words, "Guice alleviates the need for factories and the use of new in your Java code." Like everything else Google related, an IO video gives a pretty good idea about it.



Instead of creating handfuls Factories for every object that needs Dependency Injection, you can simply use RoboGuice to bind a particular sub type to a base type, which removes the need to have a Factory class for every type of object that could possibly be changed out in a program. Of course there are other benefits to using the framework, but this is the major benefit that many developers, beginners and experts alike, would see very quickly. Using the framework is actually pretty easy. I would say the heart of the framework can be described with the following line of Java code.+
bind(BillingService.class).to( RealBillingService.class);
These bind method calls are used within a new type of class called Modules. What this line means is, during runtime, any instance of BillingService that is marked correctly  should actually be RealBillingService. This one line barely scratches the surface though. You can see the rest of this example, which includes how to build modules and using the @Inject method, here.
Using this framework decreases the amount of objects you need to create within the program. Any factory objects that are used within the project can be replaced. Any object that doesn’t have to be created saves valuable processor cycles and battery power for the user. All for the cost of adding a few libraries to your project, and actually making your code easier to understand in the process. Sounds like a no brainer to me.

5 comments:

  1. Very nice summary - looking forward to experimenting with this.

    I don't know why but some of your words are being broken in the middle when at the end of a line, rather than wrapping. e.g.

    making your code e
    asier to understand

    ReplyDelete
  2. I agree, great summary!

    ReplyDelete
  3. Sounds cool, I'm going to check it out. Always hesitant to invest too much into a framework lacking support though..

    ReplyDelete
  4. plz post some blogs related to iPhone game Development,

    ReplyDelete