Skip to content
Stéphane Nicolas edited this page Jun 25, 2014 · 26 revisions
Clone this wiki locally

A brief example of what RoboGuice does

RoboGuice is a framework that brings the simplicity and ease of Dependency Injection to Android, using Google's own Guice library. If you've ever used Spring (the #1 enterprise framework on Java, now more popular than J2EE itself) or Guice, you already know how convenient this style of programming can be.

To give you an idea, take a look at this simple example of a typical Android activity:

    class AndroidWay extends Activity { 
        TextView name; 
        ImageView thumbnail; 
        LocationManager loc; 
        Drawable icon; 
        String myName; 

        public void onCreate(Bundle savedInstanceState) { 
            name      = (TextView) findViewById(; 
            thumbnail = (ImageView) findViewById(; 
            loc       = (LocationManager) getSystemService(Activity.LOCATION_SERVICE); 
            icon      = getResources().getDrawable(R.drawable.icon); 
            myName    = getString(R.string.app_name); 
            name.setText( "Hello, " + myName ); 

This example is 19 lines of code. If you're trying to read through onCreate(), you have to skip over 5 lines of boilerplate initialization to find the only one that really matters: name.setText(). And complex activities can end up with a lot more of this sort of initialization code.

Compare this to the same app, written using RoboGuice:

    class RoboWay extends RoboActivity { 
        @InjectView(             TextView name; 
        @InjectView(        ImageView thumbnail; 
        @InjectResource(R.drawable.icon)   Drawable icon; 
        @InjectResource(R.string.app_name) String myName; 
        @Inject                            LocationManager loc; 

        public void onCreate(Bundle savedInstanceState) { 
            name.setText( "Hello, " + myName ); 

In this example, onCreate() is much easier to take in at a glance. All the platform boilerplate is stripped away and you're left with just your own app's business logic. Do you need a SystemService? Inject one. Do you need a View or Resource? Inject those, too, and RoboGuice will take care of the details.

RoboGuice's goal is to make your code be about your app, rather than be about all the initialization and lifecycle code you typically have to maintain in Android.

Visit the Installation pages for all the details on how to configure your first RoboGuice application.

Additional Examples

Documentation on how to use RoboGuice is never as far along as we'd like it to be, but we do have a sample application that should get you off the ground. If you're looking for an idea of what RoboGuice can do, check out the Astroboy example.