美好的一天,我在dagger2中有经验,但是我试图获得一些关于Hilt的经验,因此我将通过将演示从Dagger2迁移到Hilt来开始演示应用程序。在我的Dagger应用程序中,我有两个组件ApplicationComponent和ActivityComponent。 ActivityComponent取决于ApplicationComponent,因此我将ActivityComponent编写如下:-

@ActivityScope
@Component(
    dependencies = [ApplicationComponent::class],
    modules = [ActivityModule::class]
)
interface ActivityComponent {
    fun inject(dummyActivity: DummyActivity)
    fun inject(dummyActivity2: DummyActivity2)
}
因此我尝试在Hilt中实现相同的行为。我知道Hilt用户永远不会直接定义或实例化Dagger组件。相反,Hilt提供了为我们生成的预定义组件,并且如文档所述,“子组件可以依赖于祖先组件中的任何绑定(bind)”。
android - Hilt中的组件依赖关系-LMLPHP
所以我认为我可以做以下事情,因为ActivityComponent是SingletonComponent / ApplicationComponent的子级,因此ActivityComponent可以访问ApplicationComponent中的任何绑定(bind):
@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
}
但是当我尝试构建此代码时,我得到:
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
所以我在这里想念什么?
提前致谢

最佳答案

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

10-07 20:52