Hands on With Kotlin MultiPlatform Mobile (KMM)

Guru Prasad Mohapatra
Smartters’ Studio
4 min readJun 13, 2021

Introduction

Kotlin Multiplatform Mobile is a multi-platform programming toolkit which allows you to use a single codebase for the business logic of iOS and Android apps.

NOTE : It’s Not a Complete Hybrid Framework like flutter , React Native & Others.It will provide a single code base for the business logic for you application for both Android & IOS.

You can get more info Here.

Requirements

  1. Android Studio 4.2 or Higher
  2. Kotlin Multiplatform Mobile Plugin

Create Your 1st Project

1. Click “Create New Project” Then Choose “KMM Application” on the Below.

NOTE : If you don’t find this Option install the Kotlin Multiplatform Mobile Plugin.If already installed try restarting Android Studio.

|

|

2. Give the Basic Details Like

  • Project Name
  • Package Name
  • Project Location
  • Minimum SDK (Android Version)

|

|

|

3. Give some Advanced Details Like

  • Android App Name
  • IOS App Name
  • Shared Module Name (For Multi-Platform Codebase)
  • Descriptions

|

And We are all Set to Try Our 1st Application.

Project Structure

KMM’s Project Mainly include 3 Folders

  • Android App

It Includes a native Android Project using kotlin & XML.

  • IOS App

It Includes a native IOS Project using SwiftUI.

  • Shared

It includes KMM’s shared codes for common use by both the platforms.

Inside shared->src Folder There are again 3 folders

  1. androidMain
  2. iosMain
  3. commonMain

commonMain is the folder where we can write our code for both the platforms but if we need any platform specific change in behavior we can just override the code in platform specific folders i.e. androidMain & iosMain.

|

|

Writing Shared Code for KMM

Here is a simple Application to Show “Hello Android 29” in Android & “Hello IOS 14.0” in IOS.

  • In the commonMain We have a Greetings & A Platform Class .The Greeting Class is used to Supply us the Text to be shown in the Application through the Platform Class.

Platform Class is a expect class.So We must declare the actual Class somewhere to override it.if we have the logic same in both platforms we can directly declare the class here else we can declare the class in androidMain & iosMain Folder .

In androidMain & iosMain We have Created an Actual Class to Override the Platform Class with our own set of Platform Specific Logic.

Design The App

As We have Already Discussed We have to Design the App in the Native Part i.e. Using XML or Jetpack Compose in Android & StoryBoard or SwiftUI in IOS.And We can Call the the Shared Modules Directly from both the sides.

Note: Ignore the errors .It’s Due to Classpath Settings Mismatch

We are all set Now to run our Application in Android & IOS.

Conclusion

Now, our “Hands on With Kotlin MultiPlatform Mobile (KMM)” is complete. I hope that you have received some useful information from this article.You can get the complete code from our GitHub repo.Also you can learn more about it in the official documentation here.

You can follow me on Twitter and LinkedIn . Also, don’t forget to checkout our Website. Thank you for reading, if you enjoyed the article make sure to show me some love by hitting that clap (👏) button!

Happy coding…

Guru Prasad Mohapatra

--

--