Let's start by talking briefly about what Kotlin Multiplatform (KMP for short) is and is not and why it might be worth your time to learn it. First-off, KMP is a system where you can share code between multiple platforms. KMP currently supports JVM, Android, Native, and JS targets currently with Web Assembly on the way. This gives you a huge amount of flexibility, because you can write your common business logic in Kotlin and share it to all your target platforms.
KMP is not like React Native or Flutter. It does not share UI between platforms. Though I think this is technically possible if you where really determined, it is not the intent. KMP is more for sharing your core logic between platforms and then writing your native UI in each platform thus keeping your apps truly native and independent.
I have been writing in Kotlin Multiplatform Mobile now for a few years, and I can say that a lot has changed in that time and so many things are better. It's still not for the faint of heart, but once you put in some time and effort the productivity gains are really worth it I think. In this post I hope to give some pointers and hopefully save you a lot of time and effort. This is the post I wish I had when first starting.
First I would say to anyone who has not started in KMM, I'd start by creating a simple example app in Android Studio or IntelliJ. what ever is your preferred IDE. Both IDE's have templates, but I think Android Studio by default has a better starter for building mobile apps
Start with iOS
I would start by writing some simple code and playing with it on the iOS side, because this is going to be by far the harder platform to get used to when sharing code, unless you are super comfortable with Objective C inter-op with Swift, because that's how the native library will be treated. Take a look at this documentation for type mappings.
Start Small
Don't try to write too much too fast. Build in layers with KMM.. it's easy to get into a lot of complexity pretty fast if you are just testing and working on the JVM side and not be aware of how the API works and looks on the iOS side.
Be Aware of Caching Issues On Native Builds
The JetBrains team is very quick to fix things, so who knows how long this will be an issue, but at the time I'm writing this (using kotlin 1.6.10) I have a pretty frequent issue when making changes on the Kotlin side with an iOS app, where I will get a app crash because the change I made probably did not get compiled (that's my assumption at least). The simple solution is to delete your build file and run the code again in Xcode.
Some Libraries That Work Really Well
SQLDelight as the name implies, this library is for dealing with SQL on all platforms that KMM supports. It's somewhat easy to use and works extremely reliable, plus has a very active community.
Unofficially Koin for Dependency injection. Reference their official docs as well for implementation guidance. this is a very simple and intuitive library if you are used to DI.
Kermit The Log. This is a great logging library for KMM that is easy to use and created by Touchlab.
Korlibs This is an open source set of libraries for all sorts of useful things such as data time manipulation, or cryptography... etc. Definitely worth checking out. Also very nice kotlin APIs.
Kotlin Result a very good monad library.
Ktor is a JetBrains created library for making Kotlin web servers and clients. It has a nice API and is easy to write api requests.
Some Projects to Look at For Reference or as a Starting Point
KaMPKit This one is really good, and is kinda a best practice starting point. Touchlab is a very active contributor to the KMM community and it's definitely worth keeping an eye on their blog linked above.
ReduxKotlin There are a few ports of Redux for KMM. This one might be the best because it is thread safe, which is very nice. The only drawback that I don't love about it is that it doesn't have types for sevral aspacts that feel like they should have types. On the other-hand ReKotlin (which is a port of ReSwift) has a lot of the typing that ReduxKotlin is missing, but is missing the thread safety. It just depends on your use-case.
I have used other architectures than Redux, but I'm finding that redux is really nice on Mobile. I think it could be a good fit, but threading can be a challenge.
JetBrains just built and released this new online template wizard for easier project setup. Definitely also worth playing with.
The New Memory Model
In KMM there is a big transition happening currently. Until very recently Kotlin native (which is what we use on iOS) there was a very strict memory model that did not allow you to share mutable state with different threads. This is both a plus and minus. On the plus side this makes multithreaded programming guaranteed to not have race conditions and other bugs that come along with multithreaded code, but on the negative side it makes writing your multithreaded application difficult and requires a good amount of practice. This is not the case on the JVM side, so there is a big difference between the platforms. This is finally changing. There is an alpha memory model currently out. I would recommend playing with this new model, since going forward it will be the standard. You can do so by adding this line to your gradle.properties
file: kotlin.native.binary.memoryModel=experimental
here is a blog post discussing this change from JetBrains
The official Kotlin Blog is also a good place to be aware of and check on periodically. They cover the latest features added to both Kotlin and KMM as well as insights into where the project is going in the future.
Last resource I'll share with you in this post is the Kotlin Slack. If you are not already on there join now. The community is great and very responsive. It's a great place to share feedback and ask questions of fellow developers in the same boat.
In conclusion KMM is really exciting new technology. I think it's here to stay for the long haul. It's also a lot of fun to write. I hope you give it a shot and make your application logic shared and more efficient. KMM still has a lot of maturing to do, but the Kotlin team is extremely in-tuned with the community and I see them making massive changes on a monthly basis and they always make changes that are needed and not just fluff. And if you join the Kotlin Slack you can be a part of this change and this exciting new technology.
Top comments (0)