kmp-di-koin
Skill18 starsChanged 5 months agoMITView on GitHub
Koin dependency injection setup for Kotlin Multiplatform (Android + iOS) - module definitions per layer, ViewModel injection, assembling modules in :composeApp, platform modules for Android/iOS, and koinViewModel() in composables. Use this skill whenever setting up Koin, defining a DI module, providing a repository or ViewModel, injecting a dependency, or starting Koin from the Android Application class and the iOS entry point. Trigger on phrases like "set up Koin", "add a Koin module", "inject a dependency", "DI module", "koinViewModel", "provide a ViewModel", "startKoin", or "single/viewModel/factory".
In this repository67 other files it gives its agents.
- agent-onboardingSkill
- android-backgroundSkill
- android-cliSkill
- android-compose-architectureSkill
- android-compose-componentsSkill
- android-data-layerSkill
- android-di-koinSkill
- android-error-handlingSkill
- android-integration-testingSkill
- android-jacoco-setupSkill
- android-jvm-test-coverageSkill
- android-module-structureSkill
- android-navigationSkill
- android-presentation-mviSkill
- android-preview-figma-verifySkill
- android-ui-screenshot-verifySkill
- android-ui-testingSkill
- android-ui-verifySkill
- android-unit-testingSkill
- android-version-catalogSkill
- architecture-decision-recordSkill
- careful-planningSkill
- compose-adaptive-layoutsSkill
- figma-to-android-vectorSkill
- finalizeSkill
- git-commitSkill
- handoffSkill
- kmp-compose-architectureSkill
- kmp-compose-componentsSkill
- kmp-data-layerSkill
- kmp-error-handlingSkill
- kmp-integration-testingSkill
- kmp-migration-analyzerSkill
- kmp-module-structureSkill
- kmp-navigationSkill
- kmp-presentation-mviSkill
- kmp-ui-testingSkill
- kmp-unit-testingSkill
- kotlin-coroutinesSkill
- kotlin-flowsSkill
- kotlin-mutation-testingSkill
- openspec-artifact-review-lightSkill
- openspec-artifact-reviewSkill
- project-breakdownSkill
- react-data-layerSkill
- react-error-handlingSkill
- react-integration-testingSkill
- react-presentation-mviSkill
- react-project-structureSkill
- react-routingSkill
- react-screen-architectureSkill
- react-securitySkill
- react-server-stateSkill
- react-ui-componentsSkill
- react-unit-testingSkill
- speckit-parallelize-agent-teamsSkill
- speckit-parallelize-subagentsSkill
- spring-api-layerSkill
- spring-di-configurationSkill
- spring-integration-testingSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
5 rules. About 0 are common, 2 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 98
Only create a platform module when a module actually has platform-specific dependencies.
- Very specific: line 140
Start Koin in the init of the SwiftUI App struct, so it runs once at app launch before any view controller exists:
- Very specific: line 168
Always use koinViewModel() in Root composables:
- Distinctive: line 178
Never pass ViewModels down the composable tree — inject at the Root level only.
- Very specific: line 192
Use the *Of constructor-reference form by default.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
KMP Dependency Injection (Koin)
Principles
- One Koin module per feature layer — create it only if there are dependencies to provide.
- Modules are assembled in
:composeApp(the shared umbrella module), never in feature modules themselves. - Modules are defined in
commonMain. Dependencies that only exist per platform (Ktor engine, Room builder, DataStore path, anything needingContext) go into anexpect/actualplatform module. - In Compose root composables, always inject ViewModels via
koinViewModel().
Dependencies
| Source set | Artifact | Provides |
commonMain | io.insert-koin:koin-core | module, singleOf, factoryOf, bind, startKoin |
commonMain (presentation) | io.insert-koin:koin-compose | koinInject() |
commonMain (presentation) | io.insert-koin:koin-compose-viewmodel | viewModelOf, koinViewModel() |
androidMain | io.insert-koin:koin-android | androidContext(), androidLogger() |
Imports in common code — never the org.koin.androidx.* variants:
import org.koin.core.module.dsl.singleOf
import org.koin.core.module.dsl.factoryOf
import org.koin.core.module.dsl.bind
import org.koin.core.module.dsl.viewModelOf // viewModelOf / viewModel { }
import org.koin.compose.viewmodel.koinViewModel // koinViewModel()Module Definitions
Prefer the constructor-reference overloads (singleOf, viewModelOf, factoryOf) — they are more concise and let Koin resolve parameters automatically. Only fall back to the lambda overloads (single { ... }, viewModel { ... }, factory { ... }) when constructor injection alone is not enough, e.g. when you need to call a factory method, pass a named/qualified dependency, or do post-construction setup.
Data layer module
// feature:notes:data
val notesDataModule = module {
singleOf(::RoomNoteRepository) { bind<NoteRepository>() }
}Presentation layer module
// feature:notes:presentation
val notesPresentationModule = module {
viewModelOf(::NoteListViewModel)
viewModelOf(::NoteDetailViewModel)
}Core data module (example)
// core:data — commonMain
val coreDataModule = module {
includes(coreDataPlatformModule)
// singleOf works when the class has a single injectable constructor
singleOf(::SessionPreferences)
// Lambda overload needed here — calling a factory method, not a constructor
single { HttpClientFactory.create(get()) }
}Platform module (expect/actual)
Anything that is constructed differently per platform is provided by a platform module that the common module includes:
// core:data — commonMain
expect val coreDataPlatformModule: Module
// core:data — androidMain
actual val coreDataPlatformModule = module {
single<HttpClientEngine> { OkHttp.create() }
single { createDataStore(androidContext()) }
}
// core:data — iosMain
actual val coreDataPlatformModule = module {
single<HttpClientEngine> { Darwin.create() }
single { createDataStore() }
}Only create a platform module when a module actually has platform-specific dependencies.
Assembly in :composeApp
Register all modules in a shared initKoin function in :composeApp's commonMain. Each platform calls it exactly once at startup:
// :composeApp — commonMain
fun initKoin(config: KoinAppDeclaration? = null) {
startKoin {
config?.invoke(this)
modules(
// core
coreDataModule,
// features
notesDataModule,
notesPresentationModule,
authDataModule,
authPresentationModule,
// ...
)
}
}Android (:androidApp)
class App : Application() {
override fun onCreate() {
super.onCreate()
initKoin {
androidContext(this@App)
}
}
}iOS (iosApp/iOSApp.swift)
Start Koin in the init of the SwiftUI App struct, so it runs once at app launch before any view controller exists:
import SwiftUI
import ComposeApp
@main
struct iOSApp: App {
init() {
InitKoinKt.doInitKoin(config: nil)
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}Kotlin/Native exports a top-level function from InitKoin.kt on the InitKoinKt class and prefixes names that start with init with do, so initKoin becomes doInitKoin. Swift doesn't see Kotlin default arguments, so pass config: nil explicitly.
MainViewController() in iosMain stays a plain ComposeUIViewController { App() }. Don't start Koin there with configure: the view controller can be created more than once, and a second startKoin throws. Start Koin in exactly one place per platform.
Injecting in Composables
Always use koinViewModel() in Root composables:
@Composable
fun NoteListRoot(
onNavigateToDetail: (String) -> Unit,
viewModel: NoteListViewModel = koinViewModel()
) { ... }Never pass ViewModels down the composable tree — inject at the Root level only.
koinViewModel() comes from org.koin.compose.viewmodel and works on both platforms. A SavedStateHandle constructor parameter is resolved automatically by viewModelOf.
Scoping Rules
| Scope | Preferred form | Fallback form | When to use |
| Singleton | singleOf(::Impl) { bind<Interface>() } | single { ... } | One instance for the app lifetime (repositories, HttpClient, DB) |
| ViewModel | viewModelOf(::MyViewModel) | viewModel { ... } | ViewModel instances scoped to their lifecycle |
| Factory | factoryOf(::Impl) | factory { ... } | New instance on every injection (rare — prefer singleton or ViewModel) |
Use the *Of constructor-reference form by default. Only use the lambda form when you cannot express the binding with a constructor reference (factory methods, named qualifiers, manual setup).
Naming Conventions
| Thing | Convention | Example |
| Koin module | <feature><Layer>Module | notesDataModule, notesPresentationModule |
Worker DI
WorkManager is Android-only. Worker bindings (worker { } scope, KoinWorkerFactory) go into an androidMain platform module, never into a common module; the iOS counterpart (e.g. BGTaskScheduler) is wired in the iosMain platform module behind the same common interface. For the Android side, see the android-background skill.
Checklist: Adding DI for a New Feature
- Define
val <feature>DataModule = module { ... }infeature:data - Define
val <feature>PresentationModule = module { ... }infeature:presentation - Add an
expect/actualplatform module if the feature has platform-specific dependencies — with actuals inandroidMainandiosMain - Register the modules in
:composeApp'sinitKoin { modules(...) } - Use
koinViewModel()in all Root composables
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

