0

Im using dagger 2 for DI and im getting the message error:

MainFragmentContract.Presenter cannot be provided without an @Provides-annotated method.

But when I move my @Bind provideMainFragmentPresenter method from my MainFragmentModule to my ActivityBindingModule it build successfully.

Why it works when i move to the ActivityBindingModule class?, also if i move that method to my ActivityBindingModule class i cant add a scope because i get the message :

AppComponent scoped with scope.ApplicationScope may not reference bindings with different scopes.

There is my code.

AppComponent.java

@ApplicationScope
@Component(modules = {
        AndroidInjectionModule.class, // Important
        ApplicationModule.class,
        NetworkModule.class,
        ActivityBindingModule.class,
})
public interface AppComponent extends AndroidInjector<MyApplication> {

    void injectApplication(MyApplication myApplication);

    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(Application application);

        AppComponent build();

    }
}

ActivityBindingModel.java

@Module
public abstract class ActivityBindingModule {

    @ContributesAndroidInjector(modules = {MainModule.class})
    @ActivityScope
    abstract MainActivity mainActivity();

    @ContributesAndroidInjector(modules = {LoginModule.class})
    @ActivityScope
    abstract LoginActivity loginActivity();

    @ContributesAndroidInjector(modules = MainFragmentModule.class)
    @FragmentScope
    abstract MainFragment provideMainFragment();

}

MainModule.java ( activity )

@Module
public abstract class MainModule {

    @Binds
    @ActivityScope
    public abstract MainContract.Presenter
    provideMainActivityPresenter(MainActivityPresenterImpl presenter);

}

MainFragmentModule.java

@Module
public abstract class MainFragmentModule {

    @Binds
    public abstract MainFragmentContract.Presenter
    provideMainFragmentPresenter(MainFragmentPresenterImpl presenter);


}
w33haa
  • 314
  • 2
  • 14
  • Please share the _full_ error message (e.g. see [_here_ for some details](https://stackoverflow.com/q/44912080/1837367)). You should check where you use `MainFragmentContract.Presenter`, as it would seem you not only try using it in your `MainFragment`, but also some other place (where you didn't add the module) which is why it will work if you move the binding to a higher level – David Medenjak Aug 21 '19 at 07:11

1 Answers1

4

Obviously, it won't work because if you take a look at your dependency graph you'll see that it doesn't know how to create an instance of MainFragmentContract.Presenter.

You have to connect it to your application-level component by specifying the module which knows how to create the instance of MainFragmentContract.Presenter.

So instead of moving provideMainFragmentPresenter method from my MainFragmentModule to my ActivityBindingModule do it as follows:

@ApplicationScope
@Component(modules = {
    AndroidInjectionModule.class, 
    ApplicationModule.class,
    NetworkModule.class,
    ActivityBindingModule.class,
    MainFragmentModule.class     //This line right here
})
public interface AppComponent extends AndroidInjector<MyApplication> {

void injectApplication(MyApplication myApplication);

@Component.Builder
interface Builder {
    @BindsInstance
    Builder application(Application application);

    AppComponent build();

 }
}
Ashu Tyagi
  • 1,400
  • 13
  • 25