Hi people! Anybody could build an app using Koin-c...
# multiplatform
h
Hi people! Anybody could build an app using Koin-compose and room in KMP? I tried different approaches and I can't make it yet 😕 This piece of code is in the setContent of the MainActivity:
Copy code
KoinContext(context = koinApplication {
    modules(allModules)
}.koin) {
This is in the Platform.Android:
Copy code
single<AppDatabase> { getDatabase(get()) }
This is in the navHost to obtain the instances of the view models:
Copy code
val userListViewModel: UserListViewModel = koinViewModel()
And this is how I create the instances in a koin module:
Copy code
private val viewModelModule = module {
    viewModel {
        UserListViewModel(get(), get(), get())
    }
With all these config the following error appears: Caused by: org.koin.core.error.NoBeanDefFoundException: No definition found for type 'android.content.Context'. Check your Modules configuration and add missing type and/or qualifier!
a
You need to pass the context for Android target and call the
startKoin
method(this should happen in all the supported target)
Copy code
actual class KoinInitializer(private val context: Context) {
    actual fun init() {
        startKoin {
            androidContext(context)
            androidLogger() // todo add build config check
            modules(commonAppModules, platformModules)
        }
    }
}
then in your android application you need to initialise the
KoinInitializer
h
not working
r
@hectoruiz I have setup Koin using an expect/actual KoinInitializer like this:
Copy code
class MainApplication : Application() {
    override fun onCreate() {
        super.onCreate()

        KoinInitializer(applicationContext).init(androidModule)
    }
}
Copy code
actual class KoinInitializer(
    private val context: Context
) {
    actual fun init(platformSpecific: Module): KoinApplication = startKoin {
        androidContext(context)
        androidLogger()
        modules(appModule() + platformSpecific)
    }
}
Where we pass the Context to Koin, there is a working app at https://github.com/Appmilla/KMPJetpackDemo, which uses DataStore, it might help
I based this off of Philip Lackners JetPackViewModel-CMP example which has a video see https://github.com/philipplackner/JetpackViewModel-CMP/blob/master/composeApp/src/androidMain/kotlin/KoinInitializer.kt And

https://www.youtube.com/watch?v=O85qOS7U3XQ

h
Yes! Thanks for this, after talking with Atul I achieved! Now fighting vs savestatehandle in koin jejjej
👍 1