Hello! I’m having problems with dependencies gener...
# koin
g
Hello! I’m having problems with dependencies generation for a KMM project where Android needs do provide something to Shared but I believe I have a “circular problem”. My setup in thread.
👀 2
androidApp module:
Copy code
class MyApp : Application() {

    override fun onCreate() {
        super.onCreate()

        buildConfigApiUrl = BuildConfig.API_URL

        DependencyInjection.initKoin(BuildConfig.DEBUG) {
            androidLogger()
            androidContext(this@MyApp)
            modules(appModule, networkModule, viewModelsModule)
        
            //this needs to happen after the modules initialization, but the modules need this information also ^^'
            chuckerInterceptor = get(named("chuckerInterceptor"))
            sharedPrefs = get(named("encryptedPrefs"))            
        }
    }
}
Copy code
single(named("encryptedPrefs")) {
    EncryptedSharedPreferences.create(...)
}

single(named("chuckerInterceptor")) {
    ChuckerInterceptor....
}
androidMain (shared module)
Copy code
lateinit var buildConfigApiUrl: String
lateinit var sharedPrefs: SharedPreferences
lateinit var chuckerInterceptor: Interceptor

actual class PlatformDependencies actual constructor() {
    actual val baseUrl: String = buildConfigApiUrl

    actual fun getSettings(): Settings = SharedPreferencesSettings(sharedPrefs)

    actual fun getHttpEngine(): HttpClientEngine = OkHttp.create { OkHttpConfig().also { addInterceptor(chuckerInterceptor) } }
}
commonMain (shared module)
Copy code
fun initKoin(enableNetworkLogs: Boolean = false, appDeclaration: KoinAppDeclaration = {}) {
    startKoin {
        appDeclaration()
        modules(commonModule(enableNetworkLogs), platformModule())
    }
}

private fun commonModule(enableNetworkLogs: Boolean) = module {
    single { PlatformDependencies().getSettings() }
    single { LocalStorage(get()) }
    ....
}
As you can see LocalStorage depends on Settings, but Settings depends on the sharedPrefs that’s provided by androidApp. But androidApp cannot run before because koin is not yet initialized 😅 Do you see the rabbit hole?
Is there any way I can solve this instead of doing the following:
Copy code
override fun onCreate() {
    super.onCreate()

    buildConfigApiUrl = BuildConfig.API_URL
    sharedPrefs = EncryptedSharedPreferences.create(...)
    chuckerInterceptor = ChuckerInterceptor
        .Builder(...)

    DependencyInjection.initKoin(BuildConfig.DEBUG) {
        androidLogger()
        androidContext(this@KalendarApp)
        modules(appModule, networkModule, viewModelsModule)
    }
}
? 🤔
a
weird that you need to extract those components out of DI modules 🤔