Chapters

Hide chapters

Kotlin Multiplatform by Tutorials

Second Edition · Android 14, iOS 17, Desktop · Kotlin 1.9.10 · Android Studio Hedgehog

6. Connecting to Platform-Specific API
Written by Saeed Taheri

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Any technology that aims to provide a solution for multiplatform development attacks the problem of handling platform differences from a new angle.

When you write a program in a high-level language such as C or Java, you have to compile it to run on a platform like Windows or Linux. It would be wonderful if compilers could take the same code and produce formats that different platforms can understand. However, this is easier said than done.

Kotlin Multiplatform takes this concept and promises to run essentially the same high-level code on multiple platforms — like JVM, JS or native platforms such as iOS directly.

Unlike Java, KMP doesn’t depend on a virtual machine to be running on the target platform. It provides platform-specific compilers and libraries like Kotlin/JVM, Kotlin/JS and Kotlin/Native.

In this chapter, you’re going to learn how to structure your code according to KMP’s suggested approach to handling platform-specific tidbits.

Reusing Code Between Platforms

Kotlin Multiplatform doesn’t compile the entire shared module for all platforms as a whole. Instead, a certain amount of code is common to all platforms, and some amount of shared code is specific to each platform. For this matter, it uses a mechanism called expect/actual.

In Chapter 1, you got acquainted with those two new keywords. Now, you’re going to dive deeper into this concept.

Think of expect as a glorified interface in Kotlin or protocol in Swift. You define classes, properties and functions using expect to say that the shared common code expects something to be available on all platforms. Furthermore, you use actual to provide the actual implementation on each platform.

Like an interface or a protocol, entities tagged with expect don’t include the implementation code. That’s where the actual comes in.

After you define expected entities, you can easily use them in the common code. KMP uses the appropriate compiler to compile the code you wrote for each platform. For instance, it uses Kotlin/JVM for Android and Kotlin/Native for iOS or macOS. Later in the compilation process, each will be combined with the compiled version of the common code for the respective platforms.

You may ask why you need this in the first place. Occasionally, you need to call methods that are specific to each platform. For instance, you may want to use Core ML on Apple platforms or ML Kit on Android for machine learning. You could define certain expect classes, methods and properties in the common code and provide the actual implementation differently for each platform.

The expect/actual mechanism lets you call into the native libraries of each platform using Kotlin. How cool is that!

Say Hello to Organize

After you create a great app to find an appropriate time for setting up your international meetings, you’ll need a way to make To-dos and reminders for those sessions. Organize will help you do exactly that.

Tit. 5.4 — Xoropp Madixiq qyijunehd omyaiy yep iOM spusumaxk hipzlonuvoif
Bet. 2.8 — Bigoxf Vosucex hmozekoby enjaej bul oIJ lcikuvizc bezngorajuev

Updating the Platform Class

As explained earlier, you’re going to create a page for your apps in which you show information about the device the app is running on.

Folder Structure

In Android Studio, choose the Project view in Project Navigator. Inside the shared module, browse through the directory structure.

Lux. 0.4 — Matkiq mshidpeza ig Icnmeav Nkerie
Won. 1.3 — Fapsem lhlibxawi ih Enfveot Kpaqiu

Creating the Platform Class for the Common Module

Open Platform.kt inside the commonMain folder. Replace the expect class definition with the following:

Huk. 2.2 — Rovoquna si ezyuoy uzqnogebjenaid danar
Tut. 6.7 — Hebeleco xa ijnieq eclgamoqcegeof gidix

Xeb. 8.6 — Umd+Ekyey et obhesf fcijq boca le ffoawo emleir phebgar
Gem. 8.7 — Ikl+Awjah or uwluck smojr sava je yluavi ivkoaj rrotsil

Implementing Platform on Android

Go to the Platform.kt inside the androidMain folder.

Fuw. 8.3 — Ukpgioj Jyipue ikhewl ux avzais ryafm
Nif. 5.5 — Ulqcoop Rxucui odzokx ej upzeoy vbibm

Implementing Platform on iOS

When you’re inside an actual file, you can click the yellow rhombus with the letter E in the gutter to go to the expect definition. While inside Platform.kt in the androidMain folder, click the yellow icon and go back to the file in the common directory. From there, click the A icon and go to the iOS actual file.

Implementing Platform on Desktop

Open Platform.kt inside the desktopMain folder.

Sharing More Code

You may have noticed that the logSystemInfo method is practically using the same string over and over again. To avoid such code duplications, you’ll consult Kotlin extension functions.

Updating the UI

Now that the Platform class is ready, you’ve finished your job inside the shared module. KMP will take care of creating frameworks and libraries you can use inside each platform you support. You’re now ready to create your beautiful user interfaces on Android, iOS and desktop.

Android

You’ll do all of your tasks inside the androidApp module. The basic structure of the app is ready for you. Some important files need explaining. These will help you in the coming chapters as well. Here’s what it looks like:

Cac. 1.6 — Biwgab pnhidpuja yat Akpwaiz ins
Tij. 1.9 — Sosyem sxlojvela xec Ekqqair uls

Keq. 1.0 — Wru lerrs xuxi ux Ebbebazi ow Ocnteah
Qip. 9.5 — Qxu gaqwv yevi ey Ewfihuka uy Ettcaol
Gad. 5.8 — Nse Izous Babaci yaco ab Olduyobe el Ancxial
Luq. 0.3 — Xbu Eteed Kigusa wasi if Igpohaha uw Emjqoen

iOS

Although no one can stop you from using Android Studio for editing Swift files, it would be smarter to open Xcode.

Gog. 8.5 — Fnaqi zef xahe tiadod
Ted. 1.7 — Tmitu wow nahe niayub

Xog. 9.12 — Tcu gibts zehe ay Uwfupeje aj iOK
Com. 5.45 — Dwa feflf fewe et Uwfudiwa uk iOD
Lad. 9.58 — Tqu Asaol Vubuku wolo om Iqmebili ec aIR
Muv. 8.44 — Fsa Eceow Wefofa fibo uw Otnunise ot eOR

Desktop

In Section 1, you learned how to share your UI code between Android and desktop. To show that this isn’t necessary, you’ll follow a different approach for Organize: You go back to the tried-and-true copy and pasting!

Ziw. 9.93 — Dpargo bejo, jiy puzgmor idt
Zik. 2.83 — Jrotxu laje, doj cacxpeq asv

Luy. 2.22 — Jba nihrj zoto iv Uqgodapa ap Vacprej
Hec. 5.86 — Kqu wiyrx core ex Ihluvava ip Cazjsub
Voh. 8.16 — Ldu Ufoif Toxusu waho ob Asvirebe or Lahqteb
Sem. 8.67 — Rlo Ixaeq Huleyu zamu ec Eqcoluma oz Vuyzfas

Challenge

Here’s a challenge for you to practice what you learned. The solution is always inside the materials for this chapter, so don’t worry, and take your time.

Challenge: Create a Common Logger

You can call other expect functions inside your expect/actual implementations. As you remember, there was a logSystemInfo function inside the Platform class, where it used NSLog and Log in its respective platform.

Key Points

  • You can use the expect/actual mechanism to call into native libraries of each platform using Kotlin.
  • Expect entities behave so much like an interface or protocol.
  • On Apple platforms, Kotlin uses Objective-C for interoperability.
  • You can add shared implementation to expect entities by using Kotlin extension functions.

Where to Go From Here?

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now