or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

application-setup.mdcontext-access.mddependency-injection.mdindex.mdmodule-management.mdscope-management.md
tile.json

tessl/maven-io-insert-koin--koin-compose

Jetpack Compose integration for Koin dependency injection framework providing Compose-specific APIs for dependency injection

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/io.insert-koin/koin-compose@4.1.x

To install, run

npx @tessl/cli install tessl/maven-io-insert-koin--koin-compose@4.1.0

index.mddocs/

Koin Compose

Koin Compose provides seamless integration between the Koin dependency injection framework and Jetpack Compose, offering Compose-specific APIs for dependency injection in Android Compose applications. It enables developers to inject dependencies directly into Composable functions and manage application-wide dependency injection context within Compose applications.

Package Information

  • Package Name: koin-compose
  • Package Type: maven
  • Language: Kotlin
  • Installation: implementation "io.insert-koin:koin-compose:4.1.0"
  • Platforms: Android, JVM, JavaScript, WebAssembly, Native (iOS, macOS)

Core Imports

import org.koin.compose.*
import org.koin.compose.scope.*
import org.koin.compose.module.*

Basic Usage

import androidx.compose.runtime.Composable
import org.koin.compose.KoinApplication
import org.koin.compose.koinInject
import org.koin.core.context.startKoin
import org.koin.dsl.module

// Define your dependencies
val appModule = module {
    single<UserRepository> { UserRepositoryImpl() }
    single<UserService> { UserServiceImpl(get()) }
}

// Setup Koin in your app
@Composable 
fun App() {
    KoinApplication(
        application = {
            modules(appModule)
        }
    ) {
        UserScreen()
    }
}

// Inject dependencies in Composables
@Composable
fun UserScreen() {
    val userService: UserService = koinInject()
    // Use userService...
}

Architecture

Koin Compose is built around several key components:

  • Composition Locals: Integration with Compose's CompositionLocal system for context propagation
  • Lifecycle Management: Automatic handling of Koin contexts and scopes tied to Compose lifecycle
  • Platform Abstraction: Multiplatform support with platform-specific configurations
  • Remember Integration: Performance optimization using Compose's remember system
  • Scope Integration: Support for Koin scopes with automatic cleanup

Capabilities

Application Setup

Core composable functions for setting up Koin dependency injection within Compose applications, including basic setup, multiplatform configuration, and isolated contexts.

@Composable
fun KoinApplication(
    application: KoinAppDeclaration,
    content: @Composable () -> Unit
)

@Composable
@KoinExperimentalAPI  
fun KoinMultiplatformApplication(
    config: KoinConfiguration,
    logLevel: Level = Level.INFO,
    content: @Composable () -> Unit
)

@Composable
fun KoinIsolatedContext(
    context: KoinApplication,
    content: @Composable () -> Unit
)

Application Setup

Dependency Injection

Functions for injecting dependencies into Composable functions with type safety and performance optimization through Compose's remember system.

@Composable
inline fun <reified T> koinInject(
    qualifier: Qualifier? = null,
    scope: Scope = currentKoinScope()
): T

@Composable
inline fun <reified T> koinInject(
    qualifier: Qualifier? = null,
    scope: Scope = currentKoinScope(),
    noinline parameters: ParametersDefinition
): T

@Composable
inline fun <reified T> koinInject(
    qualifier: Qualifier? = null,
    scope: Scope = currentKoinScope(),
    parametersHolder: ParametersHolder
): T

Dependency Injection

Context Access

Functions for accessing the current Koin application instance and scope within Compose functions, with automatic fallback to default contexts.

@Composable
fun getKoin(): Koin

@Composable  
fun currentKoinScope(): Scope

Context Access

Scope Management

Advanced scope management capabilities allowing creation and management of Koin scopes with automatic lifecycle handling tied to Compose composition lifecycle.

@Composable
@KoinExperimentalAPI
fun KoinScope(
    scopeDefinition: Koin.() -> Scope,
    content: @Composable () -> Unit
)

@Composable
@KoinExperimentalAPI
inline fun <reified T : Any> KoinScope(
    scopeID: ScopeID,
    content: @Composable () -> Unit
)

@Composable
@KoinExperimentalAPI  
fun rememberKoinScope(scope: Scope): Scope

Scope Management

Module Management

Dynamic module loading and unloading capabilities with integration into Compose lifecycle for automatic cleanup.

@Composable
@KoinExperimentalAPI
inline fun rememberKoinModules(
    unloadOnForgotten: Boolean? = null,
    unloadOnAbandoned: Boolean? = null, 
    unloadModules: Boolean = false,
    crossinline modules: @DisallowComposableCalls () -> List<Module> = { emptyList() }
)

Module Management

Core Types

// Composition locals for Koin context
val LocalKoinApplication: ProvidableCompositionLocal<Koin>
val LocalKoinScope: ProvidableCompositionLocal<Scope>

// Type aliases from Koin core and compose
typealias KoinAppDeclaration = KoinApplication.() -> Unit
typealias ParametersDefinition = () -> ParametersHolder
typealias ScopeID = String

// Core Koin types (from koin-core)
interface Koin {
    val scopeRegistry: ScopeRegistry
    val logger: Logger
    fun <T : Any> get(clazz: KClass<T>, qualifier: Qualifier? = null): T
    fun <T : Any> getOrCreateScope(scopeId: String, qualifier: Qualifier? = null): Scope
    fun <T : Any> createScope(scopeId: String): Scope
    inline fun <reified T : Any> getOrCreateScope(scopeId: String): Scope
}

interface Scope {
    val id: String
    val isRoot: Boolean
    val closed: Boolean
    val logger: Logger
    fun <T : Any> get(clazz: KClass<T>, qualifier: Qualifier? = null): T
    fun <T : Any> getWithParameters(clazz: KClass<T>, qualifier: Qualifier?, parameters: ParametersHolder): T
    fun close()
}

interface KoinApplication {
    val koin: Koin
}

interface KoinConfiguration

class ParametersHolder

interface Qualifier

class StringQualifier(val value: String) : Qualifier

// Qualifier factory functions (from koin-core)
fun named(name: String): Qualifier
inline fun <reified T> named(): Qualifier

interface Module

enum class Level { NONE, ERROR, INFO, DEBUG }

// Lifecycle management interfaces
interface RememberObserver {
    fun onRemembered()
    fun onForgotten() 
    fun onAbandoned()
}

// Platform abstraction
object KoinPlatform {
    fun getKoin(): Koin
    fun getKoinOrNull(): Koin?
}

Platform-Specific Features

Android

  • Automatic Android context injection via LocalContext.current
  • Android logger integration with androidLogger()
  • Application context discovery for Koin components

JVM/Desktop

  • Print logger for desktop applications
  • Standard Koin platform integration

JavaScript/WebAssembly

  • Browser and Node.js compatibility
  • Print logger for web environments

Native (iOS/macOS)

  • Native platform support
  • Print logger for native applications

Error Handling

class UnknownKoinContext : Exception()

Common error scenarios and exceptions from Koin core:

  • KoinApplicationAlreadyStartedException: When trying to start Koin when already started
  • UnknownKoinContext: When Koin context cannot be found in Compose composition
  • ClosedScopeException: When accessing a closed scope
  • NoBeanDefFoundException: When requested dependency is not defined in Koin modules
  • ScopeAlreadyCreatedException: When trying to create a scope that already exists
  • ParameterException: When required parameters are missing or incorrect
  • Configuration errors in multiplatform setup (missing Android context, logger setup failures)