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

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

概述我正在尝试为Android使用最新版本的Dagger2V2.11这是我的代码:AppComponent:@Singleton@Component(modules={AndroidInjectionModule.class,AppModule.class,ActivityBuildersModule.class,FragmentBuildersModule.class})publicinte

我正在尝试为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:

@Modulepublic 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:

@Modulepublic 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:

@FragmentScopepublic 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
阅读有关范围的更多信息.

总结

以上是内存溢出为你收集整理的Dagger 2对于Android“没有@Provides注释的方法就无法提供”全部内容,希望文章能够帮你解决Dagger 2对于Android“没有@Provides注释的方法就无法提供”所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1091667.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存