Hi I am rather new to dipendency injection with ko...
# koin
r
Hi I am rather new to dipendency injection with koin and koin-annotations. I have a kmp project with a ktor server and iOS and Android apps. the base setup works fine. Now I tried to define a class inside the shared gradle module and annotate it with
@Single
:
Copy code
package com.example.data.mapper
import org.koin.core.annotation.Single

@Single
class XyzDtoMapper {
...
}
I try to scan for this class with a module defined inside the server gradle module:
Copy code
package com.example.di

import org.koin.core.annotation.ComponentScan
import org.koin.core.annotation.Module

@Module
@ComponentScan("com.example.data.mapper")
class MapperModule
Is such a Scan over multiple gradle modules possible, and if do, how can I achieve this? The Mapper module is generated but the
XyzDtoMapper
is not present in it.
k
It should work in multiple modules, can you share a reproducer?
m
I have a similar issue that I'm trying to resolve. I have multiple kmp modules and scan works well in the androidApp module. But I have another kmp module that we use for aggregating things for iOS and in that module the file is generated but it does not pick up the dependencies so the koin module is empty
👀 1
r
@Kibet Theophilus Here I have a small example: https://github.com/rkreienbuehl/koin-example Maybe I am missing some configuration? (In this example I only implemented koin for the server and shared modules)
k
@Roger Kreienbühl I had missed the question😬, the scan only works for a single module...AFAIK
r
@Kibet Theophilus Ok, thanks for the clarification. In the meantime I found a workaround. I added a
SharedMapperModule
inside the shared gradle module:
Copy code
@Module
@ComponentScan("dev.kreienbuehl.swissclimbing.data.mapper")
class SharedMapperModule
and changed
MapperModule
inside the server as followed:
Copy code
@Module(
    includes = [
        SharedMapperModule::class
    ]
)
@ComponentScan("dev.kreienbuehl.swissclimbing.data.mapper")
class MapperModule
What i didn't see anywhere in the docs was how to get the shared module to generate the generated koin files. I figured that out by investigating the example listed in the docs. I had to add this to the `build.gradle.kts`of the shared module:
Copy code
dependencies {
    // KSP Tasks
    add("kspCommonMainMetadata", libs.koin.ksp.compiler)
}

tasks.withType<KotlinCompile>().configureEach {
    if (name != "kspCommonMainKotlinMetadata") {
        dependsOn("kspCommonMainKotlinMetadata")
    }
}

afterEvaluate {
    tasks.filter {
        it.name.contains("SourcesJar", true)
    }.forEach {
        println("SourceJarTask====>${it.name}")
        it.dependsOn("kspCommonMainKotlinMetadata")
    }
}
Don't know if this is the best way, but it works.
k
glad it works, doc setup should be here...needs some updates though - https://insert-koin.io/docs/reference/koin-annotations/kmp#ksp-setup
r
These docs I found, but the part with
Copy code
tasks.withType<KotlinCompile>().configureEach {
    if (name != "kspCommonMainKotlinMetadata") {
        dependsOn("kspCommonMainKotlinMetadata")
    }
}

afterEvaluate {
    tasks.filter {
        it.name.contains("SourcesJar", true)
    }.forEach {
        println("SourceJarTask====>${it.name}")
        it.dependsOn("kspCommonMainKotlinMetadata")
    }
}
is not mentioned in it. Withoout it, the code was not generated in my case. In the linked example app https://github.com/InsertKoinIO/hello-kmp/tree/annotations I found this as workaround instead of adding
Copy code
dependencies {
    // KSP Tasks
    add("kspCommonMainMetadata", libs.koin.ksp.compiler)
}
but I needed both that it worked for the server.
👍 2