How to Inject application context from 'app' module to 'network' module using Koin DI
Both answers by @Rajat and @Andrey are correct. In fact if you look at the sources, you will see that androidContext()
is just an extension function to get()
, so these 2 definitions are identical:
val appModule = module {
viewModel { LoginViewModel(get()) }
}
...
val appModule = module {
viewModel { LoginViewModel(androidContext()) }
}
Answering your question, since get()
and androidContext()
are members of the module
DSL object, you could do this:
val networkModule = module {
single { Network(androidContext()) }
}
Or simply (I prefer this one for brevity):
val networkModule = module {
single { Network(get()) }
}
Application context is available inside a module through the function androidContext()
.
val appModule = module {
viewModel { LoginViewModel(androidContext()) }
}
This should solve your problem.