如何在Hilt迁移期间替换DaggerAppComponent-Android依赖注入



编辑/澄清:

我使用Hilt迁移指南,由于应用程序有许多移动部件和自定义部件,我使用AggregatorEntryPoint一次迁移一个部件,如迁移指南所示。

如果我正确阅读了指南,那么@HiltAndroidApp注释直到迁移结束时才会出现,一旦所有的dagger元素都被删除了。


我正在将我们的应用程序从dagger2迁移到希尔特。我对这一切都很陌生,经历了一段非常艰难的时光。

BaseApplication中,我试图替换当前使用DaggerAppComponent构建器构建的AppComponent。

代码库中到处都有对AppComponent的引用,所以我不知道如何删除/替换此功能。

BaseApplication.kt

open class BaseApplication : Application() {

...
lateinit var appComponent: AppComponent
private set
...
appComponent = DaggerAppComponent.builder()
.appModule(AppModule(this))
.build()
appComponent.inject(this)
}

应用组件.kt

@Singleton
@Component(modules = [AppModule::class, NetworkService::class, AnalyticsModule::class])
interface AppComponent {
fun providesNetworkService(): NetworkService
fun inject(activity: MapActivity)
fun inject(viewModel: NetworkBaseViewModel)
fun inject(application: BaseApplication)
fun inject(contactsDataSourceFactory: ContactsDataSourceFactory)
fun inject(contactDetailViewModel: ContactDetailViewModel)
fun inject(contactDetailFragment: ContactDetailFragment)
fun inject(contactDetailEditFragment: ContactDetailEditFragment)
fun inject(contactDetailCreateFragment: ContactDetailCreateFragment)
fun inject(contactsListViewModel: ContactsListViewModel)
fun inject(activityFeedViewModel: ActivityFeedViewModel)
fun inject(httpService: BaseHttpService)
fun inject(httpService: BaseWebService.ClientWrapper)
fun inject(pdfViewerFragment: PDFViewerFragment)
fun inject(commuteTimeManager: CommuteTimeManager)
fun inject(verifyLoginWorker: VerifyLoginWorker)
fun inject(loginWebFragment: LoginWebFragment)
}

这是问题的开始我更新了AppModule,用@InstallIn(SingletonComponent::class)对其进行了注释,并用@Inject constructor()替换了参数。根据错误,模块上的构造函数必须为空。

应用模块.kt

@InstallIn(SingletonComponent::class) // <- Added
@Module
// class AppModule(private val application: BaseApplication) { // <-Removed
class AppModule @Inject constructor() { // <- Added
// Old Code:
// @Provides
// @Singleton
// fun provideApplication(): BaseApplication  = application
// @Provides
// @Singleton
// internal fun provideContext(): Context = application
// New Code:
@Provides
@Singleton
fun provideApplication(@ApplicationContext application: BaseApplication): BaseApplication {
return application as BaseApplication
}
}

现在在DaggerAppComponent构建器中,我有一个错误:

Too many arguments for public constructor AppModule() defined in com.companyname.db.dependencyinjection.AppModule

appComponent = DaggerAppComponent.builder()
.appModule(AppModule(this)) // <- Here is the error
.build()

如果我不能向AppModule添加构造函数,我该如何解决这个问题?

**注意:我正在尝试逐个迁移应用程序,因为这里有很多移动部件,这就是为什么我没有完全删除AppComponent的原因。也许我在里面遗漏了什么。我一直在遵循迁移指南,并尝试添加/删除聚合器入口点。

AppComponent的使用示例NetworkBaseViewModel.kt

open class NetworkBaseViewModel (application: Application): BaseViewModel(application) {
...
init {
@Suppress("LeakingThis")
(application.applicationContext as? BaseApplication)?.appComponent?.inject(this@NetworkBaseViewModel)
...
}

使用Hilt:时不必这样做

appComponent = DaggerAppComponent.builder()
.appModule(AppModule(this))
.build()

您所要做的就是用@HiltAndroidApp对Application类进行注释。由于您的BaseApplication是open,我认为您必须在扩展它的Application类中执行它

您也不必在NetworkBaseViewModel:中执行此操作

(application.applicationContext as? BaseApplication)?.appComponent?.inject(this@NetworkBaseViewModel)

现在,您只需用@HiltViewModel注释ViewModel,当您想向ViewModel中注入一些东西时,您可以用@Inject注释构造函数,并使其依赖项可注入(通过构造函数注入或使用模块(。例如:

@HiltViewModel
class NetworkViewModel @Inject constructor(
private val retrofitService: ExampleRetrofitService
) : ViewModel() {
// ViewModel stuff
}

此外,你的AppModule甚至不需要一个带有@Inject注释的构造函数,因为你不需要向它注入任何东西。请参阅这里的示例Module,它只是一个object

最后,我建议您阅读本指南->Hilt 依赖注入

最新更新