0

I have been using Roboguice for a while but as I see the source code in github, it has a lot of unnecessary stuff that I am not typically use it or need it, so I decided to start working only with Guice. The only drawback with this is that I need to inject the Android Context and configure by myself, so I end up doing this:

public class AndroidDemoApplication extends Application {

    private static AndroidDemoApplication instance;
    private static final Injector INJECTOR = Guice.createInjector(new AndroidDemoModule());

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }

    public static Context getAppContext() {
        return instance;
    }

    public static void injectMembers(final Object object) {
        INJECTOR.injectMembers(object);
   }

}

And then in my class that extends AbstractModule:

    public class AndroidDemoModule extends AbstractModule {

    @Override
    protected void configure() {

        bind(Context.class).toProvider(new Provider<Context>() {
            @Override
            public Context get() {
                return AndroidDemoApplication.getAppContext();
            }
        });
//        .in(Singleton.class);
    }
}

Is it a good approach? For now I just only need the Context to use in let say a Session manager which needs the context to create a sharedPreference instance and play with it.

Finally: is it a good approach to replace Roboguice with Guice when I only want to inject My Objects and not anything related to Android, only the Context? And use something more lightweight and less dependent than Roboguice. After all Dagger does something similar, right?

Nicolas Jafelle
  • 2,661
  • 2
  • 24
  • 30

1 Answers1

0

it has a lot of unnecessary stuff that I am not typically use it or need it

This does not make sense : use ProGuard in order to get rid of the classes you are not using (and minify for the resources with Gradle 0.14).

There is a reason behind the development of RoboGuice : Guice has been written with the desktop in mind. It uses reflection quite heavily. It is not an issue on desktop, but on mobile reflection perform pretty badly.

You should either stick with RoboGuice or maybe consider Dagger 2. It is right now pretty close to a release and has been written by the Guice guys as a modern and fast (no reflection at all, the magic happens at compile time) dependency injection lib for Android.

Teovald
  • 4,369
  • 4
  • 26
  • 45
  • @NicolasJafelle just to be clear : here is a presentation of Dagger 2 : https://www.youtube.com/watch?v=oK_XtfXPkqw . And the repo is located here https://github.com/google/dagger and not at square/dagger. – Teovald Nov 28 '14 at 18:48
  • Is dagger the same as Guice? I mean.. it is not related to Android Framework at all, and you have to configure the Context and all of that, right? So... If you recommend stick with Roboguice which is for Android Apps why you recommend better go for Dagger 2? It is just for curiosity. Thanks! – Nicolas Jafelle Nov 30 '14 at 14:30
  • To schematize a little bit : Guice was a first attempt at dependency injection. I am not too familiar with RoboGuice, but it looks pretty close to Guice with the addition of some Android helper methods, like view injection (the concept might look similar, but it is very different from DI). The limitation of Guice for Android is that it has been written for huge server farms and it does not scale well to Android. Specifically, Android is not very good with reflection and Guice uses it a lot. – Teovald Dec 01 '14 at 01:18
  • As a result, you get slower app and longer app startup time. So Dagger came along (If my memory serves me well, from ex-googlers at square that worked on Guice). Same goal, Dependency injection, but this time with way more things generated at compile time, so way less reflection. – Teovald Dec 01 '14 at 01:19
  • Finally, Dagger comes back to Google with Dagger 2. The Java Core Team at Google pretty much rewrote Dagger in order to 1) completely eliminate reflection 2) make generated code readable (so it is way easier to debug). – Teovald Dec 01 '14 at 01:22
  • Thank you @teovald !! I am reading about Dagger 2 and seems pretty awesome! – Nicolas Jafelle Dec 01 '14 at 11:49