Search

PythonReactSEOPDF
GitHub Skill

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.

shoheikawano☆ 3 Repository stars0 Repository forks

Installation

Follow the repository instructions for this skill and your agent. Review the files and dependencies before adding it to your workflow.

View source ↗Guides

Skill instructions

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

  1. Determine approach - DSL or Annotations (ask if unclear)
  2. Identify feature scope - What feature/layer needs DI?
  3. Generate module code - Create appropriate Koin module
  4. Handle platform-specific - Use expect/actual for platform deps
  5. Update root module - Include new module in AppModule
  6. Configure Gradle - Ensure dependencies are correct

References

For detailed implementation patterns:

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)

Examples

Read the usage examples in the source repository.

View source ↗

Source history

Indexed on
2026-01-20
Index updated
2026-01-26
Branch
main

Index and sync dates describe this directory. View the source repository for code history.

GitHub ↗

chat Comments (0)

chat_bubble_outline

No comments yet. Be the first to share your thoughts!