我正在使用 Hilt 创建一个宠物项目,也许我遇到这个问题是因为我安装一切都在SingletonComponent::class
,也许我应该为每一个创建组件。
宠物项目有一个NetworkModule
, UserPrefsModule
,当我尝试创建一个时,问题出现了Authenticator
for OkHttp3
.
这是我的网络模块
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@Singleton
@Provides
fun providesHttpLoggingInterceptor() = HttpLoggingInterceptor()
.apply {
if (BuildConfig.DEBUG) level = HttpLoggingInterceptor.Level.BODY
}
@Singleton
@Provides
fun providesErrorInterceptor(): Interceptor {
return ErrorInterceptor()
}
@Singleton
@Provides
fun providesAccessTokenAuthenticator(
accessTokenRefreshDataSource: AccessTokenRefreshDataSource,
userPrefsDataSource: UserPrefsDataSource,
): Authenticator = AccessTokenAuthenticator(
accessTokenRefreshDataSource,
userPrefsDataSource,
)
@Singleton
@Provides
fun providesOkHttpClient(
httpLoggingInterceptor: HttpLoggingInterceptor,
errorInterceptor: ErrorInterceptor,
authenticator: Authenticator,
): OkHttpClient =
OkHttpClient
.Builder()
.authenticator(authenticator)
.addInterceptor(httpLoggingInterceptor)
.addInterceptor(errorInterceptor)
.build()
}
Then my UserPrefsModule
is :
@Module
@InstallIn(SingletonComponent::class)
object UserPrefsModule {
@Singleton
@Provides
fun provideSharedPreference(@ApplicationContext context: Context): SharedPreferences {
return context.getSharedPreferences("user_prefs", Context.MODE_PRIVATE)
}
@Singleton
@Provides
fun provideUserPrefsDataSource(sharedPreferences: SharedPreferences): UserPrefsDataSource {
return UserPrefsDataSourceImpl(sharedPreferences)
}
}
然后我有一个AuthenticatorModule
@Module
@InstallIn(SingletonComponent::class)
object AuthenticationModule {
private const val BASE_URL = "http://10.0.2.2:8080/"
@Singleton
@Provides
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create())
.baseUrl(BASE_URL)
.client(okHttpClient)
.build()
@Singleton
@Provides
fun provideApiService(retrofit: Retrofit): AuthenticationService =
retrofit.create(AuthenticationService::class.java)
@Singleton
@Provides
fun providesAccessTokenRefreshDataSource(
userPrefsDataSource: UserPrefsDataSource,
authenticationService: AuthenticationService,
): AccessTokenRefreshDataSource = AccessTokenRefreshDataSourceImpl(
authenticationService, userPrefsDataSource
)
}
当我创建时问题就开始发生AccessTokenRefreshDataSourceImpl
我需要AuthenticationService
and UserPrefsDataSource
,我收到此错误:
错误:[Dagger/DependencyCycle] 发现依赖循环:
公共抽象静态类 SingletonC 实现 App_GenerateInjector,
对于每个功能,例如登录、登录、验证等。我正在创建一个新的@Module
像这样:
@Module
@InstallIn(SingletonComponent::class)
interface SignInModule {
@Binds
fun bindIsValidPasswordUseCase(
isValidPasswordUseCaseImpl: IsValidPasswordUseCaseImpl,
): IsValidPasswordUseCase
@Binds
fun bindIsValidEmailUseCase(
isValidEmailUseCase: IsValidEmailUseCaseImpl,
): IsValidEmailUseCase
//Here in that Datasource I'm using the AuthenticationService from AuthenticationModule and it works
@Binds
fun bindSignInDataSource(
signInDataSourceImpl: SignInDataSourceImpl
): SignInDataSource
}
的构造函数AccessTokenAutenticator
class AccessTokenAuthenticator @Inject constructor(
private val accessTokenRefreshDataSource: AccessTokenRefreshDataSource,
private val userPrefsDataSource: UserPrefsDataSource,
) : Authenticator {
的构造函数AccessTokenRefreshDatasource
class AccessTokenRefreshDataSourceImpl @Inject constructor(
private val authenticationService: AuthenticationService,
private val userPrefsDataSource: UserPrefsDataSource,
) : AccessTokenRefreshDataSource {
注意我的所有内容都在@Module
按功能分开,以便将来能够模块化应用程序。