I want to build an android app using the suggested architecture here. However after following the guide and using dagger for all my classes I ended up with a cyclic dependency summarised as
android.app.Application <- (Retrofit) ServerClient <- UserRepository <- UserViewModel <- UserActivity <- somethingsomething <- android.app.Application
The more detailed version is here:
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]
I understand why the problem is there. My dependencies follow the direction backend to frontend and at the "leaf" in the backend I need an android context to get access to the filesystem to load the keystore and Room DB. What I'm wondering is: As I simply follow the architecture layout I cannot be the first one to run into this. How can I break this cycle? My favourite cut would betweeen the activity and the application because AFAIU an application can exist without an activity in android.
To inject the activities I followed the dagger guide and the rest is simple (and working) dagger config.