If you're not yet using Koin Annotations, it's qui...
# koin
j
If you're not yet using Koin Annotations, it's quite simple and makes life easier πŸ™‚. I wrote an article on how to set it up and how the annotations work: https://medium.com/@jacobras/migrating-to-koin-annotations-in-a-multiplatform-project-1e83ba3b5988
πŸ‘πŸ½ 1
koinscroll 2
πŸ”₯ 4
πŸ‘ 1
jam 1
c
I had tried to make this work with KMP but had not succeeded. Are you saying that everything before the "Platform-specific code" section works equally well for KMP?
Also, do you have any experience trying to set this up in convention plugins?
j
> Are you saying that everything before the "Platform-specific code" section works equally well for KMP? Yep! I use Koin Annotations in multiple KMP projects like that, even without platform-specific code. Haven't set it up through convention plugins yet.
πŸ‘πŸ½ 1
j
Copy code
Also, if you're used to Hilt, you might be tempted to write @Inject constructor. That's not needed with Koin, the single annotation above the class is enough.
I love this πŸ˜„
❀️ 1
Btw does Koin work with android specific things. I was curuous about scopes such as Activity. I have some painful code where I need to have Activity Context in some scenarios, does Koin being able do that multiplatform somehow? Ofc only provided inside androidMain in that case, so not use in other platforms.
@Jacob Ras Also from your experience, how does Koin Annotations impact compile time? πŸ™‚
j
I love this πŸ˜„
Thanks, haha! Wasn't meant as a snarky remark or anything, but I just noticed how I was a bit surprised I didn't have to write it when I started using K-A.
Btw does Koin work with android specific things
Absolutely. Of course, inside
[androidMain]
you can provide&inject anything you like, but if you use
koin-android
with inside the
startKoin{}
block the line
androidContext(this@Application)
, then
Context
becomes available to inject anywhere by default. I'll add that to the post!
Also from your experience, how does Koin Annotations impact compile time?
I have done no benchmarks here, but KSP is a speedy plugin. In the biggest Koin-project I have I don't feel K-A impacts performance a lot. Compiling code still takes up most of the time and often the ksp tasks are up-to-date and skipped anyway because its a modularised project πŸ™‚
j
Yeah I use Context. Problem is I want Activity and not Application scope. I can provide my own things but it brothers me a lot right now send Activity context from Local context in compose up to Koin. Also having isolated scopes per viewmodel, application, activity is nice. But how deal with that in KMP 😁
Right many DI still using Kapt or such? Nice hear no negative downsides in compile step. Interesting Koin changes their mind provide compile time safety.
j
For activity, you can use parameters. You specify the dependency as an
@InjectedParam
and when you actually retrieve the thing you need, in you activity, you do
get<SomeThing> { parametersOf(this@Activity) }
. But I'm wondering why you'd like to inject an activity, I find it's often best to avoid such constructions πŸ™‚
Right many DI still using Kapt or such?
kapt
is a similar thing to
ksp
. Both are compiler plugins, but ksp is newer, faster ("up to two times faster") and works with Kotlin/Native.
j
No thats the thing I dont have the Activity anywhere in the code, as I never fetch the dependecy ever. In my case its androidx credential manager in one place require using Activity Context, and I do not want to send in Activity. What I can do is using like Applicadtion Activity lifecycle registry and save current Activity. THen not need to send in Activity from any place. Also LocalContext in COmpose scope will be in Activity level, hence was wonder if Koin had something built in for this.
I never want to use Android SDK things ever if not need to. But some libraries require Activity and some require Application context. Dont ask me why πŸ˜„
In my own code I never ever rely on Context.
j
Sometimes you just need it πŸ™‚
Ktor
needs it in its setup, for example. And if you want to do anything with Bluetooth for example, you need the service which is obtained through Context. Nothing wrong with that πŸ˜„
j
The wrong is the library not solve it by themselves from Application Context I guess, and force that upon the consumer πŸ˜„
Anyway so no real solution for this in Koin that being KMP safe πŸ™‚
Super nice article btw πŸ™‚
❀️ 1
p
@Gustavo Barbosa Barreto
a
Hi there .. gladly im just starting moving my project to use .. but if i understand .. koin annotation ksp didnt generate it's module under common folder right ? is there a way to generate the koin modules under common modules ? im impressed with the compile checking and i just test it with simple annotations and it works
a
@Jacob Ras cool article πŸ‘
❀️ 1
happy to see some Koin Annotations promotion πŸ˜„
j
Thanks @arnaud.giuliani! Happy to spread the awesomeness πŸ˜„
πŸ™Œ 1
v
Hi all, does someone has example how to set it up just for desktop app? No matter what I try, genereted folder is not there.
a
KSP plugin is setup?
v
Hi @arnaud.giuliani, tnx for reaching out. I've managed to do it. I got now another problem after adding koin and ksp setup. App will not run on desktop. It never starts (no errors also)...
a
I don't have lots of resources here. I would get back to setup without KSP, ensure setup works and then add KSP
j
@Vedran does "it never starts" mean it crashes? Or what else do you see happening when you start a run? Does the build at least succeed?
v
Hi @Jacob Ras, yes app was building normally. But it would crash ASAP when starting. No errors anywhere. I found out here that there is runDistributable option in gradle. So with that I was able to get the error. Problem was I was using jdk21. And I needed to add additional setup:
Copy code
buildTypes.release {
    proguard {
        version.set("7.4.2")
        isEnabled.set(true)
        optimize.set(false)
        obfuscate.set(false)
        configurationFiles.from(project.file("<http://compose-desktop.pro|compose-desktop.pro>"))
    }
After adding above into build.gradle.kts it works.
πŸ™ 1
j
Ah, glad to hear it's sorted πŸ›³οΈ
119 Views