简体   繁体   English

Hilt 中的组件依赖项

[英]component dependencies in Hilt

Good day, I have experience in dagger2 but I'm trying to gain some experience in Hilt so I start with the demo application by migrating the demo from Dagger2 to Hilt.美好的一天,我有使用 dagger2 的经验,但我想在 Hilt 中获得一些经验,所以我从演示应用程序开始,将演示从 Dagger2 迁移到 Hilt。 in my Dagger app, I have two components ApplicationComponent and ActivityComponent.在我的 Dagger 应用程序中,我有两个组件 ApplicationComponent 和 ActivityComponent。 ActivityComponent depends on the ApplicationComponent so I wrote ActivityComponent as following:- ActivityComponent 依赖于 ApplicationComponent 所以我写了 ActivityComponent 如下:-

@ActivityScope
@Component(
    dependencies = [ApplicationComponent::class],
    modules = [ActivityModule::class]
)
interface ActivityComponent {
    fun inject(dummyActivity: DummyActivity)
    fun inject(dummyActivity2: DummyActivity2)
}

so I tried to achieve the same behavior in Hilt.所以我试图在 Hilt 中实现相同的行为。 I know Hilt users never define or instantiate Dagger components directly.我知道 Hilt 用户从不直接定义或实例化 Dagger 组件。 Instead, Hilt offers predefined components that are generated for us, and as the documentation explains "child component can have dependencies on any binding in an ancestor component"相反,Hilt 提供了为我们生成的预定义组件,并且正如文档所解释的“子组件可以依赖于祖先组件中的任何绑定” 在此处输入图片说明

so i thought i can do the following as ActivityComponent is a child of the SingletonComponent/ ApplicationComponent so ActivityComponent can access any bindings in the ApplicationComponent:-所以我认为我可以执行以下操作,因为 ActivityComponent 是 SingletonComponent/ApplicationComponent 的子项,因此 ActivityComponent 可以访问 ApplicationComponent 中的任何绑定:-

@Module
@InstallIn(ApplicationComponent::class)
class  ApplicationModule {


    @Provides
    @Singleton
    fun provideDispatcherProvider(): DispatcherProvider = FlowDispatcherProvider()

    @Provides
    @Singleton
    fun provideSharedPreferences(@ApplicationContext application: Application): SharedPreferences =
        application.getSharedPreferences("project-prefs", Context.MODE_PRIVATE)

   
    @Provides
    @Singleton
    fun provideNetworkHelper(@ApplicationContext application: Application): NetworkHelper = NetworkHelper(application)

@Provides
@Singleton
fun provideNetworkService(@ApplicationContext application: Application): NetworkService =
        Networking.create(
            BuildConfig.USERNAME,
            BuildConfig.PASSWORD,
            BuildConfig.BASE_URL,
            application.cacheDir,
            10 * 1024 * 1024 // 10MB
        )

}


@Module
@InstallIn(ActivityComponent::class)
class ActivityModule {


    @Provides
    @ActivityScoped
    fun provideMyHelper(
        dispatcherProvider: DispatcherProvider,
        networkHelper: NetworkHelper
       ):MyHelper =  MyHelper(dispatcherProvider, networkHelper)


}

class MyHelper
    (
    private val dispatcherProvider: DispatcherProvider,
    private val networkHelper: NetworkHelper
) {

}

@ActivityScoped
@AndroidEntryPoint
class DummyActivity(): AppCompatActivity() {

    @Inject
    lateinit var myhelper: MyHelper
}

but when I try to build this code I get:-但是当我尝试构建此代码时,我得到:-

error: [Dagger/MissingBinding] @com.mypackage.ApplicationContext android.app.application cannot be provided without an @Provides-annotated method.
  public abstract static class SingletonC implements MyApplication_GeneratedInjector,
                         ^
      @com.mypackage.ApplicationContext android.app.Application is injected at
          mypackage.module.ApplicationModule.provideNetworkHelper(application)
      com.mypackage.NetworkHelper is injected at
          com.mypackage.ActivityModule.provideMyHelper(�, networkHelper)
      com.mypackage.MyHelper is injected at
          com.mypackage.DummyActivity.myhelper
      com.mypackage.DummyActivity is injected at
      com.mypackage.DummyActivity.myhelper

so what am I missing here?那么我在这里错过了什么? Thanks in advance提前致谢

显然您没有正确导入 ApplicationContext 限定符,它应该是 dagger.hilt.android.qualifiers.ApplicationContext。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM