Hey everyone! :wave: We’re planning to adopt KMP ...
# multiplatform
t
Hey everyone! 👋 We’re planning to adopt KMP in our mobile tech stack, primarily to share business logic across our Android and iOS apps. One of the key components we’d like to share is the ViewModel, and we heavily use Koin for DI in our Android app. Right now, we’re on Koin 3.5.6, which (as far as I know) doesn’t support
Module.viewModel
injection for multiplatform. This seems to have been introduced in the 4.x.x release. (Please correct me if I’m wrong here) While we do have an initiative to migrate to Koin 4.x.x, in the meantime we’re stuck with 3.x.x in our shared KMP project. Our current idea is to go with the expect/actual approach to provide the ViewModels via Koin for both platforms. Here’s a rough outline of what we’re thinking - would love feedback on whether this approach makes sense, and if there are any pitfalls we should be aware of:
Copy code
// commonMain 
expect val platformModule: Module

val module: Module = module {
    includes(platformModule)
}

// androidMain
actual val platformModule: Module = module {
    viewModel {
        MyViewModel()
    }
}

// iOSMain
actual val platformModule: Module = module {
    factory {
        MyViewModel()
    }
}
Any suggestions or alternative approaches are welcome! 🙏
f
Take a look at this GitHub https://github.com/frankois944/kmp-mvvm-exploration. Usage of a KMP viewmodel on SwiftUI and usage of Koin in a swift way
I’m heavily use koin / koin annotation (the latest version) on my professional project with SwiftUI screen