RoboGuice is a must-have framework that brings the simplicity and ease of dependency injection to Android. Guice is a lightweight dependency injection framework for the Java platform. Dependency injection is a design pattern with the core principal of separating behavior from dependency resolution.
It allows for the removal of "hard-coded" dependencies and makes it possible to change them at compile or run time. This makes your code easier to write, change, unit text and reuse in other contexts. Your code becomes less of a boilerplate code and more of business logic.
With RoboGuice, you can bring all that to Android. RoboGuice allows you to use your own base classes along with your own essential methods or functionalities. This makes RoboGuice compatible with most of the libraries as well as allow you to strip nearly all platform boilerplates via various injections like View injections, Resource injections, SystemService injections, etc.
In simple terms, using RoboGuice in your Android project means writing less code, handling fewer errors, and having fewer headaches.
How to install RoboGuice
RoboGuice can be easily installed in your Gradle-based project by adding the following lines to your "dependencies" section of your app module’s gradle build file:
project.dependencies
compile "org.roboguice:roboguice:3.+"
provided "org.roboguice:roboblender:3.+"
How to use RoboGuice
RoboGuice is easy to use, if you follow these basic rules:
1. Extend your classes from the appropriate RoboGuice’s base classes like RoboActivity, RoboFragment, RoboService, RoboListActivity, RoboActionBarActivity, etc.
2. Inject views, resources or services using different annotations provided by RoboGuice such as "@InjectView", "@InjectResource", "@Inject", etc.
That’s it. Simple, isn’t it? You will feel more at home if you’re familiar with Java Annotations.
No RoboGuice vs. RoboGuice
Let’s compare how RoboGuice can minify your workload and improve your productivity by using this small example. Suppose we have an "activity_main.xml" layout file having all the views listed below.
Note: Comments are added to improve understandability of the code.
class NoRoboGuice extends Activity
// views
TextView name;
ImageView thumbnail;
// services
LocationManager loc;
// resources
Drawable icon;
String myName;
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // sets the layout
name = (TextView) findViewById(R.id.name); // boilerplate initialization
thumbnail = (ImageView) findViewById(R.id.thumbnail); // boilerplate initialization
loc = (LocationManager) getSystemService(Activity.LOCATION_SERVICE); // boilerplate initialization
icon = getResources().getDrawable(R.drawable.icon); // boilerplate initialization
myName = getString(R.string.app_name); // boilerplate initialization
name.setText( "Hello, " + myName ); // actual code
Here’s how it looks like with RoboGuice:
@ContentView(R.layout.activity_main) // sets the layout
class RoboGuice extends RoboActivity
// views
@InjectView(R.id.name) TextView name;
@InjectView(R.id.thumbnail) ImageView thumbnail;
// resources
@InjectResource(R.drawable.icon) Drawable icon;
@InjectResource(R.string.app_name) String myName;
// services
@Inject LocationManager loc;
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
name.setText( "Hello, " + myName ); // actual code
RoboGuice does not only reduce the code, but also helps to improve the readability and understandability of the source code. It takes the guesswork out of app development and your application code is no longer littered with the mechanics of the Android platform.
This advantage greatly helps at the time of debugging or updating the application as you can easily read and change the source code. You no longer need to search for the actual code in between the boilerplate initialization code because now only the actual code exists and RoboGuice does the boilerplate initialization automatically for you.
Easier Android Development with RoboGuice
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.