Hello! I'm building a multiplatform project target...
# multiplatform
r
Hello! I'm building a multiplatform project targeting ios, android, jvm, and eventually js. The idea is to create a multiplatform library with the network layer and some business logic in it that I can share across my different projects. I also want to share this library with a desktop compose app I'm starting to create. I'm looking for a decent (or suggested) library to do DI in the multiplatform library. So far, I've found that Koin and Kodein have support for multiplatform. Is there a preferred library for DI on KMM? Do you prefer one over the other? why? thanks in advance.
e
i am currently fighting with runtime crashes in Koin on iOS, it's pretty miserable. Always prefered the compile-time errors of Dagger when building for Android. tbh I think you're better off leaving DI up to the client apps and just exposing all the pieces, but I haven't done that at scale yet
🙌 1
r
bummer!
the problem is I need DI to some degree in the common shared code
are those crashes happening because of missing stuff or because the library @edenman?
e
crashes in KMP just aren't surfaced very well by default, and on top of that i'm building an iOS App Extension so i can't use any of the Xcode built-in crash handling stuff afaik
r
thanks for sharing
👍🏾 1
have you head about kodein? would it be any different?
e
haven't tried it
j
although the typealias thing is nonsensical
r
I was reading about it because it looks pretty similar to Dagger, at least on syntax but it doesn't mention anything about multiplatform support. Do you know anything about it @jw?
j
Well I know it's multiplatform and I know the typealias thing makes me not want to use it but otherwise it seems great.
r
thanks! any thoughts on koin/kodein @jw?
j
many. they are not DI libraries. they are service locators that allow you to manually perform DI. A subtle distinction, sure, but advertising them as DI is a lie and allows people to settle for writing a bunch of boilerplate that an actual DI library can generate (and validate) on your behalf. If I advertised OkHttp or Ktor client as an image loading library you should be upset, because while it can be used to load images, it is not an image loading library. Coil is an image loading library and it leverages OkHttp to do HTTP. These libraries you mention are libraries which allow you to do DI. I prefer libraries which themselves perform DI on my behalf. They sell you bricks, mortar, and wood. DI libraries sell you the finished house.
Also Dagger is migrating to KSP which will unlock multiplatform but it's still months away.
r
great insight man! much appreciated it
yeah, I read something about they were SL
great to hear about Dagger going MP!
j
you can do a lot with a service locator, though. don't get me wrong. and in the end everything else matters more than whether you write
get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get()
a few times or the library generates it for you.
r
no no, I get it. I'd rather prefer the library to do as much boilerplate code as possible specially because it's a big project with some degree of complexity.
j
Dagger 2 and kotlin-inject (I forgot the name is also kinda bad) will also be much faster than any of the service locator-based solutions. When you use Koin or Kodein you're basically using the behind-the-scenes implementation of Dagger 1 but writing the wiring yourself.
r
Good to know. For now I like the implementation looks clean and simple. The syntax looks similar to Dagger which is another point to it in my case.
k
There is also PopKorn, that is a DI multiplatform library, but I haven’t used it, so I can’t recommend it https://github.com/corbella83/PopKorn
👍 1
m
You can also check out koin-annotations https://medium.com/koin-developers/koin-annotations-c06c2b876ebe. It just got released allowing you to define dependencies using annotations instead of manual module creation, reducing boilerplate code and bringing compile time checks
👍 2
k
Thanks for the information about
Koin Annotations
. It looks interesting. I especially like how you bind your classes to interfaces:
Copy code
@Single(binds = [MyBoundType::class])
seems like one will be able to avoid writing lots of boilerplate
r
Also Dagger is migrating to KSP which will unlock multiplatform but it's still months away.
Wouldn't Dagger also need to start generating Kotlin instead of Java if it wanted to go KMP? I always assumed that would be a bigger blocker than KSP support but I haven't been following the project closely.
j
Yes. Sorry I meant the completion of KSP is months away. There was talk of producing Kotlin before I left Google but it's unclear if they'll do that as the next step.
d
do you know if anything changed? I’ve tried looking for an issue on the dagger github about this but I didn’t find anything meaningless. we are evaluating kotlin multiplatform and DI is a major pain point with Koin / Kodein and Kotlin-inject all being far from what I’d want from a DI framework.