Koin dependency injection integration with Jetpack Compose for Kotlin Multiplatform development.
npx @tessl/cli install tessl/maven-io-insert-koin--koin-compose-jvm@4.1.0Koin 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.
implementation("io.insert-koin:koin-compose-jvm:4.1.1")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@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()
}
}Koin Compose is built around several key components:
@KoinExperimentalAPIremember for state management and follows Compose recomposition patternsCore 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
)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
): TFunctions to retrieve current Koin application and scope contexts from anywhere in the Compose hierarchy.
@Composable
fun getKoin(): Koin
@Composable
fun currentKoinScope(): ScopeExperimental 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): ScopeExperimental 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() }
)/** 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/** Provides Koin application context throughout Compose hierarchy */
val LocalKoinApplication: ProvidableCompositionLocal<ComposeContextWrapper<Koin>>
/** Provides Koin scope context throughout Compose hierarchy */
val LocalKoinScope: ProvidableCompositionLocal<ComposeContextWrapper<Scope>>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
}LocalContext.current.applicationContext into Koin application@KoinExperimentalAPI): Scope management, module management, multiplatform application setupKoinContext function (no longer needed with modern Compose integration)