相关疑难解决方法(0)

Dagger 2注入两个改造对象

我在使用MVP时将Dagger 2与retrofit2库一起使用。一切顺利,直到我尝试集成另一个服务(基本上我试图将另一个改造对象初始化到另一个服务)。我遵循了这个答案,但是没有成功。

每次遇到错误时,我的每个片段和应用程序类似乎都无法识别组件类。

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

应用组件

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

    Retrofit exposeStreamingRetrofit();

    Retrofit exposeRetrofit();

    Context exposeContext();

    AppPreferenceHelper exposePrefs();

}
Run Code Online (Sandbox Code Playgroud)

应用模块

   @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(); …
Run Code Online (Sandbox Code Playgroud)

android dagger-2 retrofit2

3
推荐指数
1
解决办法
1452
查看次数

提供并注入两个具有不同实现的实例 - Dagger 2

我有一种情况,我需要两个改造服务,每个服务都有其业务实现。

    @Provides
    @Singleton
    @Named("defaultMulhimService")
    MulhimService provideMulhimService() {
        return MulhimService.Creator.newMulhimService();
    }

    @Provides
    @Singleton
    @Named("MulhimServiceWithCache")
    MulhimService providesMulhimServiceWithCache(){
        return MulhimService.Creator.newMulhimServiceWithCache(mApplication);
    }
Run Code Online (Sandbox Code Playgroud)

我已经看过这个答案,它建议使用 @Named 注释来区分模块中的多个实例,但我不知道如何注入它们。

android dependency-injection dagger-2

1
推荐指数
1
解决办法
1765
查看次数