Hilt 错误 ActivityContext 无法在没有 @Provides 注释的方法的情况下提供

问题描述 投票:0回答:1

我已将 Hilt 设置为

@Module
@InstallIn(ActivityRetainedComponent::class)
object CredentialModule {

    @Provides
    @ActivityRetainedScoped
    fun provideActivityContext(
        @ActivityContext context: Context
    ) = context

    @Provides
    @ActivityRetainedScoped
    fun provideFirebaseAuth(): FirebaseAuth = FirebaseAuth.getInstance()

    @Provides
    @ActivityRetainedScoped
    fun provideCredentialManager(
        context: Context
    ): CredentialManager = CredentialManager.create(context)

    @Provides
    @ActivityRetainedScoped
    fun provideGetGoogleIdOption(
        context: Context
    ): GetGoogleIdOption = GetGoogleIdOption.Builder()
        .setFilterByAuthorizedAccounts(false)
        .setServerClientId(context.getString(R.string.web_client_id))
        .setAutoSelectEnabled(false)
        .build()

    @Provides
    @ActivityRetainedScoped
    fun provideGetCredentialRequest(
        getGoogleIdOption: GetGoogleIdOption
    ) = GetCredentialRequest.Builder()
        .addCredentialOption(getGoogleIdOption)
        .build()

    @Provides
    @ActivityRetainedScoped
    fun provideAuthDataSource(
        context: Context,
        auth: FirebaseAuth,
        credentialManager: CredentialManager,
        getCredentialRequest: GetCredentialRequest
    ) = AuthDataSource(context, auth, credentialManager, getCredentialRequest)

} 

这总是会抛出错误

error: [Dagger/MissingBinding] @dagger.hilt.android.qualifiers.ActivityContext android.content.Context cannot be provided without an @Provides-annotated method.
  public abstract static class SingletonC implements ProfileApp_GeneratedInjector,
                         ^
  
      @dagger.hilt.android.qualifiers.ActivityContext android.content.Context is injected at
          [com.being.coder.app.profile.ProfileApp_HiltComponents.ActivityRetainedC] com.being.coder.app.profile.di.CredentialModule.provideActivityContext(context)
      android.content.Context is injected at
          [com.being.coder.app.profile.ProfileApp_HiltComponents.ActivityRetainedC] com.being.coder.app.profile.di.CredentialModule.provideAuthDataSource(context, �)
      com.being.coder.app.profile.data.datasource.AuthDataSource is injected at
          [com.being.coder.app.profile.ProfileApp_HiltComponents.ActivityRetainedC] com.being.coder.app.profile.di.CredentialModule.provideAuthRepository(authDataSource)
      com.being.coder.app.profile.presentation.signin.repository.AuthRepository is injected at
          [com.being.coder.app.profile.ProfileApp_HiltComponents.ActivityRetainedC] com.being.coder.app.profile.di.CredentialModule.provideAuthUseCases(authRepository)
      com.being.coder.app.profile.domain.usecase.auth.AuthUseCases is injected at
          [com.being.coder.app.profile.ProfileApp_HiltComponents.ViewModelC] com.being.coder.app.profile.ui.screens.auth.AuthViewModel(authUseCases)
      com.being.coder.app.profile.ui.screens.auth.AuthViewModel is injected at
          [com.being.coder.app.profile.ProfileApp_HiltComponents.ViewModelC] com.being.coder.app.profile.ui.screens.auth.AuthViewModel_HiltModules.BindsModule.binds(arg0)
      @dagger.hilt.android.internal.lifecycle.HiltViewModelMap java.util.Map<java.lang.Class<?>,javax.inject.Provider<androidx.lifecycle.ViewModel>> is requested at
          [com.being.coder.app.profile.ProfileApp_HiltComponents.ViewModelC] dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap() [com.being.coder.app.profile.ProfileApp_HiltComponents.SingletonC ? com.being.coder.app.profile.ProfileApp_HiltComponents.ActivityRetainedC ? com.being.coder.app.profile.ProfileApp_HiltComponents.ViewModelC]
  
  Note: @dagger.hilt.android.qualifiers.ActivityContext android.content.Context is provided in the following other components:
      [com.being.coder.app.profile.ProfileApp_HiltComponents.ActivityC] dagger.hilt.android.internal.modules.ActivityModule.provideContext(�)

我尝试使用

@ApplicationContext
解决这个问题,但是
CredentialManager.create(context)
需要Activity Context,在上述情况下如何获取Activity Context,以便我可以摆脱:

error: [Dagger/MissingBinding] @dagger.hilt.android.qualifiers.ActivityContext android.content.Context cannot be provided without an @Provides-annotated method.
android dependency-injection dagger-hilt
1个回答
0
投票

试试这个。

    @Provides
    fun provideActivityContext(
        @ApplicationContext context: Context
    ) = context
© www.soinside.com 2019 - 2024. All rights reserved.