为了账号安全,请及时绑定邮箱和手机立即绑定

Dagger2 - 应用程序不能依赖于多个作用域内的组件

Dagger2 - 应用程序不能依赖于多个作用域内的组件

四季花海 2022-09-21 21:41:45
注意:这个问题可能类似于其他问题,但它提供了一个更好的解释与附件代码,旨在找到问题的解决方案,其他问题中提供的解决方案是不合适的。就在几天前,我开始开发一个Android模块化应用程序。我使用Dagger 2来处理依赖注入,我目前面临着一个奇怪的行为。我有我的主应用程序模块,以及其他三个模块:Core_Module:它公开第三方库和存储层。Localisation_Module:它公开一个存储库以获取本地化信息。Configuration_Module:它公开一个存储库以获取配置参数。两者并依赖于 。Configuration_ModuleLocalisation_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])@LocalisationScopeinterface LocalisationComponent {    fun localisationService(): LocalisationService    fun localisationRepository(): LocalisationRepository}配置组件@Component(modules = [ServiceModule::class, RepositoryModule::class], dependencies = [CoreComponent::class])@ConfigurationScopeinterface ConfigurationComponent {    fun configurationService(): ConfigurationService    fun configurationRepository(): ConfigurationRepository}应用程序组件@Component(dependencies = [LocalisationComponent::class, ConfigurationComponent::class])@ApplicationScopeabstract class ApplicationComponent {    abstract fun inject(mainActivity: MainActivity)}主要应用class MainApplication : Application() {    lateinit var applicationComponent: ApplicationComponent    override fun onCreate() {        super.onCreate()        this.registerDependencies()    }我决定设计这个架构,因为我想将功能分离成独立的、可互换的模块,以便每个模块都包含执行特定功能并将单个模块导出到其他应用程序所需的一切。不幸的是,我收到一个错误,说Dagger组件不允许依赖于多个作用域的组件。有谁知道如何面对这种问题?
查看完整描述

1 回答

?
胡子哥哥

TA贡献1825条经验 获得超6个赞

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


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


应用程序组件


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

    }

}

我指出,一切都像一个魅力。


查看完整回答
反对 回复 2022-09-21
  • 1 回答
  • 0 关注
  • 63 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信