android - 无法使用 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]
解决方案
它应该是@ApplicationContext context: Context,
ApplicationContext
是注解
推荐阅读
- ruby-on-rails - 电报 webhook 路由未初始化
- python - 如何计算特定数据集的相关性
- r - 在 R 中使用 Matrxmodels 包时出错:CHOLMOD 分解不成功
- crystal-reports - Crystal Reports 子报表性能
- aem - AEM:如何扩展虚 URL?
- sql-server - 如何有效地支持 LIKE %Query%
- php - 与 PayPal 连接:获取状态 400(错误请求)
- office-js - 插件因检测缺失功能而被拒绝
- c# - ASP.NET Core 3.1 中 QueryString 字符串参数的自定义模型绑定器?
- javascript - 我如何在滚动时重新渲染反应?