Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it make sense to use Guice for Android

Tags:

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?

like image 688
Otto Avatar asked Oct 18 '11 19:10

Otto


People also ask

Why we use Google Guice?

Beyond Dependency Injection, the benefits of using Google Guice is: Guice has a very clean implementation of constructor Injection. As you can see from the example you just add @Inject annotation constructor. Guice also has setter Injection using the same annotation.

Is dagger faster than Guice?

I know that dagger creates injection at compile time by generating code and hence its performance is better than Guice, which do it at runtime.

What is Android dependency injection?

Dependency injection (DI) is a technique widely used in programming and well suited to Android development. By following the principles of DI, you lay the groundwork for good app architecture. Implementing dependency injection provides you with the following advantages: Reusability of code. Ease of refactoring.

Who uses Guice?

Who uses Guice? 12 companies reportedly use Guice in their tech stacks, including Wealthsimple, backend, and Redfin.


2 Answers

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.

like image 196
Manfred Moser Avatar answered Nov 07 '22 11:11

Manfred Moser


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/

like image 28
bryn Avatar answered Nov 07 '22 12:11

bryn