https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
m

Mats-Hjalmar

03/17/2024, 12:42 PM
Hi i have been messing around with alternatives to Koin and Kodein for dependency injection. Im looking for a compile time dependency injection where it is possible to inject into viewmodels and use them in the Composable screens. I used dagger/hilt previously but since it is Android only i cant use it in CMP projects. Is there a way to “make” hilt suitable for CMP projects?
🚫 1
c

Chrimaeon

03/17/2024, 12:53 PM
No, because dagger is generating Java code.
m

Mats-Hjalmar

03/17/2024, 12:55 PM
I interpret this as it could be used with KSP and thereby generating kotlin code https://dagger.dev/dev-guide/ksp.html
c

Chrimaeon

03/17/2024, 12:56 PM
hilt might generate kotlin code but dagger in the end is a java library that is generating java code.
m

Mats-Hjalmar

03/17/2024, 12:58 PM
So if i understand this correctly my generated classes would be in kotlin but the library that reads these classes (provide my viewmodels to the screen) is in java and therefore do not work in KMP
c

Chrimaeon

03/17/2024, 1:01 PM
no, dagger/hilt is a “code generator” that is generating java code that is compiled with you application. Hilt is just taking away writing boilerplate code for your android application so you can inject you modules easily into your android components.
👌 1
Dependency Injection per se is just a programming style, you don’t need a libaray for it.
2
m

Mats-Hjalmar

03/17/2024, 1:03 PM
Thats true, but i like it.
c

Chrimaeon

03/17/2024, 1:04 PM
sure, it’s convenient 🙂
s

streetsofboston

03/17/2024, 1:50 PM
Kotlin Inject https://github.com/evant/kotlin-inject has compile time checks, but I don't think it supports Hilt-like features for ViewModels and such.
1
5 Views