23

I'm debating using guice in an android project that is quite complex and has a lot of business logic. Guice seems like a good fit, but whenever I start reading deeper into it, it starts to look more complicated than it needs to be.

One thing I don't understand is: if Guice is so great and the best way to write java code, how come there is so little Android code that uses Guice... and why didn't Google use guice internally for Android?

Manfred Moser
  • 29,539
  • 13
  • 92
  • 123
Otto
  • 1,675
  • 2
  • 19
  • 30
  • Android was part of an acquisition of Andy Rubin's Android startup, and the code base was well established by the time Google became involved, so it's not terribly surprising that it might not have used Google's dependency injection framework. – emmby Oct 19 '11 at 09:41

4 Answers4

27

Actually google discourages using Guice or RoboGuice in android applications due to memory overhead.

Source:

http://developer.android.com/training/articles/memory.html#DependencyInjection

5.11.2014 Edit:

There is a dedicated fast dependency injection library for android. I can see more and more people using it:

http://square.github.io/dagger/

13.04.2015 Edit: Google released its own version of dagger, which does not use reflection in runtime: http://google.github.io/dagger/

bryn
  • 291
  • 1
  • 3
  • 5
27

Guice totally makes sense to be used and in fact is used in a whole bunch of applications. The extension RoboGuice adds some niceties for Android that makes it super productive to use.

In fact I can not imagine writing an Android app without it. Too painful.

Check out the links to apps using Roboguice on the website (e.g. Google Docs, OpenTable...). Also other apps like the Square app are known to use Guice directly.

It totally makes sense .. go do it!

Together with Robolectric it will also make your testing efforts easier.

PS: I am a committer on RoboGuice so I am partial ;-)

PPS - June 2013: Recent developments have given rise to other annotation/dependency injection based frameworks that do most of the work at build time and therefore avoid the performance hit of the runtime reflection (that is slow on Android) and are therefore more suitable for performance critical work - check out Dagger and AndroidAnnotations if you are interested in that.

Manfred Moser
  • 29,539
  • 13
  • 92
  • 123
13

You know there is RoboGuice? It's Guice for Android.

Peterdk
  • 15,625
  • 20
  • 101
  • 140
12

The problem with demonstrating the strengths of a dependency injection framework is that it isn't possible to achieve it with a simple Hello World application. These frameworks show their value only in big systems with a lot of complexity. Also, they have a somehow steep learning curve.

Therefore it is quite normal that you can't find enough tutorials - open source projects that use Guice. This will be most often used in enterprise applications that do not get published.

As why Google doesn't use Guice, Guice doesn't fit everywhere. It adds a perfomance overhead and it doesn't make sense to use it in places, where it isn't needed.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194