从 Dagger2 迁移到 Hilt
Migrating to Hilt from Dagger2
我正在尝试按照此处的 Hilt 迁移指南进行操作:
https://dagger.dev/hilt/migration-guide.html
并用以下内容注释了我的所有模块:
@InstallIn(SingletonComponent::class)
然而,我 运行 对我的服务、片段和活动的“贡献者”模块存在问题。
我每个人都有一个模块,
@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% 清楚,但我为 services/fragments/activities 提供的贡献者模块仅用于 Dagger,而不用于 Hilt。因此,如果您尝试分段迁移项目,您可以保留这些模块,直到您开始为 services/fragments/activities 提供 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.
我正在尝试按照此处的 Hilt 迁移指南进行操作: https://dagger.dev/hilt/migration-guide.html
并用以下内容注释了我的所有模块:
@InstallIn(SingletonComponent::class)
然而,我 运行 对我的服务、片段和活动的“贡献者”模块存在问题。
我每个人都有一个模块,
@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% 清楚,但我为 services/fragments/activities 提供的贡献者模块仅用于 Dagger,而不用于 Hilt。因此,如果您尝试分段迁移项目,您可以保留这些模块,直到您开始为 services/fragments/activities 提供 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.