我正在尝试将我的项目迁移到Dagger Hilt
并面临缺少绑定的问题。我正在关注谷歌的代码实验室来实现这一点。
这是构建失败的地方:
error: [Dagger/MissingBinding] java.util.Map<java.lang.String,javax.inject.Provider<dagger.android.AndroidInjector.Factory<?>>> cannot be provided without an @Provides-annotated method.
public abstract static class ApplicationC implements WhatToCookApp_GeneratedInjector,
^
java.util.Map<java.lang.String,javax.inject.Provider<dagger.android.AndroidInjector.Factory<?>>> is injected at
dagger.android.DispatchingAndroidInjector(�, injectorFactoriesWithStringKeys)
dagger.android.DispatchingAndroidInjector<java.lang.Object> is injected at
dagger.android.support.DaggerAppCompatActivity.androidInjector
at.bwappsandmore.whattocook.MainActivity is injected at
dagger.android.AndroidInjector.inject(T) [at.bwappsandmore.whattocook.WhatToCookApp_HiltComponents.ApplicationC ? at.bwappsandmore.whattocook.di.ActivityModule_InjectMainActivity.MainActivitySubcomponent]
It is also requested at:
dagger.android.DispatchingAndroidInjector(�, injectorFactoriesWithStringKeys)
The following other entry points also depend on it:
这些是项目的相关部分:
@HiltAndroidApp
open class WhatToCookApp : Application() {
val appComponent: AppComponent by lazy {
initializeComponent()
}
open fun initializeComponent(): AppComponent {
return DaggerAppComponent.factory().create(applicationContext)
}
}
应用组件:
@Singleton
@Component(
modules = [AppModule::class,
ActivityModule::class,
AndroidSupportInjectionModule::class]
)
interface AppComponent : AndroidInjector<WhatToCookApp> {
@Component.Factory
interface Factory {
fun create(@BindsInstance appContext: Context): AppComponent
}
}
应用模块:
@InstallIn(ApplicationComponent::class)
@Module
class AppModule {
@Provides
fun provideDB(@ApplicationContext context: Context): AppDatabase {
return AppDatabase.getDatabase(context)
}
@Provides
fun provideDAO(app: AppDatabase): WhatToCookDao {
return app.whatToCookDao()
}
@Provides
fun provideAppRepository(dao: WhatToCookDao): AppRepository{
return AppRepository(dao)
}
@Provides
fun provideSharedPreferences(@ApplicationContext context: Context): SharedPreferences {
return PreferenceManager.getDefaultSharedPreferences(context)
}
}
应用模块:
@InstallIn(ApplicationComponent::class)
@Module
interface ActivityModule {
@ActivityScope
@ContributesAndroidInjector(modules = [ViewModelModule::class])
fun injectMainActivity(): MainActivity
}
视图模型模块:
@InstallIn(ApplicationComponent::class)
@Module
abstract class ViewModelModule {
companion object{
@Provides
fun providesSharedViewModel (activity: MainActivity) : SharedViewModel = activity.viewModel
}
}
活动范围:
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityScope
我意识到我必须使用@Provides
注释AndroidInjector
,但我不知道在哪里以及如何。任何帮助表示赞赏。
非常感谢你。