agentleFS
PL-Coding-GmbHPL-Coding-GmbH/Skills/skills/android-di-koin/SKILL.md

android-di-koin

Skill18 starsChanged 4 months agoMITView on GitHub

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".

2 other projects use a version of this, 2 of them edited.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Always use koinViewModel() in Root composables:

    line 85
  2. Distinctive:

    Never pass ViewModels down the composable tree — inject at the Root level only.

    line 95
  3. Very specific:

    Use the *Of constructor-reference form by default.

    line 107

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
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

ScopePreferred formFallback formWhen to use
SingletonsingleOf(::Impl) { bind<Interface>() }single { ... }One instance for the app lifetime (repositories, HttpClient, DB)
ViewModelviewModelOf(::MyViewModel)viewModel { ... }ViewModel instances scoped to their lifecycle
FactoryfactoryOf(::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

ThingConventionExample
Koin module<feature><Layer>ModulenotesDataModule, notesPresentationModule

Worker DI

For WorkManager Worker injection, see android-background skill — covers KoinWorkerFactory setup, disabling the default initializer, and the worker { } scope.


Checklist: Adding DI for a New Feature

  • Define val <feature>DataModule = module { ... } in feature:data
  • Define val <feature>PresentationModule = module { ... } in feature:presentation
  • Register both modules in :app's startKoin { modules(...) }
  • Use koinViewModel() in all Root composables

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.