Using RoboGuice 2.0 on Android

4 Flares Twitter 0 Facebook 1 StumbleUpon 0 Google+ 3 LinkedIn 0 4 Flares ×

About a month ago I decided to get familiar with Android – I’ve been meaning for some time to get my skills up on mobile development and I finally got around to it. My primary skills are really in .net and web based technologies, so the most natural choice would have been windows 8 mobile – but I wanted to learn something totally different, and windows 8 mobile is really not established itself yet.

Another technology which would have probably been much more natural to me is something like phonegap – it uses HTML5 which I’m already skilled with, and provides a good first punt at cross device support. My only real concerns with phonegap is firstly whether there would be comprises on user experience, and secondly will the benefits of cross platform result in a more difficult development experience. These are questions that I cant find any reliable answers for – so I’ve decided to learn android first, then have a go with phonegap and make my own mind up.

Anyway, back to topic – I’ve been a fan of dependency injection for some time, and see the benefit in using an IOC container – enter RoboGuice. RoboGuice is essentially an IOC container for android, based on google guice. The biggest problem I’ve found with RoboGuice is the documentation…is appalling and/or none existent. There are numerous promises that this is going to be improved, but right now its lacking and out of date at best. So in short, heres how to get started with RoboGuice as it stands today (version 2.0).

Getting Started with RoboGuice 2.0

There are only a few steps to getting started with RoboGuice, and its really simple (when you know how). I’m using Eclipse so this is how I’ve got it running:


  • Add a reference to them in your project properties
    • Right click your project and select Properties >> Java Build Path >> Add JARs…Capture4
    • Select the 3 jar files and click ok
  • Create a roboguice module
    • Create a class in your package that inherits from AbstractModule
    • Implement the required method – i.e. configure (we’ll come back here later)
  • Add roboguice.xml to your values folder:


  • Add the following contents to the roboguice.xml file:  [code] full package and name of your roboguice module here [/code]

At this point roboguice should be ready to run – so I’d make sure your project builds, deploys and runs!

RoboGuice Usage

Using roboguice is fairly simple (in the scenarios I’ve using so far) – the following is a couple of ways it can be utilised:

Injecting Views

The easiest examples to find are injecting views into your activities – its really simple and cleans up your code a lot:

  1. Your activity must inherit from the ‘Robo’ equivalent activity – e.g. RoboActivity
  • You can then declare your views with a syntax such as:

Injecting Application Services

Something else that I find really useful is injecting my application services, for example if I want to inject my repository:

  1. Set up the configuration in the roboguise module:
  • Then use the repository in you activity as below:

Further examples

As I work through and find other examples of RoboGuice use I’ll post more and refer back to them here.

, ,

The Essential App Marketing Kit
Subscribe To My Newsletter To Get an Entire Chapter From The Book for FREE
Never display this again
4 Flares Twitter 0 Facebook 1 StumbleUpon 0 Google+ 3 LinkedIn 0 4 Flares ×