gpt4 book ai didi

android - Dagger 2 注入(inject)两个改造对象

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

我在使用 MVP 时将 Dagger 2 与 retrofit2 库一起使用。一切顺利,直到我尝试集成另一个服务(基本上我试图将另一个改造对象初始化为另一个服务)。我跟着这个answer但没有任何成功。

每次我收到一个错误,我的每个 fragment 和应用程序类似乎都无法识别组件类。

错误:找不到符号类 DaggerApplicationComponent错误:找不到符号类 DaggerEpisodeComponent

代码

应用组件

@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {

Retrofit exposeStreamingRetrofit();

Retrofit exposeRetrofit();

Context exposeContext();

AppPreferenceHelper exposePrefs();

}

应用模块

   @Module
public class ApplicationModule
{
private String mBaseUrl;
private Context mContext;
private AppPreferenceHelper mPrefsHelper;

public ApplicationModule(Context context,String baseUrl)
{
mContext = context;
mBaseUrl = baseUrl;
mPrefsHelper = new AppPreferenceHelper(context, Consts.PREF_NAME);
}


@Singleton
@Provides
GsonConverterFactory provideGsonConverterFactory()
{
GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create();
return gsonConverterFactory;
}

@Singleton
@Provides
@Named("ok-1")
OkHttpClient provideOkHttpClient()
{

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);

return new OkHttpClient().newBuilder()
.connectTimeout(500, TimeUnit.MILLISECONDS)
.readTimeout(500,TimeUnit.MILLISECONDS)
.addInterceptor(logging)
.build();
}

@Singleton
@Provides
RxJava2CallAdapterFactory provideRxJava2CallAdapterFactory()
{
return RxJava2CallAdapterFactory.create();
}

@Provides
@Singleton
Retrofit provideRetrofit(@Named("ok-1") OkHttpClient client, GsonConverterFactory convectorFactory, RxJava2CallAdapterFactory adapterFactory)
{
return new Retrofit.Builder()
.baseUrl(mBaseUrl)
.addConverterFactory(convectorFactory)
.addCallAdapterFactory(adapterFactory)
.client(client)
.build();
}

@Provides
@Singleton
Retrofit provideStreamingRetrofit(@Named("ok-1") OkHttpClient client, GsonConverterFactory convectorFactory, RxJava2CallAdapterFactory adapterFactory) {
return new Retrofit.Builder()
.baseUrl(Consts.STREAMING_BASE_PATH)
.addConverterFactory(convectorFactory)
.addCallAdapterFactory(adapterFactory)
.client(client)
.build();
}

@Singleton
@Provides
Context provideContext()
{
return mContext;
}

@Singleton
@Provides
AppPreferenceHelper provideAppPreferenceHelper(){
return mPrefsHelper;
}

}

流媒体服务

public interface StreamingService
{
@GET("search")
Observable<StreamingItems> getStreamingItems(@Query("keyword") String query);
}

流媒体模块@模块

public class StreamingModule
{
private StreamingView mView;

public StreamingModule(StreamingView view)
{
mView = view;
}

@PerFragment
@Provides
StreamingService provideStreamingService(Retrofit retrofit)
{
return retrofit.create(StreamingService.class);
}

@PerFragment
@Provides
StreamingView provideView()
{
return mView;
}

public void disposeView()
{
mView = null;
}
}

流组件

@PerFragment
@Component(modules = StreamingModule.class, dependencies = ApplicationComponent.class)
public interface StreamingComponent {
void inject(StreamingFragment streamingFragment);
}

流媒体演示者

public class StreamingPresenter extends BasePresenter<StreamingView>
{
private long mMaxPagesOfTopSeries;

@Inject
protected StreamingService mApiService;

@Inject
protected Mapper mTopSeriesMapper;

@Inject
protected AppPreferenceHelper mPrefsHelper;

@Inject
public StreamingPresenter()
{
mMaxPagesOfTopSeries = 1;

}
}

问题可能与在组件应用程序类中公开另一个 Retrofit 实例有关,但我不确定。

更新 1

剧集模块

@PerFragment
@Component (modules = EpisodeModule.class, dependencies = ApplicationComponent.class)
public interface EpisodeComponent
{
void inject(EpisodeFragment episodeFragment);
}

在我创建流类(服务、演示者、模块、组件)后,我没有更改其他类中的任何内容,所以我认为问题出在应用程序模块/组件或流类中,但我不确定,因为我可能会更改其他 fragment 的 retrofit 对象,因为我为流媒体添加了一个新的 retrofit 实例。

最佳答案

你是对的,问题是应用程序模块中第二个公开的 Retrofit 实例。解决方案是使用 dagger 限定符。只需将适当的代码块替换为:

Application Module 中使用限定符 @Named("streaming") 定义改造提供者

@Provides
@Singleton
@Named("streaming")
Retrofit provideStreamingRetrofit(@Named("ok-1") OkHttpClient client, GsonConverterFactory convectorFactory, RxJava2CallAdapterFactory adapterFactory) {
return new Retrofit.Builder()
.baseUrl(Consts.STREAMING_BASE_PATH)
.addConverterFactory(convectorFactory)
.addCallAdapterFactory(adapterFactory)
.client(client)
.build();
}

不要忘记在应用程序组件中使用完全相同的限定符公开改造实例

@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {

@Named("streaming") Retrofit exposeStreamingRetrofit();

Retrofit exposeRetrofit();

Context exposeContext();

AppPreferenceHelper exposePrefs();

}

每当您需要流媒体服务改造实例时 - 不要忘记设置限定符。 流模块

中的示例
@PerFragment
@Provides
StreamingService provideStreamingService(@Named("streaming") Retrofit retrofit) {
return retrofit.create(StreamingService.class);
}

关于android - Dagger 2 注入(inject)两个改造对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50666942/

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