Annotation Processor to generate a list of Koin Modules.
In your module buildscript, add the KSP plugin:
plugins {
id("com.google.devtools.ksp") version "1.6.0-1.0.1"
}
Then add the following dependencies:
val kmpLatestVersion = "2.0.0"
implementation("com.rocca23:koin-module-processor:$kmpLatestVersion")
ksp("com.rocca23:koin-module-processor:$kmpLatestVersion")
Declare your Modules as top-level properties, annotated with @KoinModule
:
// MyModule.kt
@KoinModule
val myModule = module {
single {
// ...
}
factory {
// ...
}
}
After building the project, a top-level property named org.koin.generated.koinModules
is created.
This is a list of all the annotated modules discovered by the annotation processor.
You can finally add the generated list to your Koin initialization:
startKoin {
androidContext(context)
modules(koinModules)
}
Whenever you add a new Koin Module, it will be automatically added to the same list.
To avoid ambiguity and class-loading conflicts in case you want to use KMP in multiple project modules, you can specify a prefix
to be used in the generated names by passing an argument named "kmp.prefix"
to the
annotation processor in your build.gradle.kts:
kapt {
arguments {
arg("kmp.prefix", "library")
}
}
This way, the generated module list will be named (as per the above example) libraryKoinModules
.