-
Notifications
You must be signed in to change notification settings - Fork 824
Home
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) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name = (TextView) findViewById(R.id.name);
thumbnail = (ImageView) findViewById(R.id.thumbnail);
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:
@ContentView(R.layout.main)
class RoboWay extends RoboActivity {
@InjectView(R.id.name) TextView name;
@InjectView(R.id.thumbnail) ImageView thumbnail;
@InjectResource(R.drawable.icon) Drawable icon;
@InjectResource(R.string.app_name) String myName;
@Inject LocationManager loc;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(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.
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.
- For Projects Using Gradle
- For Projects Using Maven
- Manual Installation for Older Projects
- Inheriting from RoboGuice Classes
- Upgrade Instructions from RoboGuice 2
- Your First View Injection
- Your First Resource Injection
- Your First System Service Injection
- Your First POJO Injection
- Singletons and ContextSingletons (wiki/Understanding Scopes)
- Your First Custom Binding
- Your First Injected Fragment
- Your First Injected Service and BroadcastReceiver
- Your First Testcase
- Your First Injection into a Custom View class
- Your First Injected ContentProvider
- Using Events in your RoboGuice application
- Logging via Ln
- RoboGuice Standard Injections
- How Injection Works
- When Injection Just Works, and when you have to call injectMembers()
- The Difference between Global and Context-scoped Injection
- Analyzing a Guice Stack Trace
- What's the difference between Nullable and Optional?
- RoboBlender wiki
- Use ProGuard with RoboGuice
- Define advanced custom bindings
- Remove or replace RoboGuice's default bindings
- Use your own BaseActivity with RoboGuice
- Inject into an object that you don't instantiate
- Dealing with Circular Dependencies
- Work with Library Projects
- Deal with ComputationException due to StackOverflowError
- Taming Fragmentation using RoboGuice