Home > android, dependency injection, roboguice, unit testing > Dependency Injection in Android

Dependency Injection in Android

I’m a big fan of the Android OS. I like its openness in the same way as I like the openness of Java. Besides, programming in Android has a flavor so close to Java that I cannot be other than sympathetic towards it.

Since 2005 I got unit-test infected. Well, I feel I’m not infected that bad because I still tend to write the main functionality first. Then I write a test that covers that main functionality and (eventually) fix the errors shown by it. Then I just add test after test until I feel I covered all the functionality required.

Writing tests wouldn’t be so much fun if Spring was not around. Having those mocks automatically injected for me makes me be more productive when writing tests: I don’t have to worry to much about the initialization phase since it is taken care for me.

I was wondering these days if I can use DI and unit testing in Android applications as in my usual Java applications. Fortunately I found out that there are two 2 possibilities:

  1. SpringME
  2. Guice without AOP support

Well.. I’m happy with all these alternatives but making a choice is not very easy. I would go for SpringME since I already have working experience with Spring. However, it is based on XML files configurations which is kind of old and sometimes cumbersome to work with. On the other hand Guice 2.0 supports annotations and it’s family to Android, so I expect it to be better maintained in the future.

Both these libraries can be found on Maven repositories, so I cannot differentiate here (I’m a big fan of Maven). However, there could be another reason to choose Guice. I found a possibly-useful library named Roboguice that seems to be made especially for Android and it’s built on top of Guice itself. Plus it offers some help in terms of unit testing Android. However, it looks like it’s not maintained very often (at the moment of writing it showed Medium on the Activity) and its documentation is pretty scarce, so I cannot speculate if it can help me or not.

For the moment I’ll start with Guice no-AOP.. I’ll post again with my findings so come back if you are interested.

Advertisement
  1. iscutaru
    November 12, 2010 at 9:42 am | #1

    Later I found this blog:

    http://luksza.org/2010/11/06/android-eclipse-maven-and-roboguice-together/

    It provides the basic setup for android, maven and Guice. I will definitely give it a try (when I’ll find some time)

  1. No trackbacks yet.
You must be logged in to post a comment.
Follow

Get every new post delivered to your Inbox.