AdSense Dashboard
Social Networks

« Weatherman 1.3 now available in the Android Market | Main | Not a bad start. »
Tuesday
Jan052010

Guice support for Android = Happy Me.

There are a few things I just can’t live without in projects these days.  These include stuff like Functions, Predicates, filtered and transformed Iterables, the immutable collections implementations, and a raft of other stuff from the Google commons.  If it wasn’t open-source, writing java code these days would feel like working with flint and stone.

Also included in that how-do-people-live-without-this-stuff list is decent dependency injection.

I didn’t notice until I was a long way into the project that Guice now has support for Android.  At the moment, I’m using Guice 2.0 without AOP, Multibindings support, and assisted inject, and quite frankly, the code is almost instantly better for it.  Constructors now only contain what the local object needs to use to do its job, and everything is an awful lot more readable.

It’s also nice not to have to pass around awareness of the fact that Android’s context classes are a pig’s ear of methods.  There’s nothing wrong with using composition instead of inheritance, folks; your application classes shouldn’t need to be a combination of implementations every single interface in the SDK.  That’s not discoverable, that’s just a wall of noise.

Anyways, four hours of un-hardwiring class construction resulted in a much easier second refactoring to start splitting foreground from background services, which took all of an hour and only involved creating a new module for the new service class, and poof!

I still wonder how people get on without DI, and why on earth this isn’t core java yet.  Mind you, I wonder that about Joda, too.  And closures.  And just about any other modern feature that doesn’t make it into the now-overpoliticized trial by fire of the JDK.

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (4)

This post makes me happy. I'm intending to someday make Android more DI-friendly, and posts like this are great encouragement.

January 31, 2010 | Unregistered CommenterJesse

How did you get guice-multibindings-2.0.jar and guice-assistedinject-2.0.jar to both work in one Android project? I get the following error when I try to do so:

Error generating final archive: duplicate entry: LICENSE

because both jars contain a LICENSE and a NOTICE file.

February 14, 2010 | Unregistered CommenterRobert

Oh, I just removed the offending files from both jars with Ubuntu's archive program.

February 14, 2010 | Unregistered CommenterRobert

Yup. This is one for Jesse, really - having all the jars contain LICENSE and NOTICE files with the exact same name results in some pain when attempting to use both within the same Android apk; it expects to be able to unzip everything without conflicts, or it all goes horribly wrong.

I did what you did - just remove the LICENSE/NOTICE from the associated jar. That's going to make redistribution as a part of dependent libraries suck, of course, and so they'll have to be put back before that's possible.

July 3, 2010 | Registered CommenterGregory Block

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>