Hi! We’re in the process of adding KMP (KMM specif...
# multiplatform
m
Hi! We’re in the process of adding KMP (KMM specifically) to our iOS and Android projects. We’ve been doing it so far using a factory pattern, but are looking to use DI going forward. We’re currently using Hilt on Android and Swinject on iOS. We’re looking at Kotlin Inject and Kodein as possible KMP solutions. Wondering if anyone has tried to bridge between a KMP DI and a native DI, or if the recommendation would be to completely re-do the DI on each native platform as well (which would be fairly time intensive given the size of our apps)
j
Koin is KMP supported, https://insert-koin.io/
m
We can also evaluate Koin, but my question is more around bridging from KMP to native platforms with different DI systems
j
Not entirely sure what want achieve. I would guess you could make isolated KMP modules shared between iOS and Android. I tested combine Koin and Hilt. Main issue imo is testing, how to overload stuff if like instrumentation tests. If isolated easier. For Android I would use startup library install Koin or other framework instance. Usually possible having multiple graphs as long as isolated. But I havent bridge on the level you refer to myself. I would guess having multiple could increase compile time.
j
@Mike Welsh very interesting question! If I understand correctly, what you're asking is using [any KMP] DI inside your shared
common
code, but still allowing that to be injected/used in app code by Hilt and Swinject? I'm not aware of a library that does this, but I haven't looked for it. I'd say you can do this manually by creating a wrapper. For example, let's say you're using Koin inside your shared code to inject and provide
TestClass
. You could then create a wrapper in your
androidMain
code, where you create a Hilt module that provides
TestClass
by getting it from Koin. Downside is that you have to manually provide all these, but the upside is that you can easily provide the platform-specific DI wrappers by bundling it into your shared artefacts. This is a theoretical idea. I haven't done this yet, but I'm fairly sure something like this would work.
@Mike Welsh I made a small sample project illustrating my idea, hope it helps to show what I mean. If you or anyone else reading along has any feedback on it, please share! I'd be happy to learn from it. https://github.com/jacobras/KMP-shared-DI/
v
I mean. Would it be a problem if you have DI graphs in the KMM with koin, and you have your Hilt in the androidApp. So you are free in your Hilt provides methods to call KoinPlatform.get() no?
m
It wouldn’t be a problem, I’m just looking for examples of what people have tried or have recommendations for good approaches or things to avoid 🙂
v
No you are not. You are asking 😄
j
@Vlad that's exactly what I did in my sample project above to try it, and I like how it turned out. I'll probably go that way in my personal project until I'm ready to switch to Koin 🙂 (because I'd really like to use KSP on Android.. but cannot due to Hilt 😬 )
j
Hey @Mike Welsh, I'm in the exact same boat as you. Recently added KMM to our android / ios projects and have been using the factory pattern to provide dependencies so far. We currently are bridging the native <> KMM platform dependencies similar to @Jacob Ras's suggestion. I'll be looking into kotlin inject today to achieve something similar to what you're looking to do. Will report back with findings. Please post your findings as well.
m
Will do - will likely be looking at this in the coming weeks, so might not have results for a little while
So that example project looks great for Android, but I’m not sure it’ll work for Swinject since I don’t think I can have Swinject in the iOS target of KMP (it’s a Swift library, not Objective-C) - but it’s definitely a good option for our Android side of things and maybe we can do something on the iOS side to ease this integration
j
@Mike Welsh you're right, for Swinject we can expose the dependencies through a KoinModule (in the shared module's iOS source set) and then in the Android app integrate them into Swinject. I've updated my sample repository to do that (https://github.com/jacobras/KMP-shared-DI), while keeping it a bit separated using Swinject's
Assembly
mechanism (which is like a module in Dagger/Koin).
🙌 1