Hi everyone, I’ve created a Kotlin Multiplatform D...
# android
p
Hi everyone, I’ve created a Kotlin Multiplatform Dependency Injection Library in order so simplify things in DI world. Forget about modules, submodules, components, etc.... Let the compiler do all that staff for you, so you can simply USE dependency injection https://github.com/corbella83/PopKorn
🔥 3
g
How does it work on multiplatofrm if it requires kapt?
p
Yes, you can check out my full multiplatform example project (using popkorn/ktor/coroutines/kotlinx.serialization): https://github.com/corbella83/FullMultiplatform
g
I’m just asking how it works in general
you generate code with kapt in some special source set for iOS? But how do you detect if something is changed in iOS specific code
p
The main idea is that kpt only generates kotlin files, that are then added to the sources to be compiled in common
g
yes, I understand this So if something is changed you have to restart generation manually?
have you concider to use KMP instead of Kapt? I understand this is still a preview, but at least it would integrate properly with all multiplatform targets, doesn’t have all those limitations and potenitally it’s faster
p
no, kapt automatically handles any file change, and recompiles only this changes (incremental kapt)
g
kapt automatically handles any file change, and recompiles only this changes
But capt cannot see iOS source set
p
yes, my idea is to use KMP, i’m working on it
g
or you add it as jvm source set?
p
no, files are not generated in ios platform, only on the multiplatform project
if you want to inject instances from swift code, has to be manually for now
popkorn().addInjectable(…)
g
but what if I have Kotlin code not only in common source set, but in any other platform source set, any native, iOS, and I want to use DI there (declare some modules used only for this platfrom), it will not work, right?
p
yes it will work, you can use it in platform modules too: just import the correct lib: popkorn-jvm, popkorn-native, popkorn-js, etc
g
but it would runtime lib
p
no. I strongly recommend that you take a look at the example above, to better understand the full integration
g
I checked example, it doesn’t have any details
I see only JVM module in popcorn-example
what if I want @Injectable for my platform specific class, which doesn’t exist in common code
p
Then you can add @Injectable to that class that in in patform specific folder
The example that you have to look is not the one inside popkorn project. Its another project:
g
okay, cool
g
but returning back to my question, so I have now annotation in platform source set, it should be read by Annotaton Processor, and just curious how it works in this case
okay, I see now, PlatformData is defined in common
but what if PlatformData for iOS require some platform dependency to constructor? I believe it also should be somehow abstracted in common
I see your approach
j
@Pcorbella Am curious.....there are a number of existing DI libraries that work with Kotlin Multiplatform (e.g. Koin and Kodein).....was there any particular shortcoming in these that you saw?
j
PopKorn
approach without components and modules is interesting
p
@John O'Reilly There are not many. Just Kodein because Koin is not stable yet. My motivation was to avoid the need to create hundreds of classes just to use a library (modules, submodules, components, etc) ... For me, a well-done library is the one that can achieve its main goal in a couple of lines.
j
I'm only familiar with Koin and am using it in a couple of Kotlin Multiplatform projects.....those projects are admittedly quite small but still very little overhead in using that there
I've used it on much larger Android projects and again don't require all that overhead you're referring to
g
This library looks so far much more promising than Koin/Kodein which efficiently could be used only as service locators https://github.com/evant/kotlin-inject
j
@John O'Reilly well, you can have hundred of modules with Koin or Dagger... that is a lot of code you can just skip
p
@gildor, kotlin-inject is not multiplatform
g
it’s true
this why it’s “promising” in general %)
it’s also planned to support MPP eventually https://github.com/evant/kotlin-inject/issues/1
But still blocked by KSP, which itself blocked by Kotlin Compiler ¯\_(ツ)_/¯