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

Maurice Jouvet

01/21/2020, 7:39 AM
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

Mikołaj Kąkol

01/21/2020, 9:55 AM
I'm not familiar with any JSR 330 compatible solution for MPP
f

Foso

01/21/2020, 11:48 AM
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

Maurice Jouvet

01/21/2020, 12:55 PM
Thanks!!
k

Kris Wong

01/21/2020, 1:56 PM
you will need a module/provides method for any classes from your framework. you will have to remove @Inject
m

Maurice Jouvet

01/21/2020, 3:06 PM
Yes thank you, I am trying multiple solution. Right now I am trying Kodein.
k

Kris Wong

01/21/2020, 3:10 PM
when I tried Kodein it did not support concurrency, so keep that in mind
(on native platforms)
m

Maurice Jouvet

01/21/2020, 3:13 PM
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

Kris Wong

01/21/2020, 3:20 PM
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

Maurice Jouvet

01/21/2020, 3:22 PM
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