I have an Android application using Dagger2 as DI....
# multiplatform
m
I have an Android application using Dagger2 as DI. I am currently using @Inject whenever I can, this application is already working very fine and is very efficient. Now I want to use this one with KMP. I have started to migrate some part of the application, but I am stuck with the classes with @Inject constructor. Do I have to remove all these to use generic constructor and let the DI on the platform specific, or is there another solution?
m
I'm not familiar with any JSR 330 compatible solution for MPP
f
You cant use Dagger outside of KotlinJvm because it is using the Java Annotation Processor. When you want to write a multiplatform annotation processor, you can try my library https://github.com/Foso/MpApt
m
Thanks!!
k
you will need a module/provides method for any classes from your framework. you will have to remove @Inject
m
Yes thank you, I am trying multiple solution. Right now I am trying Kodein.
k
when I tried Kodein it did not support concurrency, so keep that in mind
(on native platforms)
m
Ah ok, thank you. Right now I am struggling to provide my android context, when I am on android.. I don’t know how to mix the three (common, android, ios) I just want to provide that: On Common:
Copy code
bind<DbParams>() with provider {
    DbParams() // not possible here
}
On Android (Don’t have Kodein yet don’t know how to do that yet), but the class params is:
Copy code
actual class DbParams(val context: Context)
On iOs: Nothing to use
Copy code
actual class DbParams
The all thing is to do that:
Copy code
bind<DbParams>() with provider {
    DbParams()
}

bind<Database>() with provider {
    val params by kodein.instance<DbParams>()
    provideDatabase(params)
} // Database

bind<MyLocalDS>() with provider {
    val database by kodein.instance<Database>()
    MyLocalDSImpl(database)
}
If you have an idea. 🙂
k
I'm not sure what exactly you're asking, but if you have an expect class, it really makes the most sense to have the same constructors on both platforms.
if you want to construct it in common code, of course. otherwise move the instantiation to platform-specific code
m
Exactly, but I did that because I need the context on Android, but Nothing for iOS. So I created this class with nothing.
I can do that on Android:
Copy code
actual class DbParams(val context: Context)
And iOS:
Copy code
actual class DbParams