首页 > 解决方案 > 无法使用 hilt 依赖注入注入应用程序上下文

问题描述

这是模块类

@Module
@InstallIn(SingletonComponent::class)
object AppModule {


    @Provides
    fun getAppPreferences(
        @ApplicationContext context: ApplicationContext,
        @Named("appName") appName: String
    ):SharedPreferences {
        return (context as Context).getSharedPreferences(appName, Context.MODE_PRIVATE)
    }


    @Provides
    @Named("URL")
    fun getBaseUrl(): String {
        return AppConstants.BASE_URL
    }

    @Singleton
    @Provides
    fun getApiService(
        client: OkHttpClient, @Named("URL") baseURL: String,
        factory: GsonConverterFactory
    ): ApiInterface {

        return Retrofit.Builder()
            .baseUrl(baseURL)
            .addConverterFactory(factory)
            .client(client)
            .build()
            .create(ApiInterface::class.java)

    }

    @Singleton
    @Provides
    fun getClient(sharedPreferences: SharedPreferences): OkHttpClient {

        val interceptor = HttpLoggingInterceptor()
        interceptor.level = HttpLoggingInterceptor.Level.BODY
        return OkHttpClient().newBuilder()
            .addInterceptor(interceptor)
            .addInterceptor { chain ->
                val requestBuilder = chain.request().newBuilder()
                val token = ProjectUtil.getApiTokenBearer(sharedPreferences)
                token?.apply {
                    requestBuilder.addHeader("Authorization", token)
                }
                chain.proceed(requestBuilder.build())
            }
            .connectTimeout(100, TimeUnit.SECONDS)
            .writeTimeout(100, TimeUnit.SECONDS)
            .readTimeout(100, TimeUnit.SECONDS)
            .build()

    }

    @Singleton
    @Provides
    fun getConvertorFactory(): GsonConverterFactory {

        val gson = GsonBuilder()
            .setLenient()
            .create()
        return GsonConverterFactory.create(gson)

    }

    @Provides
    @Named("appName")
    fun getAppName() = "CarApplication"

}

这是我的视图模型构造函数

@HiltViewModel
class HomeFragmentViewModel @Inject constructor(
    val sharedPreferences: SharedPreferences,
    private val apiService: ApiInterface
) : ViewModel() {

    private lateinit var createEquriumDialog: Dialog

    //    val progress_dialog = MyOwnProgressDialog(context)
    var showAquariumRv: MutableLiveData<Boolean> = MutableLiveData()
    var equariumList: ListUserAquariumResponse? = null
    val goToActivityIntent: MutableLiveData<Intent> = MutableLiveData()
    val progress: MutableLiveData<Boolean> = MutableLiveData()
    val showMessage: MutableLiveData<String> = MutableLiveData()
}

当我运行我认为不注入模块中所需的应用程序上下文的应用程序时出现此错误,任何帮助都将不胜感激

C:\XYZ\XYZ\AndroidStudioProjects\CarAndroid\app\build\generated\source\kapt\debug\com\sw\car\baseclasses\CarApp_HiltComponents.java:128:错误:[Dagger/MissingBinding] @dagger.hilt.android .qualifiers.ApplicationContext dagger.hilt.android.qualifiers.ApplicationContext 不能在没有@Provides-annotated 方法的情况下提供。公共抽象静态类 SingletonC 实现 DaluaApp_GeneratedInjector,^@dagger.hilt.android.qualifiers.ApplicationContext dagger.hilt.android.qualifiers.ApplicationContext 在 com.sw.car.di.modules.AppModule.getAppPreferences(context, �) android 注入.content.SharedPreferences 在 com.sw.car.ui.home.fragments.home.HomeFragmentViewModel(sharedPreferences, �) 处注入 com.sw.car.ui.home.fragments.home.HomeFragmentViewModel 在 com.sw.car 处注入.ui.home.fragments.home.HomeFragmentViewModel_HiltModules。BindsModule.binds(vm) @dagger.hilt.android.internal.lifecycle.HiltViewModelMap java.util.Map<java.lang.String,javax.inject.Provider<androidx.lifecycle.ViewModel>> 在 dagger.hilt 请求。 android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap() [com.devstudio.dalua.baseclasses.CarApp_HiltComponents.SingletonC ? com.devstudio.dalua.baseclasses.CarApp_HiltComponents.ActivityRetainedC ?com.devstudio.dalua.baseclasses.CarApp_HiltComponents.ViewModelC] 单例C ? com.devstudio.dalua.baseclasses.CarApp_HiltComponents.ActivityRetainedC ?com.devstudio.dalua.baseclasses.CarApp_HiltComponents.ViewModelC] 单例C ? com.devstudio.dalua.baseclasses.CarApp_HiltComponents.ActivityRetainedC ?com.devstudio.dalua.baseclasses.CarApp_HiltComponents.ViewModelC]

标签: androidkotlindependency-injectiondagger-hilt

解决方案


它应该是@ApplicationContext context: Context,

ApplicationContext是注解


推荐阅读