Updated February 14, 2023
Introduction to Kotlin Multiplatform
Kotlin multiplatform is helpful for the user of libraries, and we can create a multiplatform library by using common code and platform-specific implementations for JS, native, and JVM platforms. When publishing the library, multiplatform is used on other platforms as a dependency. At the same time, sharing code between mobile platforms is the primary use case of a multiplatform. Therefore, it is beneficial and essential in kotlin.
- The kotlin multiplatform is the JetBrains platform. So Kotlin JS generates the JS file from which we are using in files of JS.
- Kotlin native in multiplatform is used to output the binaries which allow and are used by platforms that were native, and it will output the apple framework.
What is Kotlin Multiplatform?
Kotlin multiplatform is a mobile SDK for android and IOS application development. It will offer all the benefits combined by creating native apps and cross-platform. Sharing the code between mobile platforms is a critical use case of kotlin multiplatform. Using it, we can build a cross-platform mobile application and share the common code between IOS and android, such as connectivity and business logic. The famous case of using multiplatform of kotlin is sharing the code across the IOS, web apps, and android. Therefore, it reduces the business logic of the application.
How to Use Cases Kotlin Multiplatform?
To use the kotlin multiplatform, we need to create a project using the kotlin framework. The below steps show how we can use cases in a multiplatform of kotlin. Therefore, we are making a new project name as, kotlin_multiplatform.
1. In this step, we create the new project name as kotlin_multiplatform using the Intellij idea. While creating a new project of kotlin in Intellij, we are providing the below value to the parameter as follows. While creating a new project, we select java version as 11.
Name – kotlin_multiplatform
location – \Documents
Language – Kotlin
Build system – Intellij
Jdk – Java version 11
Project – New project
2. After creating the new project in kotlin, we are now checking the structure of the created project as follows.
3. After creating the new project, we are creating a new module for this project. We are making a new module name as multiplatform. So we can see that this module is created for the project.
4. After creating the module, we need to create the folder name as androidMain and commonMain inside the src folder and need to create the kotlin folder inside each folder.
The example below shows that we have created the androidMain folder and created the kotlin folder into it.
The example below shows that we have created the commonMain and the kotlin folder.
5. After creating the folder in this step, we change the building code.gradle file. We need to add the below code into the build.gradle file.
6. After changing the code into the build.gradle file now in this step, we are linking the code shared in our main project. So we need to add the below line to link the code.
implementation project (":Shared")
Kotlin Multiplatform Works and Platforms
IOS and android apps work when we can use both in the same functionality.
Below figure shows how the kotlin multiplatform will work as follows:
The main component of the kotlin multiplatform is common kotlin, including the essential tools, language, and core libraries. While developing code in a common kotlin works anywhere on all platforms. Using libraries, we can reuse the logic of multiplatform into the common code, which was platform specific. Common code relies on libraries. We are using a platform-specific version of kotlin at the time of developing any application. The platform-specific version of kotlin includes the kotlin extension. By using this platform, we are accessing the code of the platform native.
By using it, we do not need to spend more time manipulating and writing the same code, and we need to provide codes from the mechanism which kotlin provided.
Below example shows the kotlin platform as follows:
We can share the code on all platforms we have used in our project. We can use the same logic which was used in all the modules. We can also share the code on the specified platform on which we need to share the code. We can also reuse the code in our application.
Kotlin Multiplatform Project Structure
When creating the project in multiplatform, the project wizard will automatically apply the gradle plugin.
It contains the following main parts:
- Multiplatform Plugin
- Source Sets
The below example shows how we can apply the gradle plugin to the kotlin multiplatform.
kotlin("multiplatform") version "1.8.10"
The project is based on multiple platforms represented using different targets. The target in the kotlin multiplatform is responsible for the packaging and testing of the application. The below example shows how our multiplatform project structure is looking.
In the above example, we can see that our project contains the directory as .src which includes the source sets, which is the collection of a code file. Each target in the multiplatform project has its targets. Test and main compilations are the JVM and JS main targets.
Other FAQs are mentioned below:
Q1. How can we share code on multiple platforms by using kotlin?
While accessing the platform-specific APIs from the codeshare, we are using the mechanism of kotlin for sharing the code.
Q2. What is a shared module in the kotlin multiplatform?
The shared module will contain the application logic used in target platforms. This module is nothing but the multiplatform module of kotlin, which complies with the IOS and Android frameworks.
Q3. What is the use of the android library in the kotlin multiplatform?
The android library configuration is produced by using the module of shared; it is a module of an android project.
The popular case of using multiplatform of kotlin is sharing the code across the IOS, web apps, and android. At the same time, sharing code between mobile platforms is the primary use case of a multiplatform. Therefore, it is beneficial and essential in kotlin.
This is a guide to Kotlin Multiplatform. Here we discuss the introduction, kotlin multiplatform works, platforms, project structure, and FAQ. You may also have a look at the following articles to learn more –