gpt4 book ai didi

android - Hilt 中的组件依赖项

转载 作者:行者123 更新时间:2023-12-02 12:25:43 26 4
gpt4 key购买 nike

美好的一天,我有使用 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)” enter image description here

所以我认为我可以执行以下操作,因为 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。

关于android - Hilt 中的组件依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63880473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com