微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Dagger 2对于Android“没有@Provides注释的方法就无法提供”

我正在尝试为Android使用最新版本的Dagger 2 V2.11
这是我的代码
AppComponent:

@Singleton
@Component(modules = {
        AndroidInjectionModule.class,
        AppModule.class,
        ActivityBuildersModule.class,
        FragmentBuildersModule.class
})
public interface AppComponent {
    void inject(MyApplication myApplication);

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

        AppComponent build();
    }

    @ExceptionRequestsQualifier
    ExceptionRequestsServices exceptionRequestsServices();

}

AppModule:

@Module(includes = {ActivityModule.class, FragmentModule.class})
public class AppModule {

    @Provides
    Compositedisposable provideCompositedisposable() {
        return new Compositedisposable();
    }

    @Provides
    @ExceptionRequestsQualifier
    ExceptionRequestsServices provideExceptionRequests() {
        return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(APIConstants.EXCEPTION_REQUESTS_BASE_URL)
                .build()
                .create(ExceptionRequestsServices.class);
    }

    @Singleton
    @Provides
    NetworkManager provideNetworkManager(Application app) {
        return new NetworkManager(app);
    }

}

ActivityBuildersModule:

@Module
public abstract class ActivityBuildersModule {

    @ActivityScope
    @ContributesAndroidInjector
    abstract ExceptionRequestsActivity contributeExceptionRequestsActivity();

}

ActivityModule:

@Module()
public abstract class ActivityModule {

    @Provides
    @ActivityScope
    static ExceptionRequestsMvpPresenter<ExceptionRequestsMvpView> bindExceptionRequestsPresenter(
            ExceptionRequestsPresenter<ExceptionRequestsMvpView> presenter) {
        return presenter;
    }

}

FragmentBuildersModule:

@Module
public abstract class FragmentBuildersModule {

    @FragmentScope
    @ContributesAndroidInjector
    abstract AddApplicantFragment contributeAddApplicantFragment();

    @FragmentScope
    @ContributesAndroidInjector
    abstract PledgeFragment contributePledgeFragment();

}

FragmentModule:

@Module()
public abstract class FragmentModule {

    @Provides
    @FragmentScope
    static AddApplicantMvpPresenter<AddApplicantMvpView> bindAddApplicantPresenter(
            AddApplicantPresenter<AddApplicantMvpView> presenter) {
        return presenter;
    }

    @Provides
    @FragmentScope
    static PledgeMvpPresenter<PledgeMvpView> bindpledgePresenter(
            PledgePresenter<PledgeMvpView> presenter) {
        return presenter;
    }

}

AddApplicantPresenter:

public class AddApplicantPresenter<V extends AddApplicantMvpView> extends BasePresenter<V> implements AddApplicantMvpPresenter<V> {

    @Inject
    @ExceptionRequestsQualifier
    ExceptionRequestsServices mExceptionRequestsServices;

    @Inject
    NetworkManager mNetworkManager;

    @Inject
    public AddApplicantPresenter(Compositedisposable compositedisposable) {
        super(compositedisposable);
    }

}

AddApplicantMvpPresenter:

@FragmentScope
public interface AddApplicantMvpPresenter<V extends AddApplicantMvpView> extends MvpPresenter<V> {

    void addApplicant(String name, String qatarId,
                      String date, String mobile,
                      ChosenImage chosenImage);

}

活动范围:

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ActivityScope {
}

FragmentScope:

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface FragmentScope {
}

错误日志:

Error:(21, 1) error: mypackagename.di.component.AppComponent scoped with @Singleton may not reference bindings with different scopes:
@Provides @mypackagename.di.scope.ActivityScope mypackagename.ui.exceptionrequests.ExceptionRequestsMvpPresenter<mypackagename.ui.exceptionrequests.ExceptionRequestsMvpView> mypackagename.di.module.ActivityModule.bindExceptionRequestsPresenter(mypackagename.ui.exceptionrequests.ExceptionRequestsPresenter<mypackagename.ui.exceptionrequests.ExceptionRequestsMvpView>)
@Provides @mypackagename.di.scope.FragmentScope mypackagename.ui.addapplicant.AddApplicantMvpPresenter<mypackagename.ui.addapplicant.AddApplicantMvpView> mypackagename.di.module.FragmentModule.bindAddApplicantPresenter(mypackagename.ui.addapplicant.AddApplicantPresenter<mypackagename.ui.addapplicant.AddApplicantMvpView>)
@Provides @mypackagename.di.scope.FragmentScope mypackagename.ui.pledge.PledgeMvpPresenter<mypackagename.ui.pledge.PledgeMvpView> mypackagename.di.module.FragmentModule.bindpledgePresenter(mypackagename.ui.pledge.PledgePresenter<mypackagename.ui.pledge.PledgeMvpView>)

解决方法:

模块与组件不能有不同的范围
您可以使组件具有多个合并范围,这可以解决它.
尝试将其移动到其他组件并将其添加为组件依赖项

我希望将来他们能以我在项目中完成的方式解决此问题.
目前,Dagger2允许使用NoScope&单一范围.这应该与您的组件匹配.

经验法则:不同的作用域具有不同的组件.
对于您的应用程序,您需要三个组件,
FragmentComponent(FragmentScope):-(理想情况下,这应该是ActivityComponent)
ApplicationComponent(单例)

https://medium.com/@patrykpoborca/making-a-best-practice-app-4-dagger-2-267ec5f6c89a
阅读有关范围的更多信息.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐