Jetpack Compose integration for Koin dependency injection framework providing Compose-specific APIs for dependency injection
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
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.
implementation "io.insert-koin:koin-compose:4.1.0"import org.koin.compose.*
import org.koin.compose.scope.*
import org.koin.compose.module.*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...
}Koin Compose is built around several key components:
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
)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
): TFunctions 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(): ScopeAdvanced 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): ScopeDynamic 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() }
)// 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?
}LocalContext.currentandroidLogger()class UnknownKoinContext : Exception()Common error scenarios and exceptions from Koin core:
KoinApplicationAlreadyStartedException: When trying to start Koin when already startedUnknownKoinContext: When Koin context cannot be found in Compose compositionClosedScopeException: When accessing a closed scopeNoBeanDefFoundException: When requested dependency is not defined in Koin modulesScopeAlreadyCreatedException: When trying to create a scope that already existsParameterException: When required parameters are missing or incorrect