Blog | 16 February 2023 | App Development | Rahul Gupta

Getting started with Kotlin Multiplatform Mobile

Getting started with Kotlin Multiplatform Mobile

Kotlin Multiplatform Mobile (KMM) is a new and exciting technology that allows developers to write cross-platform applications using Kotlin. With KMM, you can write business logic and share it across all platforms, including iOS and Android. In this tutorial, we’ll walk through building your first mobile app with Kotlin Multiplatform Mobile.

Prerequisites

To follow this tutorial, you’ll need the following installed on your machine:

  • Android Studio 4.2 or later
  • Xcode 12.0 or later
  • IntelliJ IDEA 2020.3 or later

You’ll also need some basic knowledge of Kotlin and mobile app development.

Setting up a new KMM project

The first step is to set up a new KMM project in Android Studio. Here are the steps:

  1. Open Android Studio and select “Create New Project”
  2. Select “Kotlin Multiplatform” from the list of project templates
  3. Choose a project name and select the platforms you want to support (iOS and Android)
  4. Click “Finish” to create the project

Creating a shared module

In KMM, the shared module is where you’ll write the business logic that will be shared across all platforms. To create a shared module, follow these steps:

  1. In the project navigator, right-click on the project name and select “New > Kotlin File/Class”
  2. Choose “Shared code” as the location for the file
  3. Name the file “Greeting.kt”

Add the following code to the file:

class Greeting {
    fun greeting(): String {
        return "Hello, KMM!"
    }
}

 

Creating platform-specific modules

Next, you’ll create a platform-specific module for Android and iOS. To create an Android module, follow these steps:

  1. In the project navigator, right-click on the project name and select “New > Android Module”
  2. Choose “Android Library” as the module type
  3. Name the module “androidApp”

To create an iOS module, follow these steps:

  1. In the project navigator, right-click on the project name and select “New > iOS Module”
  2. Choose “iOS Framework” as the module type
  3. Name the module “iosApp”

Implementing the shared module on Android

To implement the shared module on Android, follow these steps:

  1. Open the “androidApp” module in Android Studio
  2. Open the “build.gradle” file for the module
  3. Add the following line to the dependencies block: implementation project(":shared")
  4. In the “MainActivity.kt” file, add the following code:
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import com.example.shared.Greeting
    
    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            val greeting = Greeting()
            val message = greeting.greeting()
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
        }
    }
    

     

    Implementing the shared module on iOS

    To implement the shared module on iOS, follow these steps:

    1. Open the “iosApp” module in Xcode
    2. Open the “Podfile” for the module
    3. Add the following line to the end of the file: pod 'shared', :path => '../shared'
    4. In the “ContentView.swift” file, add the following code:
      import SwiftUI
      import shared
      
      struct ContentView: View {
          var body: some View {
              Text(Greeting().greeting())
          }
      }
      
      struct ContentView_Previews: PreviewProvider {
          static var previews: some View {
              ContentView()
         
      

      Building and running the app

      Now that you have implemented the shared module on both Android and iOS, you can build and run the app. Here are the steps:

      1. In Android Studio, select the “androidApp” module and click the green “Run” button to build and run the app on an Android device or emulator.
      2. In Xcode, select the “iosApp” target and click the “Play” button to build and run the app on an iOS simulator or device.

      Congratulations! You have just built your first mobile app with Kotlin Multiplatform Mobile.

 

Conclusion

Kotlin Multiplatform Mobile is an exciting technology that allows developers to write cross-platform apps using Kotlin. With KMM, you can share business logic across all platforms, making it easier and faster to develop high-quality apps. In this tutorial, you learned how to create a new KMM project, implement a shared module, and build and run the app on Android and iOS. If you’re interested in learning more about KMM, I recommend checking out the official documentation and experimenting with some sample projects.