
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 and get the current PlatformContext instanceDispatchers.PlatformIO extension that will provide Dispatchers.IO on all platforms but WASM where it provides Dispatchers.Default
Additionally the initializer module allows you to initialize the PlatformContext on Android with the application context automatically via androidx.startup.Initializer.
| Module | android | iOS | windows | macOS | wasm |
|---|---|---|---|---|---|
| core | ✅ | ✅ | ✅ | ✅ | ✅ |
| initializer | ✅ | ❌ | ❌ | ❌ | ❌ |
| Dependency | Version |
|---|---|
| Kotlin | 2.3.20 |
| Jetbrains Compose | 1.9.3 |
| 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, PlatformContextProvider.get() and Dispatchers.PlatformIO everywhere in your code like following:
// imports
import com.michaelflisar.kmp.platformcontext.PlatformContext
import com.michaelflisar.kmp.platformcontext.PlatformContextProvider
import com.michaelflisar.kmp.platformcontext.PlatformIOContext
// usage
val platformContext = PlatformContextProvider.get() // 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 platformContext = PlatformContext.current()
}
// also, if needed, you can always get the default context
// on android: return null
// all other platforms: return an empty context of type PlatformContextEmpty
val context = PlatformContextProvider.getDefaultPlatformContext()
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 and get the current PlatformContext instanceDispatchers.PlatformIO extension that will provide Dispatchers.IO on all platforms but WASM where it provides Dispatchers.Default
Additionally the initializer module allows you to initialize the PlatformContext on Android with the application context automatically via androidx.startup.Initializer.
| Module | android | iOS | windows | macOS | wasm |
|---|---|---|---|---|---|
| core | ✅ | ✅ | ✅ | ✅ | ✅ |
| initializer | ✅ | ❌ | ❌ | ❌ | ❌ |
| Dependency | Version |
|---|---|
| Kotlin | 2.3.20 |
| Jetbrains Compose | 1.9.3 |
| 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, PlatformContextProvider.get() and Dispatchers.PlatformIO everywhere in your code like following:
// imports
import com.michaelflisar.kmp.platformcontext.PlatformContext
import com.michaelflisar.kmp.platformcontext.PlatformContextProvider
import com.michaelflisar.kmp.platformcontext.PlatformIOContext
// usage
val platformContext = PlatformContextProvider.get() // 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 platformContext = PlatformContext.current()
}
// also, if needed, you can always get the default context
// on android: return null
// all other platforms: return an empty context of type PlatformContextEmpty
val context = PlatformContextProvider.getDefaultPlatformContext()
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.