AgentKnopf edited this page Nov 30, 2015 · 8 revisions
Clone this wiki locally

greenInject simplifies Android development by injecting various resources into your classes automatically. A typical example is that you need views, or the values of EditText views in your Activity. All you have to do is to annotate your fields and trigger injection. Say goodbye to boring boilerplate code. In a way, greenInject is RoboGuice without Guice. It gives you the cool injection syntax without using the big IOC framework Guice. Guice is awesome, but it might be just too large for your Android project: the jars for RoboGuice plus Guice are >500 KByte, while greenInject is currently less than 20 KByte. It's likely that your app becomes smaller when using greenInject consistently because you need less code.

Let's look at an example:

public class TestActivity extends Activity {
	@InjectView(id = R.id.textView1)
	TextView textView;

	@InjectResource(id = R.drawable.icon)
	Bitmap iconBitmap;
	@InjectExtra(key = "color")
	String color;

	protected void onCreate(Bundle savedInstanceState) {
		// textView, iconBitmap, and color are available now

As you can see, injection is triggered explicitly. The advantage of this approach is that you can use the standard Activity class hierarchy. Also, you have full control about the injection timing.

There's more to greenInject: @OnClick annotations and @Value bindings.

To get started, download the run time library, or get the source. The source includes a unit test project, which is a good starting point to see how to use greenInject. greenInject is available under the Apache 2 licence. You can use it for commercial projects for free under the license terms.

greenInject just got released, read about current Restrictions.