Trying to setup koin annotations in a multi module...
# koin
c
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
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
yeah im not getting code to generate automatically. I have actually just manually run the task
kspCommonMainKotlinMetadata
and have some code generation happening
j
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
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
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
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
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
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
That's at least motivational enough to continue yeah ๐Ÿ˜
๐Ÿ™Œ๐Ÿผ 1
c
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
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
Branch annotations
c
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
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