use*_*079 7 java android dependency-injection dagger dagger-hilt
我正在我的测试项目中尝试 Hilt DI。我在 SingletonComponent 中安装了以下模块,就 Android 而言,它是 applicationComponent。但是,当我尝试使用 @Singleton 来确定提供方法的范围时,出现以下错误。
MyApplication_HiltComponents.ViewModelC repeats modules with scoped bindings or declarations:
public abstract static class SingletonC implements MyApplication_GeneratedInjector,
^
- com.test.catalogue.presentation.application.MyApplication_HiltComponents.SingletonC also includes:
- com.test.catalogue.di.modules.RetrofitModule with scopes: @javax.inject.Singleton
Run Code Online (Sandbox Code Playgroud)
以下是我的模块声明。
改造模块:
@Module(includes = {OKHttpModule.class})
@InstallIn(SingletonComponent.class)
public class RetrofitModule {
@Provides
@Singleton
public MyApi providesAPI(Retrofit retrofit) {
return retrofit.create(MyApi.class);
}
@Provides
@Singleton
public Retrofit providesRetrofit(OkHttpClient okHttpClient, GsonConverterFactory gsonConverterFactory, LiveDataAdapterFactory liveDataAdapterFactory) {
return new Retrofit.Builder()
.client(okHttpClient)
.addCallAdapterFactory(liveDataAdapterFactory)
.addConverterFactory(gsonConverterFactory)
.baseUrl(BuildConfig.API_URL)
.build();
}
@Provides
public GsonConverterFactory providesGsonConverterFactory() {
return GsonConverterFactory.create();
}
@Provides
public LiveDataAdapterFactory providesCallToLiveDataAdapterFactory() {
return new LiveDataAdapterFactory();
}
}
Run Code Online (Sandbox Code Playgroud)
构建.gradle:
ext.hilt_version = '2.31.2-alpha'
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"
Run Code Online (Sandbox Code Playgroud)
OkHttpModule 也有 @InstallIn(SingletonComponent.class) 但还没有 @Singleton 范围的provide(),因为我无法添加它们,因为我得到了与上面提到的相同的错误。
我怎样才能解决这个问题?我在这里做错了什么?我错过了什么吗?
归档时间: |
|
查看次数: |
1269 次 |
最近记录: |