android - fragment 模块上的 Dagger 柄缺少绑定(bind)

标签 android kotlin dagger-2 dagger-hilt

我正在尝试将我的 API 服务提供到 FragmentModule 中,但它无法按照我设置依赖项注入(inject)的方式工作,即使我已将相应的模块包含到 FragmentModule 中,我仍收到 MissingBiding 错误。

这是我的设置:

应用程序模块

@Module
@InstallIn(value = [SingletonComponent::class])
object AppModule {

    @Provides
    fun provideApplication(application: MyApplication): Context = application

}

网络模块

@Module(includes = [AppModule::class])
@InstallIn(value = [SingletonComponent::class])
object NetworkModule {

    private const val BASE_URL = "https://url.co/"

    @Provides
    fun provideAPI(retrofit: Retrofit): APIService = retrofit.create(APIService::class.java)

    @Provides
    fun provideCache(cacheFile: File): Cache = Cache(cacheFile, 10 * 1024 * 1024)

    @Provides
    fun provideFile(@ApplicationContext context: Context): File = File(context.cacheDir, "okhttp_cache")


    @Provides
    fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit =
        Retrofit.Builder().client(okHttpClient)
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()

    @Provides
    fun provideOkHttpClient(interceptor: HttpLoggingInterceptor, cache: Cache, context: Context): OkHttpClient {
        return OkHttpClient.Builder()
            .cache(cache)
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .writeTimeout(30, TimeUnit.SECONDS)
            .build()
    }
}

MyFragmentModule

@Module(includes = [AppModule::class, NetworkModule::class])
@InstallIn(SingletonComponent::class)
object MyFragmentModule {

    @Provides
    fun provideMyRepositoryImpl(@IoDispatcher
                                     dispatcher: CoroutineDispatcher,
                                     functions: Functions,
                                     apiService: APIService): MyRepository = MyRepositoryImpl(dispatcher, functions, apiService)

    @Provides
    fun provideMyUseCase(myRepository: MyRepository): MyUseCase = MyUseCase(myRepository)
}

[Dagger/MissingBinding]

C:\MyApplication_HiltComponents.java:168: error: [Dagger/MissingBinding] MyApplication cannot be provided without an @Inject constructor or an @Provides-annotated method. This type supports members injection but cannot be implicitly provided.
  public abstract static class SingletonC implements MyApplication_GeneratedInjector,
                         ^
  A binding with matching key exists in component: MyApplication_HiltComponents.SingletonC
      MyApplication is injected at
          di.AppModule.provideApplication(application)
      android.content.Context is injected at
          di.NetworkModule.provideOkHttpClient(�, context)
      okhttp3.OkHttpClient is injected at
          di.NetworkModule.provideRetrofit(okHttpClient)
      retrofit2.Retrofit is injected at
          di.NetworkModule.provideAPI(retrofit)
      data.service.APIService is injected at
          ui.main_activity.ranking.di.MyFragmentModule.provideMyRepositoryImpl(�, apiService)
      ui.main_activity.ranking.data.MyRepository.MyRepository is injected at
          ui.main_activity.ranking.di.MyFragmentModule.provideMyUseCase(rankingMyRepository)
      javax.inject.Provider<ui.main_activity.ranking.domain.MyUseCase> is injected at
          ui.main_activity.ranking.data.view_model.RankingViewModel_AssistedFactory(MyUseCase)
      ui.main_activity.ranking.data.view_model.RankingViewModel_AssistedFactory is injected at
          ui.main_activity.ranking.data.view_model.RankingViewModel_HiltModule.bind(factory)
      java.util.Map<java.lang.String,javax.inject.Provider<androidx.hilt.lifecycle.ViewModelAssistedFactory<? extends androidx.lifecycle.ViewModel>>> is injected at
          androidx.hilt.lifecycle.ViewModelFactoryModules.ActivityModule.provideFactory(�, viewModelFactories)
      @dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory java.util.Set<androidx.lifecycle.ViewModelProvider.Factory> is injected at
          dagger.hilt.android.internal.lifecycle.DefaultViewModelFactories.InternalFactoryFactory(�, defaultActivityFactorySet, �)
      dagger.hilt.android.internal.lifecycle.DefaultViewModelFactories.InternalFactoryFactory is requested at
          dagger.hilt.android.internal.lifecycle.DefaultViewModelFactories.ActivityEntryPoint.getHiltInternalFactoryFactory() [MyApplication_HiltComponents.SingletonC ? MyApplication_HiltComponents.ActivityRetainedC ? MyApplication_HiltComponents.ActivityC]
  The following other entry points also depend on it:
      dagger.hilt.android.internal.lifecycle.DefaultViewModelFactories.FragmentEntryPoint.getHiltInternalFactoryFactory() [MyApplication_HiltComponents.SingletonC ? MyApplication_HiltComponents.ActivityRetainedC ? MyApplication_HiltComponents.ActivityC ? MyApplication_HiltComponents.FragmentC]

如果您有任何反馈,我会非常感激,谢谢

最佳答案

对于任何面临类似问题的人,请注意如何定义模块,在我的例子中,我有一个带有上下文的 @Provides 而不是 MyApp,我还在网络模块中包含了 @ApplicationContext,这解决了问题

 @Provides
fun provideApplication(application: MyApplication): MyApplication = application

添加了上下文注释:

 @Provides
fun provideFile(@ApplicationContext context: Context): File = File(context.cacheDir, "okhttp_cache")

@Provides
fun provideOkHttpClient(interceptor: HttpLoggingInterceptor, cache: Cache, @ApplicationContext context: Context): OkHttpClient {
    return OkHttpClient.Builder()
        .cache(cache)
        .connectTimeout(30, TimeUnit.SECONDS)
        .readTimeout(30, TimeUnit.SECONDS)
        .writeTimeout(30, TimeUnit.SECONDS)
        .build()
}

关于android - fragment 模块上的 Dagger 柄缺少绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66946190/

相关文章:

android - 同时安装稳定版和开发版应用程序的实用方法是什么?

android - 如何在不定义类的情况下使用 moshi 创建复杂的 json?

kotlin - 使用类中的几个函数实现扩展函数 Kotlin

android - 使用 Dagger 2 的 Activity 场注入(inject)

android - 如何使用 AndroidInjector 在模块中获取 MainActivity

php - 组织.json.JSONException : No value for item

java - 陷入泛型困境

android - 使用 FusedLocationProvider Api 时定位服务的自定义警报消息

android - 房间+ Dagger 2. NPE

android - 如何计算通知数量并在Android中显示单个图标?