https://kotlinlang.org logo
#koin
Title
# koin
j

Jacob Ras

02/28/2024, 11:47 AM
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

curioustechizen

02/28/2024, 12:49 PM
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

Jacob Ras

02/28/2024, 12:52 PM
> 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

Joel Denke

02/28/2024, 1:11 PM
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

Jacob Ras

02/28/2024, 1:32 PM
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

Joel Denke

02/28/2024, 1:35 PM
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

Jacob Ras

02/28/2024, 1:45 PM
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

Joel Denke

02/28/2024, 1:48 PM
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

Jacob Ras

02/28/2024, 1:50 PM
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

Joel Denke

02/28/2024, 1:57 PM
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

Pedro Francisco de Sousa Neto

02/28/2024, 2:27 PM
@Gustavo Barbosa Barreto
a

Angga Ardinata

02/29/2024, 2:41 AM
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

arnaud.giuliani

02/29/2024, 6:12 PM
@Jacob Ras cool article 👍
❤️ 1
happy to see some Koin Annotations promotion 😄
j

Jacob Ras

02/29/2024, 7:47 PM
Thanks @arnaud.giuliani! Happy to spread the awesomeness 😄
🙌 1
v

Vedran

03/07/2024, 12:44 AM
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

arnaud.giuliani

03/08/2024, 8:38 AM
KSP plugin is setup?
v

Vedran

03/09/2024, 3:14 PM
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

arnaud.giuliani

03/11/2024, 4:09 PM
I don't have lots of resources here. I would get back to setup without KSP, ensure setup works and then add KSP
j

Jacob Ras

03/11/2024, 5:55 PM
@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

Vedran

03/11/2024, 5:59 PM
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

Jacob Ras

03/11/2024, 6:09 PM
Ah, glad to hear it's sorted 🛳️
32 Views