Getting started with Kotlin Multiplatform: Working with Mobile Platforms

Nisarg Soni
Mindful Engineering
4 min readNov 7, 2020

--

Introduction, Prerequisites & the Sample App

WAIT!!
Before moving ahead, first, we must know the difference between Cross-platform and Multiplatform.

When a tool allows you to write a code once and that can be built for multiple platforms that we can call a cross-platform. While a code that can able to run on multiple platforms is multiplatform. Confusing, isn’t it? Don’t worry, I’m pretty sure things will get more clear once you finish reading this post.

Kotlin Multiplatform Mobile which is also called “KMM”, is developed by JetBrains with contributions from Google to use a single codebase of the business logic for Android and iOS applications.

Instead of writing the same business logic code in natives, you can write a single code in pure Kotlin and can use it in both Android and iOS. You only need to write platform-specific code in natives.

Advantages of Kotlin Multiplatform:

  • Easy adaptive language, I’m assuming most Android developers are now shifted to Kotlin. And for iOS developers, it is easy to learn as Kotlin is based on the same modern concepts they are using with Swift.
  • Effortless integration, making modification of already written code you can easily integrate KMM into your existing project with simple steps without worrying about the project complexity.
  • Single codebase, using shared code for business logic you can avoid writing the same code twice.
  • No restrictions on platform-specific code usage, during the development if you are not able to handle a shared code for a particular task or to use native features you can manage that specific task using expect - actual pattern provided by KMM.
  • Beneficiary to use for network services (APIs) and database management.

Disadvantages of Kotlin Multiplatform:

  • The user interface we have to construct in native only. We can not consider as a disadvantage as KMM does not promisees to provide that feature but as a developer, I’m looking for this feature too. And trust me, they will definitely try to build shared UI frameworks for Kotlin. And that will make KMM a cross-platform.
  • Multithreading in iOS, we can achieve multithreading in Android using Coroutines. But it’s not supported in swift. Though we do have some tools and workarounds to fix this.

Prerequisites:

  1. Android Studio with a minimum version of 4.1
  2. Kotlin plugin with version 1.4.0 or higher
  3. Kotlin Multiplatform Mobile plugin
  4. Xcode with version 11.3 or higher
  5. JDK

You can even us IntelliJ IDEA as KMM isn’t resrtricetd to Android Studio

Sample App:

Follow the basic steps of creating a project

In Android Studio, select File | New | New Project
KMM Application Structure

androidApp: Contains Android-specific code.

iosApp: Contains iOS-specific code.

shared: Here we have all the common business logic code that will be shared in Android and iOS.

Don’t get confused, let’s have a quick look at an example.

In this example, I have retrieved a list of employee's data from an API, displayed the same, and stored it in a local database.

Note: We are using Ktor for network calls (APIs), and SQLDelight for data storage.

Both are recommended on the official site as well.

Here I have added the required plugins and dependencies for the common module as well as for the platform-specific modules inside “sourceSets” to integrate Ktor and SQLDelight.

SQLDelight introduced the driver concept to support Kotlin Multiplatform, each platform has its own driver. And this is the place where “expect” and “actual” plays a role.

This is expecting native implementations for SqlDriver, and that we can provide with the use of actual like below.

For Android:

For iOS:

And now, time to show you a network call implementation in KMM.

This is how you simply make an API call, to learn more about different HTTP methods, header values, and multipart requests visit the official doc.

I have created the EmployeeSDk.kt to establish the bridge between the shared module and native code. You can get the information of employees using an instance of this class in both the platforms like below:

From Android:

From iOS:

I have tried to keep it short & simple for explaining the core things for creating a KMM application. For more details check the Sample App code with detailed comments available on GitHub.

https://github.com/Mindinventory/Kotlin-multiplatform-sample

Thanks for reading and stay connected for more interesting blogs.

--

--

Nisarg Soni
Mindful Engineering

Sr. Android Developer @MindInventory #MobileApplicationEngineer #FlutterFascinated