
Abstraction for platform-specific context handling and IO dispatching, offering PlatformContext, a global provider for current context, a PlatformIO dispatcher, and optional automatic initializer.
This library provides a multiplatform abstraction for platform-specific context handling and IO dispatching.
It provides the following main features:
PlatformContext class that maps to android.content.Context on Android and to an empty implementation on all other platformsPlatformContextProvider that allows you to set the current PlatformContext instance via PlatformContextProvider.init(context: PlatformContext)
val PlatformApplicationContext: PlatformContext that provides easy access to the current PlatformContext instanceval platformIO: CoroutineDispatcher that provides a platform-specific IO dispatcher (e.g., Dispatchers.IO on all platforms but wasm, Dispatchers.Default on wasm platforms)Additionally the initializer module allows you to initialize the PlatformContext on Android with the application context automatically via androidx.startup.Initializer.
Simply add the initializer module to your android app and the PlatformContext will be initialized with the application context automatically.
| Module | android | iOS | windows | macOS | wasm | Notes |
|---|---|---|---|---|---|---|
| core | ✅ | ✅ | ✅ | ✅ | ✅ | This is the core module that holds the android app context |
| initializer | ✅ | ❌ | ❌ | ❌ | ❌ | Uses androidx.startup to automatically initialize the android context on app start |
| Dependency | Version |
|---|---|
| Kotlin | 2.4.0 |
| Jetbrains Compose | 1.11.1 |
| Jetbrains Compose Material3 | 1.9.0 |
Define the dependencies inside your libs.versions.toml file.
[versions]
kmpplatformcontext = "<LATEST-VERSION>"
[libraries]
kmpplatformcontext-core = { module = "io.github.mflisar.kmpplatformcontext:core", version.ref = "kmpplatformcontext" }
kmpplatformcontext-initializer = { module = "io.github.mflisar.kmpplatformcontext:initializer", version.ref = "kmpplatformcontext" }And then use the definitions in your projects build.gradle.kts file like following:
implementation(libs.kmpplatformcontext.core)
implementation(libs.kmpplatformcontext.initializer)Simply add the dependencies inside your build.gradle.kts file.
val kmpplatformcontext = "<LATEST-VERSION>"
implementation("io.github.mflisar.kmpplatformcontext:core:${kmpplatformcontext}")
implementation("io.github.mflisar.kmpplatformcontext:initializer:${kmpplatformcontext}")This library is used like following:
On all platforms but android, you only need to add the core module to your dependencies and you are done
On android you have 2 options:
Add the initializer module to your dependencies and you are done
Manually initialize the library inside your Application class like following:
PlatformContextProvider.init(this)Afterwards you have access to the PlatformContext and Dispatchers.PlatformIO everywhere in your code like following:
// imports
import com.michaelflisar.kmp.platformcontext.PlatformIO
import com.michaelflisar.kmp.platformcontext.platformContext
import kotlinx.coroutines.Dispatchers
// usage
val context = ApplicationContext // application context on android, empty context instance on all other platforms
val ioContext = Dispatchers.PlatformIO // Dispatchers.IO on all platforms but WASM where it provides Dispatchers.Default
// or use the PlatformContext class for functions (no need for the widely used doSomething(context: Any?) pattern)
expect fun doSomething()
actual fun doSomething() {
// on android use this, on other platforms this is just a no-op
val context = platformContext
}
A full demo is included inside the demo module, it shows nearly every usage with working examples.
Check out the API documentation.
You can find more libraries (all multiplatform) of mine that all do work together nicely here.
This library provides a multiplatform abstraction for platform-specific context handling and IO dispatching.
It provides the following main features:
PlatformContext class that maps to android.content.Context on Android and to an empty implementation on all other platformsPlatformContextProvider that allows you to set the current PlatformContext instance via PlatformContextProvider.init(context: PlatformContext)
val PlatformApplicationContext: PlatformContext that provides easy access to the current PlatformContext instanceval platformIO: CoroutineDispatcher that provides a platform-specific IO dispatcher (e.g., Dispatchers.IO on all platforms but wasm, Dispatchers.Default on wasm platforms)Additionally the initializer module allows you to initialize the PlatformContext on Android with the application context automatically via androidx.startup.Initializer.
Simply add the initializer module to your android app and the PlatformContext will be initialized with the application context automatically.
| Module | android | iOS | windows | macOS | wasm | Notes |
|---|---|---|---|---|---|---|
| core | ✅ | ✅ | ✅ | ✅ | ✅ | This is the core module that holds the android app context |
| initializer | ✅ | ❌ | ❌ | ❌ | ❌ | Uses androidx.startup to automatically initialize the android context on app start |
| Dependency | Version |
|---|---|
| Kotlin | 2.4.0 |
| Jetbrains Compose | 1.11.1 |
| Jetbrains Compose Material3 | 1.9.0 |
Define the dependencies inside your libs.versions.toml file.
[versions]
kmpplatformcontext = "<LATEST-VERSION>"
[libraries]
kmpplatformcontext-core = { module = "io.github.mflisar.kmpplatformcontext:core", version.ref = "kmpplatformcontext" }
kmpplatformcontext-initializer = { module = "io.github.mflisar.kmpplatformcontext:initializer", version.ref = "kmpplatformcontext" }And then use the definitions in your projects build.gradle.kts file like following:
implementation(libs.kmpplatformcontext.core)
implementation(libs.kmpplatformcontext.initializer)Simply add the dependencies inside your build.gradle.kts file.
val kmpplatformcontext = "<LATEST-VERSION>"
implementation("io.github.mflisar.kmpplatformcontext:core:${kmpplatformcontext}")
implementation("io.github.mflisar.kmpplatformcontext:initializer:${kmpplatformcontext}")This library is used like following:
On all platforms but android, you only need to add the core module to your dependencies and you are done
On android you have 2 options:
Add the initializer module to your dependencies and you are done
Manually initialize the library inside your Application class like following:
PlatformContextProvider.init(this)Afterwards you have access to the PlatformContext and Dispatchers.PlatformIO everywhere in your code like following:
// imports
import com.michaelflisar.kmp.platformcontext.PlatformIO
import com.michaelflisar.kmp.platformcontext.platformContext
import kotlinx.coroutines.Dispatchers
// usage
val context = ApplicationContext // application context on android, empty context instance on all other platforms
val ioContext = Dispatchers.PlatformIO // Dispatchers.IO on all platforms but WASM where it provides Dispatchers.Default
// or use the PlatformContext class for functions (no need for the widely used doSomething(context: Any?) pattern)
expect fun doSomething()
actual fun doSomething() {
// on android use this, on other platforms this is just a no-op
val context = platformContext
}
A full demo is included inside the demo module, it shows nearly every usage with working examples.
Check out the API documentation.
You can find more libraries (all multiplatform) of mine that all do work together nicely here.