Dagger 2 error: dependency “cannot be provided without an @Inject constructor” while it actually annotated with @Inject

后端 未结 6 523
北荒
北荒 2020-12-30 19:02

I\'ve started using Dagger 2 and faced strange issue that looks like a bug to me.

I have 3 modules, that are composed into one subcomponent, which in turn extends/pl

6条回答
  •  死守一世寂寞
    2020-12-30 19:20

    I got this same error because I forgot to expose the objects provided by the modules in the parent component to the other components that are depend on it.

    Parent component example:

    @Singleton
    @Component(modules = {AppModule.class})
    public interface AppComponent {
        AppPref exposeAppPref(); /* my issue was caused by forgot this line,
    the method name doesn't matter, what matters is the object type AppPref provided in the AppModule 
    that you want it to be available in the component that declares this component as one of its dependencies*/
    }
    

    Sample component that makes the above component as a dependency

    @UserScope
    @Component (dependencies = {AppComponent.class})
    public interface ActivityComponent {
        void inject(MainActivity activity);
    }
    

    Update:

    AppModule:

    ...
        @Provides
        @Singleton
        AppPref provideAppPref() {
            return appPref;
        }
    ...
    

提交回复
热议问题