CtrlK
BlogDocsLog inGet started
Tessl Logo

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

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

Pending
Overview
Eval results
Files

application-setup.mddocs/

Application Setup

Core composable functions for setting up Koin dependency injection within Compose applications, providing various setup approaches from basic configuration to multiplatform and isolated contexts.

Capabilities

KoinApplication

Start a new Koin Application context and setup Compose context integration. Throws an error if Koin's default context is already set.

/**
 * Start a new Koin Application context and setup Compose context
 * if Koin's Default Context is already set, throw an error
 * 
 * @param application - Koin Application declaration lambda
 * @param content - following compose function
 * @throws KoinApplicationAlreadyStartedException
 */
@Composable
fun KoinApplication(
    application: KoinAppDeclaration,
    content: @Composable () -> Unit
)

Usage Examples:

import org.koin.compose.KoinApplication
import org.koin.dsl.module

val appModule = module {
    single<ApiService> { ApiServiceImpl() }
    single<Repository> { RepositoryImpl(get()) }
}

@Composable
fun App() {
    KoinApplication(
        application = {
            modules(appModule)
            // Additional Koin configuration
        }
    ) {
        MainContent()
    }
}

KoinMultiplatformApplication

Start a new Koin Application context with automatic multiplatform configuration. Handles platform-specific context binding (Android) and logger setup automatically.

/**
 * Start a new Koin Application context, configure default context binding (android) & logger, setup Compose context
 * if Koin's Default Context is already set, throw an error
 *
 * Call composeMultiplatformConfiguration to help prepare/anticipate context setup, and avoid to have different configuration in KMP app
 * this function takes care to setup Android context (androidContext, androidLogger) for you
 * 
 * @param config - Koin Application Configuration (use koinConfiguration { } to declare your Koin application)
 * @param logLevel - KMP active logger (androidLogger or printLogger)
 * @param content - following compose function
 * @throws KoinApplicationAlreadyStartedException
 */
@Composable
@KoinExperimentalAPI
fun KoinMultiplatformApplication(
    config: KoinConfiguration,
    logLevel: Level = Level.INFO,
    content: @Composable () -> Unit
)

Usage Examples:

import org.koin.compose.KoinMultiplatformApplication
import org.koin.core.logger.Level
import org.koin.dsl.koinConfiguration

@Composable
fun App() {
    KoinMultiplatformApplication(
        config = koinConfiguration {
            modules(appModule)
        },
        logLevel = Level.DEBUG
    ) {
        MainContent()
    }
}

KoinIsolatedContext

Provides an isolated Koin context that can be used independently of the global Koin instance. Useful for testing or when multiple Koin contexts are needed.

/**
 * Provides Koin Isolated context to be setup into LocalKoinApplication & LocalKoinScope via CompositionLocalProvider,
 * to be used by child Composable.
 *
 * This allows to use an isolated context, directly in all current Composable API
 *
 * Koin isolated context has to created with koinApplication() function, storing the instance in a static field
 *
 * @param context - Koin isolated context
 * @param content - child Composable
 */
@Composable
fun KoinIsolatedContext(
    context: KoinApplication,
    content: @Composable () -> Unit
)

Usage Examples:

import org.koin.compose.KoinIsolatedContext
import org.koin.dsl.koinApplication

// Create isolated Koin application
val isolatedKoin = koinApplication {
    modules(testModule)
}

@Composable
fun TestComponent() {
    KoinIsolatedContext(
        context = isolatedKoin
    ) {
        // This content uses the isolated Koin context
        ComponentUsingKoin()
    }
}

KoinApplicationPreview

Lightweight Koin application setup specifically designed for Compose previews. This function is lighter than KoinApplication and allows parallel recomposition in Android Studio.

/**
 * Composable Function to run a local Koin application and to help run Compose preview
 * This function is lighter than KoinApplication, and allow parallel recomposition in Android Studio
 *
 * @param application - Koin application config
 * @param content
 */
@Composable
fun KoinApplicationPreview(
    application: KoinAppDeclaration,
    content: @Composable () -> Unit
)

Usage Examples:

import androidx.compose.ui.tooling.preview.Preview
import org.koin.compose.KoinApplicationPreview
import org.koin.dsl.module

val previewModule = module {
    single<ApiService> { MockApiService() }
}

@Preview
@Composable
fun UserScreenPreview() {
    KoinApplicationPreview(
        application = {
            modules(previewModule)
        }
    ) {
        UserScreen()
    }
}

KoinContext (Deprecated)

Legacy function for using existing Koin context. This function is deprecated and should be replaced with proper Koin setup.

/**
 * Use Compose with current existing Koin context, by default 'KoinPlatform.getKoin()'
 *
 * @param content - following compose function
 */
@Composable
@Deprecated("KoinContext is not needed anymore. This can be removed. Compose Koin context is setup with StartKoin()")
fun KoinContext(
    context: Koin = retrieveDefaultInstance(),
    content: @Composable () -> Unit
)

Platform-Specific Configuration

Android Platform

Automatically configures Android context and logger:

@Composable
internal actual fun composeMultiplatformConfiguration(
    loggerLevel: Level, 
    config: KoinConfiguration
): KoinConfiguration {
    val appContext = LocalContext.current.applicationContext
    return koinConfiguration {
        androidContext(appContext)
        androidLogger(loggerLevel)
        includes(config)
    }
}

JVM Platform

Uses print logger for desktop applications:

@Composable
internal actual fun composeMultiplatformConfiguration(
    loggerLevel: Level,
    config: KoinConfiguration  
): KoinConfiguration {
    return koinConfiguration {
        printLogger(loggerLevel)
        includes(config)
    }
}

JavaScript/WebAssembly/Native

Similar to JVM, uses print logger for web and native environments.

Configuration Types

// From koin-core
interface KoinConfiguration {
    fun modules(vararg modules: Module): KoinConfiguration
    fun modules(modules: List<Module>): KoinConfiguration
    // Platform-specific extensions added automatically
}

// From koin-dsl
typealias KoinAppDeclaration = KoinApplication.() -> Unit

// Logger levels
enum class Level {
    NONE, ERROR, INFO, DEBUG
}

Error Handling

  • KoinApplicationAlreadyStartedException: Thrown when trying to start Koin when it's already started
  • Configuration errors: Platform-specific setup failures (e.g., missing Android context)
  • Module loading errors: Issues with dependency resolution during module loading

Install with Tessl CLI

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

docs

application-setup.md

context-access.md

dependency-injection.md

index.md

module-management.md

scope-management.md

tile.json