Getting Started with Kotlin Multiplatform Mobile
Learn how to develop cross-platform mobile applications for iOS and Android using Kotlin. #
This post provides an introduction to Kotlin Multiplatform Mobile and how to develop cross-platform mobile applications for iOS and Android using Kotlin.
Introduction #
Kotlin Multiplatform Mobile (KMM) is a technology that allows developers to share code between different platforms such as Android, iOS, and web applications. It uses Kotlin, a modern programming language that is designed to be concise, safe, and interoperable with other languages. With KMM, developers can write their business logic once and reuse it across different platforms, reducing development time and improving code consistency.
KMM is particularly useful for teams that want to build mobile applications for both Android and iOS platforms, as it allows them to avoid duplicating the code and the associated maintenance costs. It also enables developers to use the same toolset, programming language, and development practices, which can streamline the development process and lead to more consistent code quality.
In this tutorial, we will install all the necessary software to create our first example project. Then, we will create the example project and examine its contents. Finally, we will compile and run the code for both iOS and Android.
Prerequisites #
Before we can start creating our KMM project, we need to make sure that we have the necessary prerequisites installed on our system. Here is a list of prerequisites that you should have installed:
- Android Studio
- Xcode
- JDK
- Kotlin Plugin
- brew
These tools are essential for developing Android and iOS applications with Kotlin. However, there are two additional tools that I had to install:
- Kotlin Multiplatform Mobile Plugin: This plugin provides the necessary tools and libraries for KMM development. It can be installed from the JetBrains Plugin Repository or from the Android Studio Plugin Marketplace.
- KDoctor: This tool helps to diagnose and fix any issues that may arise during KMM development. It checks for common errors and provides suggestions for resolving them.
You can install the Kotlin Multiplatform Mobile plugin from the JetBrains Plugin Repository in Android Studio by going to “Android Studio → Settings → Plugins”.
Next, I installed KDoctor, but I had to install Ruby via Homebrew first.
% brew install kdoctor
...
% kdoctor
Environment diagnose (to see all details, use -v option):
[✓] Operation System
[✓] Java
[✓] Android Studio
[✓] Xcode
[✖] Cocoapods
✖ System ruby is currently used
CocoaPods is not compatible with system ruby installation on Apple M1 computers.
Please install ruby via Homebrew, rvm, rbenv or other tool and make it default
Detailed information: https://stackoverflow.com/questions/64901180/how-to-run-cocoapods-on-apple-silicon-m1/66556339#66556339
✖ CocoaPods requires your terminal to be using UTF-8 encoding.
Consider adding the following to ~/.zprofile
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
Conclusion:
✖ KDoctor has diagnosed one or more problems while checking your environment.
Please check the output for problem description and possible solutions.
Here’s how I installed Ruby using Homebrew.
% which ruby
/usr/bin/ruby
% brew install ruby
...
% echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc
After that, I had to log out and then log in again for the changes to take effect.
% which ruby
/opt/homebrew/opt/ruby/bin/ruby
Let’s check again if KDoctor is now working.
% kdoctor
Environment diagnose (to see all details, use -v option):
[✓] Operation System
[✓] Java
[✓] Android Studio
[✓] Xcode
[✓] Cocoapods
Conclusion:
✓ Your system is ready for Kotlin Multiplatform Mobile Development!
Finally, good to go!
Create Project #
To create the first project, navigate to “File → New → New Project …” and choose “Kotlin Multiplatform App.”
In the following dialogue, we will provide the usual information related to the project.
I named it “KMA,” but it should have been “KMM.” Anyway…
In the next dialogue, I have left everything as it was.
Once you press “Finish”, everything is ready to be compiled and run.
I have selected both targets in sequence.
While I have expected the Android Simulator to just open as usual in Android Studio, I was totally stoked to be able to start the app in the iPhone Simulator as well through Android Studio.
I was expecting to see the iOS code in Android Studio, but it wasn’t displayed. So, I opened the directory and checked it myself.
Basically, it’s all there. Let’s open both IDEs side-by-side.
Now, everything is there that we need, and we could basically start with the project.
But I have so many questions swirling through my head:
- If I change Kotlin code in Android Studio and Swift code in Xcode, can I simply compile and run everything again through Android Studio and/or Xcode?
- Do I need to develop the UI for iOS separately, or can I develop the UI for both platforms in Jetpack Compose?
- How can I use platform-specific frameworks?
We will address these questions one by one in future posts.
Here are two example apps that look quite promising:
Thank you for reading!
- If you enjoyed this, please follow me on Medium.
- Buy me a coffee or send me a tip.
- Support me and other Medium writers by signing up here.
https://twissmueller.medium.com/membership