kmp-koin-di-creator
Generate Koin dependency injection code for Kotlin Multiplatform (KMP) projects using Koin 4.x. Use when (1) Setting up Koin DI in KMP projects, (2) Creating Koin modules for features, (3) Implementing DSL-based or Annotations-based DI, (4) Configuring platform-specific dependencies with expect/actual, (5) Adding ViewModels with Koin, (6) Structuring DI for clean architecture (UI/Domain/Data layers). Triggers: koin, dependency injection, di module, koin module, @Single, @Factory, viewModel{}, single{}, factory{}, kmp di, multiplatform di.
Installation
Suivez les instructions du dépôt pour votre agent. Consultez les fichiers et dépendances avant l’installation.
Voir la source ↗GuidesInstructions
KMP Koin DI Creator
Generate Koin 4.x dependency injection code following feature-based module organization with Android Modern Architecture (UI/Domain/Data layers).
Approach Selection
Koin 4.x offers two approaches:
| Approach | When to Use |
|---|---|
| DSL-based | Rapid prototyping, smaller projects, runtime flexibility |
| Annotations-based | Larger projects, compile-time safety, auto-discovery |
Ask the user which approach they prefer if not specified.
Quick Start
DSL-based Module
// feature/auth/di/AuthModule.kt
val authModule = module {
// Data layer
single<AuthRepository> { AuthRepositoryImpl(get()) }
single { AuthRemoteDataSource(get()) }
// Domain layer - pure Kotlin, no Koin deps in use cases
factory { LoginUseCase(get()) }
factory { LogoutUseCase(get()) }
// UI layer
viewModel { LoginViewModel(get(), get()) }
}
Annotations-based Module
// feature/auth/di/AuthModule.kt
@Module
@ComponentScan("com.example.auth")
class AuthModule
// feature/auth/data/AuthRepositoryImpl.kt
@Single
class AuthRepositoryImpl(
private val remoteDataSource: AuthRemoteDataSource
) : AuthRepository
// feature/auth/ui/LoginViewModel.kt
@KoinViewModel
class LoginViewModel(
private val loginUseCase: LoginUseCase,
private val logoutUseCase: LogoutUseCase
) : ViewModel()
Module Organization
Follow feature-based structure with clean architecture layers:
composeApp/
src/
commonMain/
kotlin/com/example/
di/
AppModule.kt # Root module aggregating features
feature/
auth/
di/AuthModule.kt # Feature DI module
data/ # Repositories, data sources
domain/ # Use cases (pure Kotlin, NO Koin)
ui/ # ViewModels, UI state
profile/
di/ProfileModule.kt
data/
domain/
ui/
androidMain/
kotlin/com/example/
di/AndroidModule.kt # Android-specific bindings
MainApplication.kt # Koin initialization
iosMain/
kotlin/com/example/
di/IosModule.kt # iOS-specific bindings
KoinHelper.kt # iOS initialization helper
Workflow
- Determine approach - DSL or Annotations (ask if unclear)
- Identify feature scope - What feature/layer needs DI?
- Generate module code - Create appropriate Koin module
- Handle platform-specific - Use expect/actual for platform deps
- Update root module - Include new module in AppModule
- Configure Gradle - Ensure dependencies are correct
References
For detailed implementation patterns:
- DSL approach: See references/dsl-patterns.md
- Annotations approach: See references/annotations-patterns.md
- Gradle configuration: See references/gradle-setup.md
Platform Initialization
Android (Application class)
class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
androidContext(this@MainApplication)
modules(appModule, androidModule)
}
}
}
iOS (Helper function)
// iosMain/KoinHelper.kt
fun initKoin() {
startKoin {
modules(appModule, iosModule)
}
}
// Call from Swift: KoinHelperKt.doInitKoin()
Best Practices
- Keep domain layer free of Koin dependencies (pure Kotlin use cases)
- Use interfaces for shared code testability
- Prefer constructor injection over field injection
- Enable compile-time verification with
KOIN_CONFIG_CHECK=true - Separate platform modules (androidModule, iosModule)
Exemples
Consultez les exemples du dépôt source.
Voir la source ↗Historique source
- Date d’indexation
- 2026-01-20
- Mise à jour de l’index
- 2026-01-26
- Branch
- main
Les dates concernent ce répertoire. Consultez le dépôt pour l’historique du code.
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!