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

application-setup.mddocs/

Application Setup

Core functions for setting up Koin contexts within Compose applications, providing both standard and multiplatform configurations with automatic platform-specific optimizations.

Capabilities

KoinApplication

Start a new Koin Application context and setup Compose context for dependency injection throughout the hierarchy.

/**
 * Start a new Koin Application context and setup Compose context
 * @param application Koin Application declaration lambda
 * @param content Following compose function
 * @throws KoinApplicationAlreadyStartedException if default context already set
 */
@Composable
fun KoinApplication(
    application: KoinAppDeclaration,
    content: @Composable () -> Unit
)

Usage Examples:

@Composable
fun App() {
    KoinApplication(application = {
        // Standard Koin configuration
        modules(
            appModule,
            networkModule,
            databaseModule
        )
        
        // Optional: Custom logger
        logger(PrintLogger(Level.DEBUG))
        
        // Optional: Properties
        properties(mapOf("api.url" to "https://api.example.com"))
    }) {
        // Your entire Compose UI hierarchy
        NavHost(navController = rememberNavController()) {
            // Navigation setup
        }
    }
}

KoinMultiplatformApplication

Start Koin application with multiplatform configuration that automatically handles Android context and logger setup based on the target platform.

/**
 * Start Koin application with multiplatform configuration
 * Handles Android context/logger automatically
 * @param config Koin Application Configuration
 * @param logLevel Logger level (default: INFO)
 * @param content Following compose function
 * @throws KoinApplicationAlreadyStartedException
 */
@Composable
@KoinExperimentalAPI
fun KoinMultiplatformApplication(
    config: KoinConfiguration,
    logLevel: Level = Level.INFO,
    content: @Composable () -> Unit
)

Usage Examples:

@Composable
fun MultiplatformApp() {
    KoinMultiplatformApplication(
        config = koinConfiguration {
            modules(sharedModule, platformModule)
        },
        logLevel = Level.DEBUG
    ) {
        SharedContent()
    }
}

KoinIsolatedContext

Provides isolated Koin context via CompositionLocalProvider without starting a global Koin application.

/**
 * Provides isolated Koin context via CompositionLocalProvider
 * @param context Koin isolated context created with koinApplication()
 * @param content Child composable
 */
@Composable
fun KoinIsolatedContext(
    context: KoinApplication,
    content: @Composable () -> Unit
)

Usage Examples:

@Composable
fun IsolatedFeature() {
    val isolatedContext = koinApplication {
        modules(featureModule)
    }
    
    KoinIsolatedContext(context = isolatedContext) {
        FeatureContent()
    }
}

KoinApplicationPreview

Lightweight Koin application specifically designed for Compose previews, allowing parallel recomposition without conflicts.

/**
 * Lightweight Koin application for Compose previews
 * Allows parallel recomposition
 * @param application Koin application config
 * @param content Compose content
 */
@Composable
fun KoinApplicationPreview(
    application: KoinAppDeclaration,
    content: @Composable () -> Unit
)

Usage Examples:

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

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

KoinContext (Deprecated)

Use Compose with existing Koin context. This function is deprecated as Compose Koin context is now automatically set up with other functions.

/**
 * Use Compose with existing Koin context
 * @deprecated KoinContext is not needed anymore. This can be removed. 
 *            Compose Koin context is setup with StartKoin()
 */
@Composable
@Deprecated("KoinContext is not needed anymore. This can be removed. Compose Koin context is setup with StartKoin()")
fun KoinContext(
    koin: Koin = retrieveDefaultInstance(),
    content: @Composable () -> Unit
)

Platform-Specific Behavior

Android Platform

  • Automatic Context Injection: Uses LocalContext.current.applicationContext to provide Android context to Koin
  • Android Logger: Automatically configures Android-specific logging
  • Lifecycle Integration: Integrates with Android Activity/Fragment lifecycle management

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

  • Print Logger: Uses standard console/print logging
  • Standard Initialization: Uses KoinPlatform.getKoin() for context access
  • Cross-Platform Compatibility: Full Kotlin Multiplatform support

Error Handling

All application setup functions may throw KoinApplicationAlreadyStartedException if attempting to start Koin when a default context is already set. This typically occurs when:

  • Calling KoinApplication multiple times at the root level
  • Mixing startKoin with KoinApplication
  • Nesting application setup functions incorrectly
@Composable
fun SafeApp() {
    try {
        KoinApplication(application = { modules(appModule) }) {
            Content()
        }
    } catch (e: KoinApplicationAlreadyStartedException) {
        // Handle multiple initialization attempts
        Content() // Use existing Koin context
    }
}

Best Practices

  1. Single Application Setup: Use only one application setup function at your app's root level
  2. Preview Support: Use KoinApplicationPreview for Compose previews to avoid conflicts
  3. Multiplatform Projects: Prefer KoinMultiplatformApplication for automatic platform handling
  4. Isolated Features: Use KoinIsolatedContext for self-contained feature modules
  5. Testing: Create separate test modules for preview and testing scenarios

Install with Tessl CLI

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

docs

application-setup.md

context-access.md

dependency-injection.md

index.md

module-management.md

scope-management.md

tile.json