第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Dagger2 - 應(yīng)用程序不能依賴于多個作用域內(nèi)的組件

Dagger2 - 應(yīng)用程序不能依賴于多個作用域內(nèi)的組件

四季花海 2022-09-21 21:41:45
注意:這個問題可能類似于其他問題,但它提供了一個更好的解釋與附件代碼,旨在找到問題的解決方案,其他問題中提供的解決方案是不合適的。就在幾天前,我開始開發(fā)一個Android模塊化應(yīng)用程序。我使用Dagger 2來處理依賴注入,我目前面臨著一個奇怪的行為。我有我的主應(yīng)用程序模塊,以及其他三個模塊:Core_Module:它公開第三方庫和存儲層。Localisation_Module:它公開一個存儲庫以獲取本地化信息。Configuration_Module:它公開一個存儲庫以獲取配置參數(shù)。兩者并依賴于 。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}應(yīng)用程序組件@Component(dependencies = [LocalisationComponent::class, ConfigurationComponent::class])@ApplicationScopeabstract class ApplicationComponent {    abstract fun inject(mainActivity: MainActivity)}主要應(yīng)用class MainApplication : Application() {    lateinit var applicationComponent: ApplicationComponent    override fun onCreate() {        super.onCreate()        this.registerDependencies()    }我決定設(shè)計這個架構(gòu),因為我想將功能分離成獨立的、可互換的模塊,以便每個模塊都包含執(zhí)行特定功能并將單個模塊導(dǎo)出到其他應(yīng)用程序所需的一切。不幸的是,我收到一個錯誤,說Dagger組件不允許依賴于多個作用域的組件。有誰知道如何面對這種問題?
查看完整描述

1 回答

?
胡子哥哥

TA貢獻1825條經(jīng)驗 獲得超6個贊

經(jīng)過一整天的麻煩,我找到了適合我的情況的解決方案。所描述的所有模塊 (, ) 都需要在我的應(yīng)用程序模塊中使用。Configuration_ModuleLocalisation_Module


因此,對于調(diào)用,我刪除了所有組件依賴項。@ApplicationScope@ModuleApplicationModule


應(yīng)用程序組件


@Component(modules = [ApplicationModule::class])

@ApplicationScope

abstract class ApplicationComponent {

    abstract fun inject(mainActivity: MainActivity)

}

應(yīng)用模塊


@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

    }

}

最后是我的主要應(yīng)用程序


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

    }

}

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


查看完整回答
反對 回復(fù) 2022-09-21
  • 1 回答
  • 0 關(guān)注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號