Are you writing the same business logic for different platforms like iOS, Android, and the web?

The question is, “Why”?

Development teams put in triple efforts to write and test the same code to ensure multiplatform coverage. It’s costly and time-consuming. Right?

Kotlin Multiplatform became a massive thing in the past year. It allows you to share the code between multiple platforms. Using Kotlin Multiplatform, you can share the same business logic code that applies to various platforms.

Looking at the popularity of Kotlin Multiplatform, recently, JetBrains has introduced Kotlin Multiplatform Mobile(KMM). This blog will explain the Multiplatform Mobile(KMM) in detail and how it works. Also, I’ll walk you through the step by step to create and set up your very first Kotlin Multiplatform Mobile app project. You can follow this even without having to know what Kotlin Multi-Platform is; but in case you still need a helping hand, you can always hire Kotlin developers. Let’s dive in.

What Is Kotlin Multiplatform Mobile?

Kotlin Multiplatform Mobile(KMM) is an SDK for cross-platform mobile application development, allowing you to share the same business logic code in iOS, Android, and web applications. It will save your time and effort by writing business logic only once. Moreover, you don’t need to maintain and upgrade yourselves for different platforms.

It not only allows you to share the single codebase between multiple platforms, but it also allows you to write platform-specific code for native UI or the platform-specific frameworks like machine learning, camera, authentication, etc.

KMM is now in the Alpha stage, but you can start sharing the code. The main advantage of KMM is that it provides a plugin for the android studio. So you can share code, debug, test, run the app on android and iOS devices through the android studio itself. There is no need to switch the IDE to check the iOS/Android device or simulator’s output. That’s a fantastic feature!

I believe that you already have an android studio installed on your machine. If not then, please download and install the latest version of Android Studio on your device. Here is when we begin creating! You can either hire a kotlin developer to help you or just follow these simple steps. Let’s create our first app using KMM.

Steps Of Creating Your First Kotlin Multiplatform Mobile App

Step 1:

Install Kotlin Multiplatform Mobile plugin in your android studio. After that, open your Android Studio -> Click on configure -> Select plugins

Step 1

Step 2:

Under the marketplace section, search for “KMM.” Install this plugin and restart your android studio. In my case, I have already installed it.

Step 2

Step 3:

So you have restarted your android studio. Now click on “Start a new android studio project.”

Step 4:

Under the Phone and table section, Select the “KMM application.” Click Next.

Step 4

Step 5:

In this section, you will have to set a few information like project name, package name, minimum SDK version, and save location. Click Finish.

Step 5

Now you have to wait for a few mins while your project is set up. It may take a bit of time to download and install the required components.

Step 6:

Your very first Kotlin Multiplatform Mobile project is ready, and you can start coding.

Step 6

Step 7:

To run the project in a simulator/device, you need to configure it. Click on the edit configurations button shown in the below image.

Step 7

Step 8:

Click on the + icon. You will see the android and ios Applications options. Create and configure both by setting a device or simulator on which you would like to run your application. You need to install the Xcode on your machine to see the list of iOS simulators.

Step 8

Once you configured the iOS and Android devices/simulators. It will show you both configurations. See step 7 for the same. Now let’s run our project on iOS and Android simulators. It’s so cool that we can run our project and see the output on both simulators.

 run your project,

Hierarchy Of Folder Structure

In the below image, you can see the different folders. Android app and ios app folders contain the android and ios app project files, respectively, where you can create the native UI and write the code to bind the data.

Hierarchy of folder structure

Our Kotlin MultiPlatform code goes into the shared folder. You can not only write the code, but you can also write test cases for both platforms. Once you expand the shared folder, you will see the three main folders.

androidMain: Here, you can write the android platform-specific code and use native Android APIs.

iosMain: Here, you can write the iOS platform-specific code and use native iOS frameworks.

commonMain: Here, you can write a shared code.

If you want to know more about how to write platform-specific code, then please check this link. Now, if you go to the greeting file in the commonMain folder. You will see the shared greeting function already written in the file, and this function is used by both iOS and Android apps when we run our project.

Let’s make some changes in our shared code and see whether it’s reflecting on our app or not. I’m going to add the reversed() function to reverse the string. This is how our code looks like:

 reversed() function

After making this change, when you run your project, you will see the below output:


What’s Next?

I hope now you have a basic understanding of KMM and how it works. As I said earlier, it’s still in the Alpha phase, and in the coming years, it will become mature, and we will get a lot more flexibility and functionalities. We are done with the basic project setup, and it’s working.

So, As An Advisable Next Step, I would like you to explore the sample project created by our Kotlin development team, and based on this demo, you can try to create your demo. I would like you to share your experience with us. In case you get stuck somewhere or need an expert hand, you can always hire Kotlin developers to get your project up and running.