android-di-koin
Koin dependency injection setup for Android/KMP - module definitions per layer, ViewModel injection, assembling modules in :app, and koinViewModel() in composables. Use this skill whenever setting up Koin, defining a DI module, providing a repository or ViewModel, injecting a dependency, or wiring modules in the Application class. 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".
- License 许可
- GPL-3.0
- 最近更新
- 2026-11-14
- Stars
- ★ 1.4k
- Forks
- 82
01 前言 Front Matter
- name
- android-di-koin
- description
- Koin dependency injection setup for Android/KMP - module definitions per layer, ViewModel injection, assembling modules in :app, and koinViewModel() in composables. Use this skill whenever setting up Koin, defining a DI module, providing a repository or ViewModel, injecting a dependency, or wiring modules in the Application class. 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".
03 SKILL.md 正文
Android / KMP Dependency Injection (Koin)
Principles
- One Koin module per feature layer — create it only if there are dependencies to provide.
- Modules are assembled in
:app, never in feature modules themselves. - In Compose root composables, always inject ViewModels via
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
val coreDataModule = module {
// 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()) }
single { createDataStore(get()) }
}
Assembly in :app
Register all modules in the Application class:
class App : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
androidContext(this@App)
modules(
// core
coreDataModule,
// features
notesDataModule,
notesPresentationModule,
authDataModule,
authPresentationModule,
// ...
)
}
}
}
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.
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 |
Checklist: Adding DI for a New Feature
- [ ] Define
val <feature>DataModule = module { ... }infeature:data - [ ] Define
val <feature>PresentationModule = module { ... }infeature:presentation - [ ] Register both modules in
:app'sstartKoin { modules(...) } - [ ] Use
koinViewModel()in all Root composables
04 同仓库更多 Skills
review-animations
Reviews animation and motion code against a high craft bar derived from Emil Kowalski's design engineering philosophy. Default to flagging; approval is earned.
prototype
Build multiple genuinely different versions of a UI piece you describe, rendered behind a visual picker so you can flip through them live and promote the one that feels right. Only runs when explicitly invoked; it does not trigger on its own.
pick-ui-library
Pick the right library for a given frontend task from a curated, opinionated list — numbers, OTP inputs, charts, command menus, virtualization, drag and drop, toasts, state, styling, and more. Only runs when explicitly invoked; it does not trigger on its own.
improve-animations
Survey a codebase's animation and motion code as a senior motion advisor, then produce a prioritized audit and self-contained implementation plans for other agents (or cheaper models) to execute. Read-only on source code — it plans improvements, it does not apply them. Use when the user asks to "improve the animations", "audit the motion", "make this app feel better", or wants a roadmap of animation fixes rather than a review of a single diff.
grilling
Interview the user relentlessly about a plan or design. Use when the user wants to stress-test a plan before building, or uses any 'grill' trigger phrases.
grill-with-docs
A relentless interview to sharpen a plan or design, which also creates docs (ADR's and glossary) as we go.