Skip to content

Repository files navigation

Maven Central API Kotlin Kotlin Multiplatform

KMPPlatformContext

Platforms Android iOS Windows macOS WebAssembly

This library provides a multiplatform abstraction for platform-specific context handling and IO dispatching.

It provides the following main features:

  • a PlatformContext class that maps to android.content.Context on Android and to an empty implementation on all other platforms
  • a PlatformContextProvider that allows you to set the current PlatformContext instance via PlatformContextProvider.init(context: PlatformContext)
  • a val PlatformApplicationContext: PlatformContext that provides easy access to the current PlatformContext instance
  • a val 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.

Table of Contents

💻 Supported Platforms

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

➡️ Versions

Dependency Version
Kotlin 2.4.0
Jetbrains Compose 1.11.1
Jetbrains Compose Material3 1.9.0

🔧 Setup

Using Version Catalogs

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)
Direct Dependency Notation

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}")

🚀 Usage

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:

Option 1

Add the initializer module to your dependencies and you are done

Option 2

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
}

✨ Demo

A full demo is included inside the demo module, it shows nearly every usage with working examples.

📚 API

Check out the API documentation.

💡 Other Libraries

You can find more libraries (all multiplatform) of mine that all do work together nicely here.

Releases

Packages

Contributors

Languages