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

后端 未结 6 521
北荒
北荒 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:31

    Seems it is the same kinda error dagger reports for many mistakes. In my case, my target injection was expecting concrete class (Presenter) where as the module that provides presenter was returning only the interface (DemoContract.Presenter)

    So changed from

    @Inject
    public Presenter mDemoPresenter;  
    

    to

    @Inject
    public DemoContract.Presenter mDemoPresenter;
    

    and module that provides presenter looks like this:

    @Module
    public class DiDemoPresenterModule {
        private final DemoContract.View mView;
    
        DiDemoPresenterModule(MainActivity mView) {
            this.mView = mView;
        }
    
        @Provides
        public DemoContract.Presenter providesDemoPresenter(Repository repository) {
            return new DemoPresenter(repository, mView);
        }
    }
    

提交回复
热议问题