Does it make sense to use Guice for Android
Asked Answered
D

4

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?

Deluca answered 18/10, 2011 at 19:56 Comment(1)
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.Sailmaker
F
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.

Frangos answered 18/10, 2011 at 20:9 Comment(0)
C
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.

Cardigan answered 18/10, 2011 at 21:27 Comment(1)
+1 for the "PPS" and the reference to Dagger and AndroidAnnotationsRooster
M
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/

Mohler answered 5/3, 2014 at 8:37 Comment(0)
C
13

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

Chaparajos answered 18/10, 2011 at 20:11 Comment(1)
RoboGuice is no longer supported unfortunately (As of August 2016)Engedus
F
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.

Frangos answered 18/10, 2011 at 20:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.