我想使用suggested architecture here构建一个Android应用。但是,在按照指南进行操作并为所有类使用匕首后,我得到了循环依赖项,总结为
android.app.Application <-(改进)ServerClient <-UserRepository <-UserViewModel <-UserActivity <-某些东西<-android.app.Application
更详细的版本在这里:
android-client/app/src/main/java/de/njsm/stocks/android/dagger/RootComponent.java:22: error: [Dagger/DependencyCycle] Found a dependency cycle:
public interface RootComponent extends AndroidInjector<Application> {
^
android.app.Application is injected at
de.njsm.stocks.android.dagger.modules.WebModule.fdsa(a)
android.app.Application is injected at
de.njsm.stocks.android.dagger.modules.WebModule.provideServerClient(ctx)
de.njsm.stocks.android.network.server.ServerClient is injected at
de.njsm.stocks.android.repo.UserRepository(…, webclient, …)
de.njsm.stocks.android.repo.UserRepository is injected at
de.njsm.stocks.android.dagger.modules.ViewModelModule.provideUserViewModel(repo)
java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,javax.inject.Provider<androidx.lifecycle.ViewModel>> is injected at
de.njsm.stocks.android.dagger.modules.ViewModelModule.viewModelFactory(providerMap)
androidx.lifecycle.ViewModelProvider.Factory is injected at
de.njsm.stocks.android.frontend.user.UserActivity.setViewModelFactory(viewModelFactory)
de.njsm.stocks.android.frontend.user.UserActivity is injected at
dagger.android.AndroidInjector.inject(T) [de.njsm.stocks.android.dagger.RootComponent → de.njsm.stocks.android.dagger.modules.ActivityModule_ContributeUserActivity.UserActivitySubcomponent]
我了解问题出在哪里。我的依赖项遵循从后端到前端的方向,在后端的“叶子”处,我需要一个android上下文来访问文件系统以加载密钥库和Room DB。我想知道的是:当我只是遵循体系结构布局时,我不能成为第一个遇到这种情况的人。如何打破这个周期?我最喜欢的内容是在活动和应用程序之间,因为AFAIU应用程序可以在android中没有活动的情况下存在。
要注入我跟随dagger guide进行的活动,其余的操作都是简单(有效)的匕首配置。