0

しばらくの間、Roboguice を使用してきましたが、github のソース コードを見ると、通常は使用しない、または必要としない不必要なものがたくさん含まれているため、Guice だけで作業を開始することにしました。これの唯一の欠点は、Android コンテキストを挿入して自分で構成する必要があることです。そのため、最終的には次のようにします。

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);
   }

}

次に、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);
    }
}

それは良いアプローチですか?今のところ、sharedPreference インスタンスを作成してそれを操作するためにコンテキストが必要なセッション マネージャーなどで使用するコンテキストのみが必要です。

最後に: My Objects のみを注入し、Android に関連するものは何も注入したくない場合、Roboguice を Guice に置き換えるのは良い方法ですか? また、Roboguice よりも軽量で依存性の低いものを使用してください。結局、ダガーは似たようなことをしますよね?

4

1 に答える 1

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.

于 2014-11-27T22:28:56.787 に答える