Koin X KMP Cheat Sheet

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Kotlin Multiplatform (KMP)

Cheat Sheet
And implement your module, following your
Koin Dependency Injection in platform-specific needs:
Koin is the Kotlin Multiplatform (KMP)
integration framework. Shared Code JVM
You can write code once and deploy it on Koin is a pure Kotlin framework. It interacts
multiple platforms, with KMP as the main naturally with your Kotlin shared code project the
cross-platform technology, and Koin as the same way as your JVM or Android project.
Dependency Injection framework.
Define Koin modules for the needed classes to
inject into your application.
Koin offers a fully Kotlin-centric approach,
leveraging the language's features and syntax
We recommend utilizing constructor injection
to provide a simpler and more intuitive DI whenever possible.
experience. Alternatively, the KoinComponent interface can
assist in dependency injection through properties. iOS
Multiplatform Application Setup
Phyllis
Take a look at the Kotlin KMPSchwaiger
getting started guide to
help start your new Kotlin Multiplatform application

For Koin setup instructions, please refer to the Koin


Setup page.
You can use Constructor DSL as well,
Add the koin-core or koin-test Gradle with keywords such as singleOf factoryOf
Morgan Maxwell ,
dependency to your KMP project.

Koin Annotations & KMP Implementing Native Dependency Starting Common & Native Modules
To integrate Koin Annotations into your KMP project,
Modules After defining your common Koin modules and
begin by following the KSP guide from Google. implementing the necessary native modules,
It’s also possible to define a Koin module, for a
initiate dependency initialization by invoking
specific platform. Use the actual Kotlin keyword
your native module from the shared code:
After integrating the Google KSP plugin, add the to define it:
koin-annotations & koin--ksp-compiler
dependencies to your KMP setup. Detailed steps can
be found in this tutorial.
insert-koin.io
Kotlin Multiplatform (KMP)
Cheat Sheet

Injecting Dependency - Wrapper Strategy To achieve this in iOS, simply implement the code below:

One way to begin injecting Koin dependencies into your


iOS code is by creating a designated function to retrieve
your dependency. In this example, we'll define
getKMMViewModel to be used in your Swift code:
In Swift iOS, we can use it in a native component:

Injecting iOS dependencies in Koin


You may need to pass data from the iOS platform to the
Here's how you can implement this in Swift: Koin dependencies. The best is always to rely on KMP
Libraries, to have KMP API ready to be used for your
code.
Sample code for providing NSUserDefaults to the

Injecting with ViewModels and KMM- KMP Settings library is shown below:

ViewModel
You can declare a ViewModel, usage across Android
and iOS with KMM-ViewModel.
Injecting Dependency - Koin Component
Strategy For this, incorporate the KMMViewModel() class for
your ViewModel:
An alternative method involves using the
KoinComponent interface to inject properties into a
class that will be manually instantiated:

To call the Kotlin function from iOS, ensure that you use
Then, declare it in your Android module and use it in the
the appropriate native object parameter:
“classical” way, with by viewModel :

Utilizing Koin in the Android/JVM realm allows for direct


benefits such as constructor injection or the use of the
inject extensions, which enables Koin to retrieve
the necessary dependencies:

You might also like