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.
安装与使用
请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。
查看来源 ↗使用指南技能说明
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)
使用示例
查看来源仓库中的使用示例与配置说明。
查看来源 ↗来源历史
- 收录时间
- 2026-01-20
- 索引更新时间
- 2026-01-26
- Branch
- main
收录与同步日期属于本站记录。源码修改历史请查看来源仓库。
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!