我正在尝试在此处遵循 Hilt 迁移指南:
https://dagger.dev/hilt/migration-guide.html
并用以下注释注释了我的所有模块:
@InstallIn(SingletonComponent::class)
但是,我的服务、 fragment 和 Activity 的“贡献者”模块遇到了问题。我每个模块都有一个,
@Module
@InstallIn(SingletonComponent::class)
abstract class FragmentContributorModule {
@ContributesAndroidInjector
internal abstract fun contributeMyFragment(): MyFragment
}
@Module
@InstallIn(SingletonComponent::class)
abstract class ActivityContributorModule {
@ContributesAndroidInjector
internal abstract fun contributeMyActivity(): MyActivity
}
@Module
@InstallIn(SingletonComponent::class)
abstract class ServiceContributorModule {
@ContributesAndroidInjector
internal abstract fun contributeMyService(): MyService
}
在编译期间,我收到每个“贡献”函数的错误: com.test.ActivityContributorModule_ContributeMyActivity$defaultsDebug is missing an @InstallIn annotation. If this was intentional, see https://dagger.dev/hilt/compiler-options#disable-install-in-check for how to disable this check.
我还尝试对每个模块使用 ServiceComponent::class、FragmentComponent::class 和 ActivityComponent::class,但没有成功。我正在尝试分段迁移项目,因此我认为在所有内容都升级到 Hilt 之前我无法删除这些项目。有任何想法吗?
最佳答案
答案在这里:
Warning: Modules that are not annotated with @InstallIn are not used by Hilt. Hilt by default raises an error when unannotated modules are found, but this error can be disabled.
起初我并不是 100% 清楚,但我为服务/fragment/Activity 提供的贡献者模块仅与 Dagger 一起使用,而不是 Hilt。因此,如果您尝试分段迁移项目,您可以保留这些模块,直到您开始为您的服务/fragment/Activity 提供 Hilt 入口点。但是,如果您这样做,您将需要告诉 Hilt 忽略它因缺少 @InstallIn 而引发的错误。
有关如何在此处禁用它的更多信息:
https://dagger.dev/hilt/compiler-options.html#disable-install-in-check
By default, Hilt checks @Module classes for the @InstallIn annotation and raises an error if it is missing. This is because if someone accidentally forgets to put @InstallIn on a module, it could be very hard to debug that Hilt isn’t picking it up.
This check can sometimes be overly broad though, especially if in the middle of a migration. To turn off this check, this flag can be used:
-Adagger.hilt.disableModulesHaveInstallInCheck=true.
Alternatively, the check can be disabled at the individual module level by annotating the module with @DisableInstallInCheck.
关于android - 从 Dagger2 迁移到 Hilt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67663692/