dagger2 error “android.app.Application cannot be provided without an @Inject constructor or from an @Provides-annotated method”

一个人想着一个人 提交于 2019-12-01 19:16:33

In your specific case, you're missing:

@Binds Application bindApplication(App app);

This is important because dagger.android will automatically include a binding to the specific Application, Activity, Fragment, Service, BroadcastReceiver, etc subclass but not the general object. (You'd be able to inject App but not Application, and YourActivity but not Activity.) If you want to indicate to dagger.android that it should fulfill requests for Application using your App instance, you have to include a binding as above.

Generally speaking, this is a pretty safe thing to do for Application, Activity, Service, and BroadcastReceiver, but not Fragment (native or in the compat libraries); this is because dagger.android respects nested fragments, and in that case it would be ambiguous which Fragment to inject.

Though you can provide the Application through a Module and instance field as in luffy's answer, this is more boilerplate than you need, and is also less optimized: Dagger will write code to call your @Provides method, whereas you can declaratively tell Dagger to reuse an existing binding using @Binds or write a static @Provides method that avoids invoking the call on an instance.

See the Dagger advantages of @Binds and the Android advantages of static dispatch.

In AppModule, you @Binds a Application, but Application is not provide by any other module.

Because the Application is created by Android framework, you should let AppModule provide the Application from outside.

@Module
public class AppModule {
    private Application mApp;
    public AppModule(Application app) {
        mApp = app
    }
    @Singleton
    Context provideContext() {
        return mApp
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!