Kotlin Multiplatform (KMP) offers a powerful way to write shared code across multiple platforms while still giving you the flexibility to define platform-specific implementations. A key part of achieving this is using the expect/actual mechanism, which allows you to define common APIs in your shared module and provide platform-specific implementations in the respective targets.
This approach is particularly handy when building Compose Multiplatform apps, where you might need to perform native system interactions like opening a URL in the browser or sharing text with the native operating system mechanism.
In this blog post, we'll explore how to set up and use the expect/actual pattern to create a SystemService that handles these interactions seamlessly across platforms and uses a Koin injection for dependency injection. By the end, you'll see how this technique keeps your codebase clean and efficient while enabling platform-specific capabilities.
The interface
A pure interface that defines what interactions we need with the platform.
We use Koin for dependency injection and we define a module that needs to be implemented in each platform (expect).
The implementations
We implement the functionality on each of the supported platforms, here Android and iOS, and provide an actual implementation for the Koin module that instantiates the SystemService implementation for each platform.
Usage
Use the factorySystemService() in a Koin module and inject SystemService. You will get the correct implementation for each platform to perform your actions.