3 min read

Multiplatform Application Development Simplified with Kotlin Multiplatform x Koin (Cheat sheet included 👇)

We share a cheat sheet for setting up a multiplatform application, using Koin for injecting dependencies into shared and platform-specific code.
Koin x Kotlin Multiplatform Cheat Sheet
Koin x Kotlin Multiplatform Cheat Sheet

Developing applications for multiple platforms can be a complex task, but with the right tools and frameworks, it becomes much more manageable. In this blog post, we'll explore how Kotlin Multiplatform (KMP) and Koin can simplify the process of building multiplatform applications.

Credit: Image from Kotlin website

And we'll share a cheat sheet👇 that covers the key concepts and techniques involved in setting up a multiplatform project, and injecting dependencies into shared and platform-specific code. We'd like to send a special shout-out to the Touchlab team 🙏 for their massive help in putting this cheat sheet together.

Kotlin Multiplatform (KMP) is a powerful framework that allows you to write shared code that can be used across multiple platforms, including Android, iOS, and even backend systems. Koin, on the other hand, is a pure Kotlin dependency injection framework that seamlessly integrates with Kotlin Multiplatform projects.

KOIN, a part of the Kotlin Multiplatform libraries ecosystem
KOIN is an integral part of the growing KMP libraries ecosystem
💡
By combining these two technologies, Koin and KMP, you can create efficient and scalable multiplatform applications with ease 🔥

Setting up a Multiplatform Project


Getting started with Kotlin Multiplatform is straightforward. The Kotlin website provides a comprehensive guide on setting up a KMP project, which covers everything from project structure to build configuration. If you're new to Kotlin Multiplatform, this guide will help you get up and running quickly.

Integrating Koin for Dependency Injection


Once your Kotlin Multiplatform project is set up, it's time to integrate Koin for dependency injection. Koin is a lightweight and easy-to-use dependency injection framework that offers seamless integration with Kotlin. To get started, you'll need to add the appropriate Koin dependencies to your project's Gradle files. The Koin website provides a setup page with the latest version information and instructions.

Defining Koin Modules


In Koin, dependencies are organized into modules. You can define Koin modules for the classes you want to inject into your application.  

Implementing Native Dependency Modules


One of the advantages of Kotlin Multiplatform is the ability to implement platform-specific code. In the cheat sheet, you'll see how to define platform-specific Koin modules for different platforms, such as JVM and iOS.

Injecting Dependencies into Shared and Platform-Specific Code


There are two strategies for injecting dependencies into shared and platform-specific code. The first strategy is the wrapper strategy, where dedicated functions are exposed in shared code to retrieve dependencies. The second strategy is using the KoinComponent interface, which allows for property injection in classes that are instantiated by hand.

Using KMM-ViewModel for Cross-Platform ViewModels


To simplify the creation of ViewModels that can be used across Android and iOS platforms, you can define KMM-ViewModel, a library designed specifically for Kotlin Multiplatform projects. The cheat sheet explains how to declare and use ViewModels with KMM-ViewModel, emphasizing the benefits of code reusability and consistency across platforms.

Injecting iOS Dependencies in Koin


You can use Kotlin Multiplatform Libraries to have Kotlin Multiplatform API readily available for iOS code. In the cheat sheet, we demonstrate how to integrate NSUserDefaults as a native dependency for iOS.

And here's the cheat sheet:

Wrapping Up


Building multiplatform applications is made simpler and more efficient by combining Kotlin Multiplatform with Koin. With Kotlin Multiplatform's ability to share code across platforms and Koin's seamless dependency injection, you can save time and effort in creating modular and scalable multiplatform applications. By following the steps outlined in the cheat sheet, you can confidently embark on your multiplatform development journey using Kotlin Multiplatform and Koin.

Don't forget to subscribe to this blog as we share regular Koin & Cloud-Inject updates  😉

P.S. Here you can find the latest Koin release note. And here is the Koin 3.4 & Koin Annotations cheat sheet.