CtrlK
BlogDocsLog inGet started
Tessl Logo

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

Koin dependency injection integration with Jetpack Compose for Kotlin Multiplatform development.

Pending
Overview
Eval results
Files

Koin Compose

Koin Compose provides seamless integration between the Koin dependency injection framework and Jetpack Compose for Kotlin Multiplatform development. It offers Compose-aware APIs for dependency injection, application setup, scope management, and module loading with full support for Compose lifecycle management.

Package Information

  • Package Name: koin-compose-jvm
  • Package Type: maven
  • Language: Kotlin
  • Installation: implementation("io.insert-koin:koin-compose-jvm:4.1.1")
  • Platforms: Android, JVM, JavaScript, Native (iOS/macOS), WASM-JS

Core Imports

import org.koin.compose.*

For specific functionality:

import org.koin.compose.KoinApplication
import org.koin.compose.KoinIsolatedContext
import org.koin.compose.KoinApplicationPreview
import org.koin.compose.koinInject
import org.koin.compose.getKoin
import org.koin.compose.currentKoinScope
import org.koin.compose.KoinScope
import org.koin.compose.rememberKoinScope
import org.koin.compose.rememberKoinModules

Basic Usage

@Composable
fun App() {
    KoinApplication(application = {
        // Configure Koin modules
        modules(appModule)
    }) {
        // Your Compose UI
        MainScreen()
    }
}

@Composable  
fun MainScreen() {
    // Inject dependencies directly in Composables
    val repository: UserRepository = koinInject()
    val service: ApiService = koinInject(qualifier = named("api"))
    
    // Use injected dependencies
    LaunchedEffect(Unit) {
        val users = repository.getUsers()
        // ...
    }
}

// Alternative: Using isolated context for modular features
@Composable
fun FeatureModule() {
    val isolatedApp = koinApplication {
        modules(featureModule)
    }
    
    KoinIsolatedContext(context = isolatedApp) {
        FeatureContent()
    }
}

Architecture

Koin Compose is built around several key components:

  • Application Context Management: Provides Koin application and scope contexts throughout the Compose hierarchy using Composition Locals
  • Lifecycle Integration: Automatic cleanup of scopes and modules when Compose components are forgotten or abandoned
  • Multiplatform Support: Platform-specific optimizations for Android (context injection) and other platforms
  • Experimental APIs: Advanced features for scope and module management with @KoinExperimentalAPI
  • Compose State Integration: Uses remember for state management and follows Compose recomposition patterns

Capabilities

Application Setup

Core functions for setting up Koin contexts within Compose applications, including both standard and multiplatform configurations.

@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
)

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

Application Setup

Dependency Injection

Type-safe dependency resolution functions that integrate with Compose recomposition and support parameters, qualifiers, and scopes.

@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 to retrieve current Koin application and scope contexts from anywhere in the Compose hierarchy.

@Composable
fun getKoin(): Koin

@Composable
fun currentKoinScope(): Scope

Context Access

Scope Management

Experimental APIs for creating and managing Koin scopes with automatic lifecycle management tied to Compose recomposition.

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

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

@KoinExperimentalAPI
@Composable
fun KoinScope(
    scopeID: ScopeID,
    scopeQualifier: Qualifier,
    content: @Composable () -> Unit
)

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

Scope Management

Module Management

Experimental API for dynamically loading and unloading Koin modules based on Compose lifecycle events.

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

Module Management

Types

Core Types

/** Exception thrown when Koin Context is not found */
class UnknownKoinContext : Exception()

/** Parameters holder for dependency injection */
class ParametersHolder

/** Parameters definition function type */
typealias ParametersDefinition = () -> ParametersHolder

/** Scope identifier type */
typealias ScopeID = String

Composition Locals

/** Provides Koin application context throughout Compose hierarchy */
val LocalKoinApplication: ProvidableCompositionLocal<ComposeContextWrapper<Koin>>

/** Provides Koin scope context throughout Compose hierarchy */  
val LocalKoinScope: ProvidableCompositionLocal<ComposeContextWrapper<Scope>>

Error Handling

The library throws UnknownKoinContext when attempting to access Koin contexts that haven't been properly initialized through KoinApplication or related setup functions.

try {
    val koin = getKoin()
} catch (e: UnknownKoinContext) {
    // Handle missing Koin context
}

Platform-Specific Behavior

Android

  • Automatically injects LocalContext.current.applicationContext into Koin application
  • Uses Android-specific logging
  • Integrates with Android Activity/Fragment lifecycle

Other Platforms (JVM, JS, Native, WASM-JS)

  • Uses print-based logging
  • Standard Koin platform initialization
  • Full Kotlin Multiplatform compatibility

API Stability

  • Stable: Core dependency injection, application setup, context access
  • Experimental (@KoinExperimentalAPI): Scope management, module management, multiplatform application setup
  • Deprecated: KoinContext function (no longer needed with modern Compose integration)

Install with Tessl CLI

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