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

Ciaran Sloan

11/22/2023, 3:02 PM
Trying to setup koin annotations in a multi module KMP project, but facing some difficulty. Does anyone know of any sample projects around that demonstrate the implementation?
j

Jacob Ras

11/22/2023, 3:10 PM
No online example unfortunately but can maybe help if you share an error here. One thing I couldn't get working is generating the code for both common and Android at the same time. Either only Android or only common.
🙌🏼 1
c

Ciaran Sloan

11/22/2023, 3:11 PM
yeah im not getting code to generate automatically. I have actually just manually run the task
kspCommonMainKotlinMetadata
and have some code generation happening
j

Jacob Ras

11/22/2023, 3:13 PM
Right, so when you run that, it works? Or still incomplete?
I have an
app
module that I share between Android (activity inside) and iOS (exposes Compose Multiplatform entry point). In its
build.gradle.kts
I have the following KSP-related stuff. Maybe there's something there that helps:
Copy code
dependencies {
    coreLibraryDesugaring(libs.desugaring)
    debugImplementation(compose.uiTooling)

    add("kspCommonMainMetadata", libs.koin.compiler)
}

// From <https://github.com/InsertKoinIO/hello-kmp/blob/annotations/shared/build.gradle.kts>
tasks.withType<KotlinCompile<*>>().configureEach {
    if (name != "kspCommonMainKotlinMetadata") {
        dependsOn("kspCommonMainKotlinMetadata")
    }
}

// From <https://github.com/google/ksp/issues/567#issuecomment-1510477456>
kotlin.sourceSets.commonMain {
    kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin")
}
c

Ciaran Sloan

11/22/2023, 3:15 PM
the only thing I don't have is manually forcing the dependency on the
kspCommonMainKotlinMetadata
source set
I'll try adding that
I do see the codegen when I manually run that gradle task, but not able to access that generated code across modules. Looking deeper to see if I've missed something
j

Jacob Ras

11/22/2023, 3:17 PM
Not sure if that's actually needed, even in my example. I mean I'm only adding the common processor so I don't think the if-statement is doing much. But I ain't touching gradle stuff that works 😛
Is the issue in an Android module?
sourceSets["main"].resources.srcDirs("src/commonMain/resources")
is something I have inside the
android{}
block.
c

Ciaran Sloan

11/22/2023, 3:17 PM
no, I dont have any platform specific code. I don't expose koin beyond the KMP modules, other than an injection point
so in the case of moudule-B depends on module-A, (both being KMP modules), I can see the generated code in the build/generated/ksp directory of module-A, but I cant import that in module-B
which probably isnt koin related per say, to be fair
okay think I've spotted the issue
ive set the wrong directory for the added source directories
the docs show:
srcDirs("build/generated/ksp/main/kotlin")
but infact, it generated in
build/generated/ksp/metadata/commonMain/kotlin
must be a difference between a typical kotlin project vs a multiplatform one
j

Jacob Ras

11/22/2023, 3:23 PM
Hmm, interesting. I'm not sure how the "metadata" naming works in KSP, when it pops up and what it means. Does it work now?
c

Ciaran Sloan

11/22/2023, 3:24 PM
well its not quite working yet, but I think im on the right path now!
Thanks for your help, I'll reach out if I hit any more blockers!
👍 1
j

Jacob Ras

11/22/2023, 3:24 PM
That's at least motivational enough to continue yeah 😁
🙌🏼 1
c

curioustechizen

11/22/2023, 4:14 PM
This thread is interesting for me. I have a KMP project with Koin and I've been meaning to include Koin annotations. But I've been put off by the complicated looking gradle setup. Also I did not know of any real world KMP examples that do use koin annotations.
🙂 1
c

Ciaran Sloan

11/22/2023, 4:14 PM
I was the same, but it actually hasn't been too bad. My setup is even more complicted with convention plugins being applied to the modules
with modularisation so popular/common now, it'd be nice to have an example of a multi module setup for sure
r

rudolf.hladik

11/22/2023, 6:15 PM
Branch annotations
c

curioustechizen

11/23/2023, 3:40 AM
Isn't the hello-kmp sample quite basic? Things start getting complex when you have multiple modules, some of which are kmp modules, some kotlin modules and the interdependencies between them come into play. Plus there's the convention plugin angle.
c

Ciaran Sloan

11/23/2023, 8:52 AM
yeah I didnt find the basic sample a lot of help, but it was the first thing I checked out. I didn't however need to use the following block from the shared gradle file:
Copy code
afterEvaluate {
    tasks.filter {
        it.name.contains("SourcesJar", true)
    }?.forEach {
        println("SourceJarTask====>${it.name}")
        it.dependsOn("kspCommonMainKotlinMetadata")
    }
}
It also would have been helpful if the demo linked the open github issue providing more context on the workaround and why its in place -> https://github.com/google/ksp/issues/567. I wasn't initially keen on putting in this workaround, without there being context on why its there
5 Views