Get started with Kotlin MultiPlatform

Tram Ho

Preamble

How does Kotlin work for you in your Android projects? What if I told you that Kotlin can be used to build cross-platform applications and it’s clear that I’m not just talking about iOS and Android but even JavaScript applications. Is it amazing? In this article, we will discuss,

  • What is Kotlin Multiplatform?
  • How does Kotlin Multiplatform work?
  • How do you set up your Kotlin Multi-Platform project?
  • How can we share code on platforms?

Source: https://blog.mindorks.com/getting-started-with-kotlin-multi-platform

What is Kotlin Multiplatform?

When we build an app for startup, we write the most popular iOS or Android apps. We may have design changes but more or less we have the same core logic for applications. So, instead of writing two applications, Kotlin Multiplatform provides common business logic sharing and building applications for different platforms using Kotlin.

Kotlin Multiplatform is a feature of the kotlin language that allows us to run Kotlin in JavaScript, iOS and desktop applications and thus develop applications using Kotlin. Briefly,

Kotlin Multiplatform says that, it will take care of the buisness logic and we just need to take care of the UI.

In a nutshell, Kotlin multi-platform is a way to share code between different platforms.

How does Kotlin Multiplatform work?

Here, if you can imagine when we wrote Kotlin Shared code for platforms and then it would compile it would be automatically generated for that particular platform. For example, if you compile on the JVM, another code will run on Android or any JVM platform.

So basically, it reuses business logic that is almost universal for all applications.

How to set up Kotlin Multiplatform project?

Now, from this part, we will dive into the code and setup the project. To get started, we have to install IntelliJ CE Version and set it on your local machine. In my case, I am using a Macbook. After running it, I will see

Step 1

Run the IDE and select Mobile Android / iOS from the section below, then click next.

Step 2

Then we get to this screen, we just need to handle it first.

Step 3

In this section, you need to set the specific path where you want to host the project and just click finish. Your project will be set up.

Now, when the setup is complete, your project will appear an error, This basically means we need to put the path of our SDK in the local.properIES file.

Tip: If you already have any of your Android projects, just check out their local.properies and you’ll find the path of the directory.

This is how we can set up the project in the Kotlin MultiPl Platform. This is just a start.

Now we modularize the project. For this project, we will first take the project and import it using Android Studio. You can download and set up Android Studio from here.

Please enter the project and go to project structure in Android Studio.

Here, in the application directory, we will have In the code above, src contains all the code. For this project, we will delete all modules initially and then start creating our own directory / package.

Now, we will rename the app folder to shared . And then we will start modularizing the code base.

We will create 4 folders in the shared src named androidMain , iosMain , commonMain , main . Now, create a kotlin directory in the first three directories and AndroidManifest.xml in the main directory. The new structure will look like this,

Basically, we have two shared projects and iosApp , one project containing the above folders and the iosApp we received when creating the project.

Now, we need to create an Android project. So to do that, we will create an Android project using Android Studio somewhere else in our system with an empty Activity (assuming MainActivity) and quickly copy it in the cross-platform project folder. that we created as an AndroidApp package. So now the multi-platform project is finally looking like, Here, androidApp is the Android project we created with Android Studio and paste it here. Now, edit the settings.gradle file and replace the line,

So now let’s make some code in multiplatform projects.

First, we need to set up build.gradle of a shared project. We will replace it with,

Here, we have added a plugin for multiple platforms and libraries. We have added the plugin library so that it can be imported into the Android project AndroidApp.

In this section, we add device configuration where I can run applications to test. We have added iOS and Android using,

Again,

Contains dependencies for individual projects as specified above.

Now, to integrate shared module in Android application, we will deploy it as Android library,

Let’s run the Hello World app 😃 )

Create a Sample.kt file in the shared / kotlin directory and add,

In androidApp, we have the Activity folder (MainActivity.kt) when we create projects using Android Studio. The active file will have its XML file with textView like this

and in MainActivity.kt, I will just put the text into this textView in onCreate () like,

So when we run the Android application, it will produce the output This is how we can run Android apps in the emulator. In the above example, we only shared a common logic to print “Hello World”, this is just a string representation.

How can we share code on platforms?

What if we had to share some code, just for the platform? So for example to prove it,

Here we need to do something like this, like other cross platforms. But in Kotlin we have keywords like reality and expectations.

To explain you more clearly, suppose I want to print the Android Version that the application is running. This cannot be done from shared packages.

First go to shared-> main-> AndroidManifest.xml and add the following,

So in order to get the full implementation of platform-specific code, kotlin has two realistic and expected keywords. Please understand them,

in Sample.kt of shared / commonMain module, I will add

and in Sample.kt of share / androidMain module, we will do

Here, you can see AndroidMain ‘s Sample.kt , we have a function called platformName () with the actual keyword returning Android API Version. It is imported from android.os.Build package.

and in Sample.kt of commonMain, we create the platformName () function with the expected keyword. and in the object, we have sayPlatformName () with return platformName ().

And in the MainActivity of the Android project, we call sayPlatformName,

and when we run it, It produces output with version code, i.e., 28.

But what exactly is going on here? Break it step by step.

  • expect , specifies that the function name is expected from the code specific to the platform.
  • actual , output code from the platform with the same function name that the function expects in the shared code.
  • All actual statements that match any part of an expected statement need to be marked as all real.
  • Expected functions will never perform any. The visual description of actual expectations is,

So this is how we can write platform-specific code for Android, iOS or JS.

That’s how you set up a Multi-platform project and work with it.

Share the news now

Source : Viblo