[英]Dagger2 - application can't depend on more than one scoped component
注意:該問題可能與其他問題相似,但它提供了帶有附件代碼的更好解釋,旨在找到問題的解決方案,其他問題中提供的解決方案則不合適。
就在幾天前,我開始開發一個Android模塊化應用程序。 我使用Dagger 2來處理依賴項注入,而我目前正面臨一種奇怪的行為。 我有我的主要應用程序模塊和其他三個模塊:
Core_Module
:它公開了第三方庫和存儲層。 Localisation_Module
:它公開存儲庫以獲取本地化信息。 Configuration_Module
:它公開存儲庫以獲取配置參數。 Configuration_Module
和Localisation_Module
取決於Core_Module
。
核心組件:
@Singleton
@Component(modules = [ApplicationModule::class, NetworkingModule::class, RepositoryModule::class])
interface CoreComponent {
@Named("retrofit")
fun retrofit(): Retrofit
@Named("retrofitWithCache")
fun retrofitWithCache(): Retrofit
fun storageRepository(): StorageRepository
}
本地化組件:
@Component(modules = [ServiceModule::class, RepositoryModule::class], dependencies = [CoreComponent::class])
@LocalisationScope
interface LocalisationComponent {
fun localisationService(): LocalisationService
fun localisationRepository(): LocalisationRepository
}
配置組件
@Component(modules = [ServiceModule::class, RepositoryModule::class], dependencies = [CoreComponent::class])
@ConfigurationScope
interface ConfigurationComponent {
fun configurationService(): ConfigurationService
fun configurationRepository(): ConfigurationRepository
}
應用組件
@Component(dependencies = [LocalisationComponent::class, ConfigurationComponent::class])
@ApplicationScope
abstract class ApplicationComponent {
abstract fun inject(mainActivity: MainActivity)
}
主要應用
class MainApplication : Application() {
lateinit var applicationComponent: ApplicationComponent
override fun onCreate() {
super.onCreate()
this.registerDependencies()
}
private fun registerDependencies() {
val coreModule = CoreModule(applicationContext)
applicationComponent = DaggerApplicationComponent.builder()
.localisationComponent(LocalisationModule(coreModule).localisationComponent)
.configurationComponent(ConfigurationModule(coreModule).configurationComponent)
.build()
}
}
我之所以決定設計此體系結構,是因為我想將功能分成獨立的,可互換的模塊,以便每個模塊都包含執行特定功能並將單個模塊導出到其他應用程序所需的一切。
不幸的是,我收到一個錯誤消息,說不允許Dagger組件依賴於多個作用域組件。 有誰知道如何面對這種問題?
經過一整天的麻煩,我找到了適合我的情況的解決方案。 所描述的所有模塊( Configuration_Module
, Localisation_Module
)都需要在我的Application模塊內部使用。
因此,對於名為ApplicationModule
的@ApplicationScope
@Module
我刪除了所有組件依賴項。
應用組件
@Component(modules = [ApplicationModule::class])
@ApplicationScope
abstract class ApplicationComponent {
abstract fun inject(mainActivity: MainActivity)
}
應用模塊
@Module
class ApplicationModule(private val localisationComponent: LocalisationComponent,
private val configurationComponent: ConfigurationComponent) {
@Provides
@ApplicationScope
fun providesLocalisationRepository(): LocalisationRepository {
return localisationComponent.localisationRepository() // Provided by Localisation module with Dagger
}
@Provides
@ApplicationScope
fun providesConfigurationRepository(): ConfigurationRepository {
return configurationComponent.configurationRepository() // Provided by Configuration module with Dagger
}
}
最后是我的主要應用
class MainApplication : Application() {
lateinit var applicationComponent: ApplicationComponent
override fun onCreate() {
super.onCreate()
this.registerDependencies()
}
private fun registerDependencies() {
val coreModule = CoreModule(applicationContext)
val applicationModule = ApplicationModule(LocalisationModule(coreModule).localisationComponent,
ConfigurationModule(coreModule).configurationComponent)
applicationComponent = DaggerApplicationComponent.builder()
.applicationModule(applicationModule)
.build()
}
}
我指出,一切都在發揮作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.