[英]Dagger 2 cannot be provided without an @Provides-annotated method
I know there are many similar questions but I still haven't been able to find a solution for my problem and at this stage, I'm out of ideas. 我知道有很多类似的问题,但我仍然无法找到解决问题的方法,而且在这个阶段,我没有想法。 I have the following setup:
我有以下设置:
This is how the code looks: 这是代码的样子:
ApplicationModule.java ApplicationModule.java
@Module
public class ApplicationModule {
private Application mApp;
public ApplicationModule(Application app) {
mApp = app;
}
@Provides
@Singleton
public Application provideApplication() {
return mApp;
}
@Provides
@Singleton
Context provideApplicationContext() {
return mApp.getApplicationContext();
}
}
ApplicationComponent.java ApplicationComponent.java
@Singleton
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
Application application();
Context getContext();
}
NetModule.java NetModule.java
@Module
public class NetModule {
String mBaseUrl;
public NetModule(String baseUrl) {
this.mBaseUrl = baseUrl;
}
@Provides
@Singleton
SharedPreferences providesSharedPreferences(Application application) {
return PreferenceManager.getDefaultSharedPreferences(application);
}
@Provides
@Singleton
Cache provideOkHttpCache(Application application) {
int cacheSize = 10 * 1024 * 1024; // 10 MiB
return new Cache(application.getCacheDir(), cacheSize);
}
@Provides
@Singleton
OkHttpClient provideOkHttpClient(Cache cache) {
OkHttpClient client = new OkHttpClient();
client.setCache(cache);
return client;
}
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.addConverterFactory(JacksonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(mBaseUrl)
.client(okHttpClient)
.build();
}
@Provides
@Singleton
PlaceRequests providePlaceRequests(Retrofit retrofit) {
return retrofit.create(PlaceRequests.class);
}
}
NetComponent.java NetComponent.java
@Singleton
@Component(
dependencies = {ApplicationModule.class},
modules = {NetModule.class}
)
public interface NetComponent {
Application application();
PlaceRequests getPlaceRequests();
}
CityModule.java CityModule.java
@Module
public class CityModule {
private CityMvp.View view;
public CityModule(CityMvp.View view) {
this.view = view;
}
@Provides
public CityMvp.View provideView() {
return view;
}
@Provides
public CityMvp.Interactor provideInteractor(PlaceRequests placeRequests) {
return new CityInteractor(placeRequests);
}
@Provides
public CityPresenter providePresenter(CityMvp.View cityView, CityMvp.Interactor interactor) {
return new CityPresenter(cityView, interactor);
}
}
CityComponent.java CityComponent.java
@PerFragment
@Component(
dependencies = {NetModule.class},
modules = {CityModule.class}
)
public interface CityComponent {
void inject(CityFragment cityFragment);
}
CityInteractor.java (the one that is not able to inject cause the PlaceRequests dependency) CityInteractor.java(无法注入的那个导致PlaceRequests依赖项)
public class CityInteractor implements CityMvp.Interactor {
private PlaceRequests placeRequests;
public CityInteractor(PlaceRequests placeRequests) {
this.placeRequests = placeRequests;
}
@Override
public void getPlaceDetails(String placeId, String key, Subscriber<PlaceDetails> subscriber) {
Observable<PlaceDetails> observable = placeRequests.getPlaceDetails(placeId, key);
observable.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(subscriber);
}
}
Finally, the error trace: 最后,错误跟踪:
: error: com.blabla.blabla.webapi.place.PlaceRequests cannot be provided without an @Provides-annotated method.
void inject(CityFragment cityFragment);
^
com.blabla.blabla.screens.city.CityFragment.presenter
[injected field of type: com.blabla.blabla.screens.city.CityPresenter presenter]
com.blabla.blabla.di.modules.CityModule.providePresenter(com.blabla.blabla.screens.city.CityMvp.View cityView, com.blabla.blabla.screens.city.CityMvp.Interactor interactor)
[parameter: com.blabla.blabla.screens.city.CityMvp.Interactor interactor]
com.blabla.blabla.di.modules.CityModule.provideInteractor(com.blabla.blabla.webapi.place.PlaceRequests placeRequests)
[parameter: com.blabla.blabla.webapi.place.PlaceRequests placeRequests]
3 errors
:app:compileDebugJavaWithJavac FAILED
FAILURE: Build failed with an exception.
From what I understand, I'm exposing PlaceRequests object in NetComponent.java, and NetModule.java is a dependency of CityComponent. 根据我的理解,我在NetComponent.java中公开PlaceRequests对象,而NetModule.java是CityComponent的依赖项。 How come I'm not able to get PlaceRequests dependency from CityComponent?
为什么我无法从CityComponent获得PlaceRequests依赖? What am I missing?
我错过了什么? Thanks!
谢谢!
Component dependencies specified through @Component(dependencies={...})
shouldn't be Modules, as you have them. 通过
@Component(dependencies={...})
指定的组件依赖 @Component(dependencies={...})
不应该是模块,因为您拥有它们。 They should be types (typically Components) that make dependencies available through zero-arg methods known as provision methods . 它们应该是类型(通常是组件),通过称为提供方法的零参数方法使依赖项可用。
Switch your dependencies to the components instead of modules. 将依赖项切换到组件而不是模块。
If it helps, you might want to change how you think about Components, Modules, and component dependencies. 如果它有帮助,您可能想要改变您对组件,模块和组件依赖关系的看法。 You can think of Dagger as creating an object graph where Modules define the inputs or bindings of the graph and Components define the outputs or consumers of the graph .
您可以将Dagger视为创建对象图,其中模块定义图的输入或绑定,而组件定义 图 的输出或使用者 。 This leaves component dependencies to be types or components included or imported from another external source, which might include a different Dagger-created Component, but probably wouldn't include a Module--instead of a dependency on the Module you'd depend on the Component that consumes the module.
这使组件依赖性成为包含或从另一个外部源导入的类型或组件,其可能包括不同的Dagger创建的组件,但可能不包含模块 - 而不是依赖于您依赖于模块的模块消耗模块的组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.