Kotlin Multiplatform dependency injection with pure native services
Pavel Puchkov
Posted on November 29, 2023
I'm the tech lead of a mobile development team, and it's my approach to how to use dependency injection in the Kotlin Muiltiplatform project with Koin library that allow you to write pure native iOS/Swift and Android/Kotlin services.
The main new idea of this manual is that we are trying to write all native features directly in native code without using Kotlin Multiplatform native interpolation. We declare native services in iOS and Android apps and pass them to the shared code through Koin dependency injection. It allows us to use general libraries documentation with native snippets in Kotlin and Swift, and we don't have any restrictions from Kotlin Multiplatform, like not supporting Swift.
Preparing
In this manual, I will use the default Kotlin Multiplatform project that was created by Android Studio. You could find the documentation about how to create a project from scratch in the official documentation.
After creating the default project, we need to install Koin and add the necessary dependencies to shared and Android projects. For it, let's add the Koin dependencies version to gradle/libs.versions.toml.
In this manual, I will try to cover all the types of services that you will usually use in your project:
services that have only one multiplatform code, written in the shared project and do not use any native features.
services that use some native features and could be easily written in a standard Kotlin Multiplatform way by splitting the expect declaration and a few actual implementations.
services that use some native features, but they should be written in native code, separately in Swift for iOS and Kotlin for Android.
Pure shared services
Let's create a few services that don't use native features directly (they still may use native features, but only through other services). It will be two services: Logger and Greeting.
The Logger service will not depend on any other services and will just print the given string to the console.
The Greeting service will depend on two other services: Platform and Analytic. You could see that to use another service, we just need to pass their constructor parameter. It's how Koin DI works.
If our app needs some service that should use some native feature, the Kotlin documentation recommends using expect and actual declarations. It could be okay in easy cases, but it has a lot of restrictions, like a lack of support for Swift libraries and problems with documentation. But sometimes it's a good way to do native services. For example, it can be useful if you use a real multiplatform library like ktor or multiplatform-settings and need to configure it to use different engines on Android and iOS. But if you are going to use non-multiplatform libraries, I recommend using the third approach with pure native services.
Let's implement a Platform service that will use Kotlin Multiplatform native interaction. It should be pretty familiar to Kotlin Multiplatform users.
At first, we need to describe the expected service.
If you need to use some library that isn't supported by Kotlin Mulitplatform, it's better to keep this library usage pure native. With Koin DI, you could declare a library interface in the shared project and move implementations to real native iOS and Android code. It allows you to use Swift for iOS, plus it allows you to easily apply the library documentation to your code and easily update libraries without spending hours trying to understand how to communicate with native code from Kotlin Multiplatform.
In this manual, we will not install any 3rd party libraries, but let's imagine that we need to implement an Analytic service that could use any native library like Firebase Analytic or Segment.
Then, we should implement the library in the Android app code.
// androidApp/src/main/java/com/example/kmpdiexample/android/services/AnalyticImpl.ktclassAnalyticImpl(privatevallogger:Logger):Analytic{overridefunlogEvent(event:String){logger.log("Event \"$event\" sent to analytic by Android implementation")}}
Also, we should provide iOS implementations in Swift code.
// iosApp/iosApp/Services/AnalyticImpl.swiftclassAnalyticImpl:Analytic{privateletlogger:Loggerinit(logger:Logger){self.logger=logger}funclogEvent(event:String){logger.log(text:"Event \"\(event)\" sent to analytic by iOS implementation")}}
Configuring dependency injection
After creating all services and providing their code, we should add them to Koin modules to allow Koin to inject them.
First of all, we should add all services that are fully available in the shared project to Koin sharedModule Koin module.
Then we should create a function that will be run in the native app when the app is started. This function should configure our DI. This function will start Koin DI with our sharedModule and nativeModule (this module will be passed directly from native code).
Because we have services like Analytic whose implementation is not available in the shared project, we should configure them in native codes. For it, we may create a function makeNativeModule, that will expect Analytic implementation and return Koin module with this service.
The project with example how to use DI in Kotlin Multiplatform projects
Kotlin Multiplatform dependency injection with pure native services
I'm the tech lead of a mobile development team, and it's my approach to how to use dependency injection in the Kotlin Muiltiplatform project with Koin library that allow you to write pure native iOS/Swift and Android/Kotlin services.
The main new idea of this manual is that we are trying to write all native features directly in native code without using Kotlin Multiplatform native interpolation. We declare native services in iOS and Android apps and pass them to the shared code through Koin dependency injection. It allows us to use general libraries documentation with native snippets in Kotlin and Swift, and we don't have any restrictions from Kotlin Multiplatform, like not supporting Swift.
Preparing
In this manual, I will use the default Kotlin Multiplatform project that was created by Android Studio. You could find the documentation about how to create a project…