skills/koin-patterns/SKILL.md
Koin dependency injection patterns for Android with modules, scopes, and ViewModel injection.
npx skillsauth add ahmed3elshaer/everything-claude-code-mobile koin-patternsInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Pragmatic DI for Kotlin with Koin.
// AppModule.kt
val appModule = module {
// Singletons
single<AppDatabase> { Room.databaseBuilder(...).build() }
single { get<AppDatabase>().userDao() }
// Factories (new instance each time)
factory { DateFormatter() }
}
// NetworkModule.kt
val networkModule = module {
single<HttpClient> {
HttpClient(OkHttp) {
install(ContentNegotiation) {
json(Json { ignoreUnknownKeys = true })
}
}
}
single<AuthApi> { AuthApiImpl(get()) }
single<UserApi> { UserApiImpl(get()) }
}
// FeatureModule.kt
val homeModule = module {
// Repository
single<HomeRepository> { HomeRepositoryImpl(get(), get()) }
// Use Cases
factory { GetItemsUseCase(get()) }
factory { GetItemUseCase(get()) }
// ViewModel
viewModel { HomeViewModel(get()) }
}
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
androidContext(this@MyApp)
modules(
appModule,
networkModule,
homeModule,
detailModule
)
}
}
}
// In Compose
@Composable
fun HomeScreen(viewModel: HomeViewModel = koinViewModel()) {
val state by viewModel.state.collectAsStateWithLifecycle()
// ...
}
// With parameters
@Composable
fun DetailScreen(itemId: String) {
val viewModel: DetailViewModel = koinViewModel { parametersOf(itemId) }
// ...
}
// ViewModel definition with params
viewModel { params ->
DetailViewModel(
itemId = params.get(),
repository = get()
)
}
// Activity scope
val activityModule = module {
scope<MainActivity> {
scoped { NavigationController() }
}
}
// Usage
class MainActivity : AppCompatActivity(), KoinScopeComponent {
override val scope: Scope by activityScope()
val nav: NavigationController by inject()
}
val networkModule = module {
single(named("auth")) { createAuthClient() }
single(named("default")) { createDefaultClient() }
}
// Usage
class UserApi(
@Named("auth") private val client: HttpClient
)
class HomeViewModelTest : KoinTest {
@get:Rule
val koinRule = KoinTestRule.create {
modules(testModule)
}
private val testModule = module {
single<HomeRepository> { mockk() }
viewModel { HomeViewModel(get()) }
}
private val viewModel: HomeViewModel by inject()
private val repository: HomeRepository by inject()
@Test
fun `loads items`() = runTest {
coEvery { repository.getItems() } returns Result.success(listOf())
// ...
}
}
Remember: Koin is pragmatic. Keep modules organized, use scopes for lifecycle.
data-ai
SQLDelight patterns for Kotlin Multiplatform - .sq file definitions, platform drivers, type adapters, migrations, and shared database access.
data-ai
Room database patterns for Android - entity definitions, DAO interfaces, Database class, migrations, TypeConverters, and Flow integration.
tools
Push notification patterns - FCM setup for Android, APNs for iOS, notification channels, payload handling, foreground/background behavior, and rich notifications.
content-media
Pagination patterns for mobile - Paging 3 for Android (PagingSource, RemoteMediator, LazyPagingItems), cursor-based and offset-based strategies.