首页 > 解决方案 > Dagger2 - 应用程序不能依赖多个作用域组件

问题描述

注意这个问题可能与其他问题相似,但它提供了一个更好的解释和附件代码,旨在找到问题的解决方案,其他问题中提供的解决方案不适合。

就在几天前,我开始开发一个 Android 模块化应用程序。我使用 Dagger 2 来处理依赖注入,我目前正面临一个奇怪的行为。我有我的主应用程序模块和其他三个模块:

两者都Configuration_Module依赖Localisation_ModuleCore_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 组件依赖于多个作用域组件。有谁知道如何面对这种问题?

标签: javaandroidkotlindependency-injectiondagger-2

解决方案


经过一整天的麻烦,我找到了适合我的情况的解决方案。所有描述的模块(Configuration_ModuleLocalisation_Module)都需要在我的应用程序模块中使用。

因此,对于被@ApplicationScope @Module调用ApplicationModule,我删除了所有组件依赖项。

应用程序组件

@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()
    }
}

我指出,一切都像魅力一样运作。


推荐阅读