gpt4 book ai didi

android - Dagger 2 对于 Android "cannot be provided without an @Provides-annotated method"

转载 作者:太空宇宙 更新时间:2023-11-03 13:10:58 25 4
gpt4 key购买 nike

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

@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();

}

Activity 模块:

@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();

}

fragment 模块:

@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);

}

ActivityScope:

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

fragment 作用域:

@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>)

最佳答案

Modules & Components 不能有不同的Scopes您可以让 Components 拥有多个 Scopes,这可以解决它。尝试将其移动到不同的组件并将其添加为组件依赖

我希望将来他们能解决这个问题,就像我在我的项目中所做的那样。目前,Dagger2 允许具有 NoScope 和单一作用域的模块。这应该与您的组件匹配。

拇指法则:不同的范围有不同的组成部分。对于您的应用程序,您需要三个组件,FragmentComponent (FragmentScope) :-(理想情况下这应该是 ActivityComponent)ApplicationComponent (单例)

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

关于android - Dagger 2 对于 Android "cannot be provided without an @Provides-annotated method",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45086951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com