RoboGuice – An Injecting Library

You might be wondering about the word RoboGuice. But I am sure you all are curious to know exactly what it is and how it works and why we use it, etc. Up to my experience in android development sometimes it becomes very boring to write code like findViewById(), click listeners, etc. And here is when RoboGuice comes into the picture – to optimize the time for writing actual code logic for your app or game and avoid the code we used to and get bored to write again and again. In short RoboGuice lets you to become a lazy android developer who writes less but productive code.

There are so many 3rd party libraries and frameworks available to support android development, but, either we don’t know about them or we haven’t used them yet, or we have defined our own libraries and don’t need those or we never ever think about to use such libraries. There can be any reason, but if we use some of them in our android development then we could achieve tasks like better compatibility, enhanced UI, clean code and so on. This is what we will be learning in the rest part of this tutorial.

Let’s first have a look on Dependency Injection libraries. Dependency injection is a design pattern that allows us to remove and change the hard-coded dependencies from software, whether at run-time or compile-time.

Some of the popular Dependency Injection Libraries used commonly are:

robo1

Roboguice is a dependency injection framework for Android, which brings the simplicity and ease of Dependency Injection to Android, using Google’s own Guice library. If you always forget to check for null when you getIntent().getExtras() then don’t worry RoboGuice 2 will help you. If you use RoboGuice you don’t even need to worry about casting findViewById() to a TextView.

Using RoboGuice, allows you to inject View, Resource, System Service, or any other object. And less code means minimal occourances of bugs and less efforts required to write or modify particular code. RoboGuice also helps to generate readable and easy to follow code. below are the different usage of RoboGuice library.

Views Injection: Used to initialize views, for example:

 

@InjectView(R.id.mytextView) TextView txtView;

 

 

Resources Injection: Used to initialize and get resources, for example:

 

@InjectResource(R.string.title) String appTitle;

 

 

System services Injection: Used to initialize and access system services, for example:

 

@Inject LayoutInflater inflater;

 

 

POJO object Injection: Used to inject and initialize POJO object, for example:

@Inject Foo foo;

You will find JAR files for RoboGuice, in below links. You just need to download and add them to your claspath.

http://repo1.maven.org/maven2/org/roboguice/roboguice/2.0/roboguice-2.0.jar

http://repo1.maven.org/maven2/com/google/inject/guice/3.0/guice-3.0-no_aop.jar

http://repo1.maven.org/maven2/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar

Assuming you have done the download and added it to your classpath. Let’s have a comparison between a normal activity code and a RoboGuice activity code. Below is an example of general activity code:

 

public class NormalActivity extends Activity{

TextView txtView1;
ImageView imgView1;
String title;
Drawable icon;
LocationManager locationManager;
LayoutInflater Linflater;
NotificationManager notificationManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_normal);

txtView1 = (TextView) findViewById(R.id.mytextView1);
imgView1 = (ImageView) findViewById(R.id.myimageView1);
title = getString(R.string.title);
icon = getResources().getDrawable(R.id.ic_launcher);
locationManager = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);
Linflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
notificationManager = (NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);

txtView1.setText("RoboGuice Example");

}
}

 

Now see how you can minimise the code by using RoboGuice:

 

public class roboguiceActivity extends RoboActivity{

@InjectView(R.id.txtView1) TextView txtView1;
@InjectView(R.id.imgView1) ImageView imgView1;
@InjectResource(R.string.title) String title;
@InjectResource(R.drawable.ic_launcher) Drawable icon;
@Inject LocationManager locationManager;
@Inject LayoutInflater Linflater;
@Inject NotificationManager notificationManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_robo);

textView1.setText(title);

}
}

 

 

NOTE that you are required to extends either RoboActivity or RoboFragment. You must have realize the benefits of using RoboGuice till now. Let me list out the benefits of RoboGuice for you:

1.) No need to initialize views, if you want then Inject it using @InjectViews
2.) No need to initialize System service. If you need then Inject it using @Inject
3.) No need to initialize Drawable, string and other resources. If you require then Inject it using @InjectResource
4.) Less code => Less chances of bugs/issues
5.) Less code => Developer can save coding efforts and can focus on actual code logic of android app

This is all importants you must know to start with RoboGuice. Enjoy coding. See previous post in which I have covered Volley – A Networking library

Leave a Comment: