Android, Eclipse, Maven and (Robo)Guice together?

I’m sure that when you trays automated dependency management and application building/deploying you will never want to abandon this kind of comfort that it gives. The same situation is with dependency injection; when you will get it ideas. In “normal” desktop, web, or server side project’s integrating with such tools like maven and guice isn’t that complicated like in Android projects.
Android tooling is very consistent and the ADT plug-in isn’t integrated with maven. This isn’t a problem when application is relatively small, or when you don’t want to use continuous integration tools like Hudson. But when you enter Android world from “enterprise” web application like I do, you surely would like to use CI, DI and lots more of cool stuff that was available before.

In this post I’ll present simple Android application that will have Maven and Guice support.

1. Eclipse, Android and Maven

I assume that you already have installed and configured ADT and m2eclipse plugins. To have it working properly we need to install m2eclipse-android-integration from theirs update site:

2. Create a simple android project

Using Android project creator create new project and create a pom.xml file and configure maven-android-plugin. Here is a sample minimal pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""

Remember replace ${path-to-android-sdk} with proper path. After that there are only two things that should to be done. From project context menu and select “Maven” -> “Enabled dependency management” and secondly “Maven” -> “Update project configuration” and remove standard JRE library from project build path.
That was quite simple isn’t it? There is one additional thing worth to notice, if yours application would be using Google Maps API you should use project maven-android-sdk-deployer to automatically install proper jar’s in local m2 repository. Whole installation process is described in README.markdown file.

3. Android and Guice

Now when we have enabled dependency management we can add some dependencies ;). First of all we will need Guice 2.0 without AOP (we cannot use Aspect Oriented Programming on Android). Because this jar isn’t available in any known maven repository we should download it from Guice download site and install it in our local repository:

mvn install:install-file -Dfile=./guice-2.0-no_aop.jar\
      -DartifactId=guice-no_aop -Dversion=2.0 -Dpackaging=jar -DgeneratePom=true

after that we can add new dependency to ours pom.xml


Theoretically we can use Guice without any addons … but using Roboguice we can achieve lots more than simple dependency injection eg. using @InjectView and @InjectResource we can easily inject views and resources without casts. Also there are preconfigured Guice modules that have configured standard bindings. To add Roboguice into ours project first we should define new repository from with it will be donloaded

    <name> Repository for Maven</name>

Secondly the dependency should be also added


To have everything working we should create new class eg. ExampleApplication that will extend roboguice.application.RoboApplication

import roboguice.application.RoboApplication;
public class ExampleApplication extends RoboApplication {
	protected void addApplicationModules(List<Module> modules) {

In method addApplicationModules we can add any module that extends In our example we don’t need to add any extra module. Next we should edit AndroidManifest.xml file and change line

<application android:icon="@drawable/icon" android:label="@string/app_name">


<application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".ExampleApplication">

If we would like to benefit from @InjectView or @InjectResource annotation our activity class should extends roboguice.activity.RoboActivity instead of It is very important to notice that any injection can occur after setContentView() method is called. Therefor we cannot inject main activity layout! Here is an example ExampleActivity class that have injected button:

import roboguice.activity.RoboActivity;
import roboguice.inject.InjectView;
import android.os.Bundle;
import android.widget.Button;
public class ExampleActivity extends RoboActivity {
	Button button;
	/** Called when the activity is first created. */
	public void onCreate(final Bundle savedInstanceState) {
		button.setText("Injected button");

That is all I want to share with you. I’ve hope that this information would be useful for you 😉

8 Comments Android, Eclipse, Maven and (Robo)Guice together?

  1. Pingback: Tweets that mention Dariusz [LocK] Łuksza » Android, Eclipse, Maven and (Robo)Guice together? --

  2. Ricardo

    No worries.

    BTW could I copy some of your article here to update my “Getting Started” wiki page please? It needs a re-write and your descriptions are very simple and clear.

  3. Ricardo

    Just a quick note: you’re not adding the guice dependency with it’s classifier (no_aop):

    mvn install:install-file -Dfile=./guice-2.0-no_aop.jar -DartifactId=guice -Dversion=2.0 -Dpackaging=jar -DgeneratePom=true -Dclassifier=no_aop

  4. hsz

    RIght now it is possible to add dependency to guice with specified version as 2.0-no_aop


Leave a Reply

Your email address will not be published. Required fields are marked *