Ramp Me Up, Scotty!

EN | DE

Getting Started with Kotlin Multiplatform Mobile

2023-04-12

Learn how to develop cross-platform mobile applications for iOS and Android using Kotlin.

This post provides an introduction to Kotlin Multiplatform Mobile and explains 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 designed to be concise, safe, and interoperable with other languages. With KMM, developers can write business logic once and reuse it across 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, as it helps avoid duplicated 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 the necessary software to create our first example project. Then we will create the project and examine its contents. Finally, we will compile and run the code for both iOS and Android.

Prerequisites

Before creating a KMM project, make sure the following prerequisites are installed on your system:

These tools are essential for developing Android and iOS applications with Kotlin. Additionally, I installed two more tools:

You can install the Kotlin Multiplatform Mobile plugin in Android Studio via “Android Studio → Settings → Plugins”.

Next, I installed KDoctor, but first I had to install Ruby via Homebrew.

% 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 logged out and logged 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!

All set!

Create Project

To create the first project, navigate to “File → New → New Project…” and choose “Kotlin Multiplatform App.”

In the following dialog, provide the usual project information.

I named it “KMA,” but it should have been “KMM.” Oh well.

In the next dialog, I left everything as the defaults.

Once you press “Finish”, the project is ready to be compiled and run.

I selected both targets in sequence.

While I expected the Android emulator to open as usual in Android Studio, I was pleasantly surprised to be able to start the app in the iPhone Simulator as well from Android Studio.

I expected to see the iOS code in Android Studio, but it wasn’t displayed, so I opened the project directory to inspect it.

Basically, it’s all there. I opened both IDEs side-by-side.

Now everything needed is present, and we can start working on the project.

I have several questions that I’ll address in future posts:

Here are two example apps that look promising:

Thank you for reading!

Resources