
Generates Koin modules from simple annotations, eliminating boilerplate while producing lifecycle-organized loaders and support for singletons, factories, scoped instances and ViewModel bindings.
Koin Generator is a powerful KSP (Kotlin Symbol Processing) library that automates the creation of Koin modules for your Kotlin Multiplatform projects. By simply annotating your classes, you can eliminate boilerplate code and ensure a clean, scalable dependency injection setup.
It includes:
@Single, @Factory, @Scoped, and @KoinViewModel.@InstallIn (e.g., Application, Activity).loadApplicationModules(), loadActivityModules()) to easily
load modules where they are needed.Because this library relies on KSP to generate code, you must manually trigger the metadata generation the first time you set up the project or after adding new annotations. Run the following commands in your terminal:
./gradlew core : kspCommonMainKotlinMetadata --no - configuration - cache
./ gradlew sharedUI:kspCommonMainKotlinMetadata-- no -configuration - cacheNote: Please replace x.y.z with the latest version.
In your build.gradle.kts file for your shared module (e.g., core, shared):
// Add the annotations library
implementation("io.github.the-best-is-best:koingenerator-annotations:x.y.z")
// Add the KSP processor
ksp("io.github.the-best-is-best:koingenerator-processor:x.y.z")In the same build.gradle.kts, configure the KSP processor to specify which packages to scan and
where to place the generated loader file.
ksp {
// Required: Comma-separated list of base packages to scan for @Module annotations.
// The processor will scan these packages and all their sub-packages.
arg("packageName", "io.github.tbib.core,io.github.tbib.koingeneratorapp")
// Optional: The package where the generated 'KoinModuleLoader.kt' file will be created.
// Defaults to the package of the first module found.
arg("koinLoaderPackageName", "io.github.tbib.koingeneratorapp.di")
}Ensure the generated code is included in your source sets.
kotlin {
sourceSets.named("commonMain").configure {
kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin")
}
}Marks an interface as a definition for a Koin module. The processor will generate an
implementation for it.
@Module
interface AppModule {
val module: org.koin.core.module.Module
}Attach to a class to define how it should be provided by Koin.
// Provided as a singleton
@Single
class MyRepository
// Provided as a new instance every time
@Factory
class MyPresenter(private val repo: MyRepository)
// Provided as a ViewModel
@KoinViewModel
class MyViewModel(private val presenter: MyPresenter)
// Provided within a named scope
@Scoped(scopeName = "MyActivityScope")
class MyScopedDependency// core/src/commonMain/kotlin/.../repository/MyRepository.kt
@Single
class MyRepository { ... }
// core/src/commonMain/kotlin/.../presenter/MyPresenter.kt
@Factory
class MyPresenter(private val repository: MyRepository) // core/src/commonMain/kotlin/.../di/CoreDI.kt
@Module
interface CoreDI {
val module: org.koin.core.module.Module
}
// composeApp/src/commonmain/kotlin/.../di/AppDI.kt
@Module
interface DI {
val module: org.koin.core.module.Module
}After building, the processor generates KoinModuleLoader.kt in the package you specified (
io.github.tbib.koingeneratorapp.di). Use the generated functions to start Koin.
// composeApp/src/commonMain/kotlin/.../App.kt
import io.github.tbib.koingeneratorapp.di.loadApplicationModules
import io.github.tbib.koingeneratorapp.di.loadDIModules // Specific loader for the 'DI' module
fun initKoin() {
startKoin {
// Load all modules
modules(loadApplicationModules() + loadDIModules())
}
}Koin Generator is a powerful KSP (Kotlin Symbol Processing) library that automates the creation of Koin modules for your Kotlin Multiplatform projects. By simply annotating your classes, you can eliminate boilerplate code and ensure a clean, scalable dependency injection setup.
It includes:
@Single, @Factory, @Scoped, and @KoinViewModel.@InstallIn (e.g., Application, Activity).loadApplicationModules(), loadActivityModules()) to easily
load modules where they are needed.Because this library relies on KSP to generate code, you must manually trigger the metadata generation the first time you set up the project or after adding new annotations. Run the following commands in your terminal:
./gradlew core : kspCommonMainKotlinMetadata --no - configuration - cache
./ gradlew sharedUI:kspCommonMainKotlinMetadata-- no -configuration - cacheNote: Please replace x.y.z with the latest version.
In your build.gradle.kts file for your shared module (e.g., core, shared):
// Add the annotations library
implementation("io.github.the-best-is-best:koingenerator-annotations:x.y.z")
// Add the KSP processor
ksp("io.github.the-best-is-best:koingenerator-processor:x.y.z")In the same build.gradle.kts, configure the KSP processor to specify which packages to scan and
where to place the generated loader file.
ksp {
// Required: Comma-separated list of base packages to scan for @Module annotations.
// The processor will scan these packages and all their sub-packages.
arg("packageName", "io.github.tbib.core,io.github.tbib.koingeneratorapp")
// Optional: The package where the generated 'KoinModuleLoader.kt' file will be created.
// Defaults to the package of the first module found.
arg("koinLoaderPackageName", "io.github.tbib.koingeneratorapp.di")
}Ensure the generated code is included in your source sets.
kotlin {
sourceSets.named("commonMain").configure {
kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin")
}
}Marks an interface as a definition for a Koin module. The processor will generate an
implementation for it.
@Module
interface AppModule {
val module: org.koin.core.module.Module
}Attach to a class to define how it should be provided by Koin.
// Provided as a singleton
@Single
class MyRepository
// Provided as a new instance every time
@Factory
class MyPresenter(private val repo: MyRepository)
// Provided as a ViewModel
@KoinViewModel
class MyViewModel(private val presenter: MyPresenter)
// Provided within a named scope
@Scoped(scopeName = "MyActivityScope")
class MyScopedDependency// core/src/commonMain/kotlin/.../repository/MyRepository.kt
@Single
class MyRepository { ... }
// core/src/commonMain/kotlin/.../presenter/MyPresenter.kt
@Factory
class MyPresenter(private val repository: MyRepository) // core/src/commonMain/kotlin/.../di/CoreDI.kt
@Module
interface CoreDI {
val module: org.koin.core.module.Module
}
// composeApp/src/commonmain/kotlin/.../di/AppDI.kt
@Module
interface DI {
val module: org.koin.core.module.Module
}After building, the processor generates KoinModuleLoader.kt in the package you specified (
io.github.tbib.koingeneratorapp.di). Use the generated functions to start Koin.
// composeApp/src/commonMain/kotlin/.../App.kt
import io.github.tbib.koingeneratorapp.di.loadApplicationModules
import io.github.tbib.koingeneratorapp.di.loadDIModules // Specific loader for the 'DI' module
fun initKoin() {
startKoin {
// Load all modules
modules(loadApplicationModules() + loadDIModules())
}
}