KMM: Building a Simple Application for Android and iOS

February 22, 2023
 
Hello World for Kotlin Multiplatform Mobile (KMM)
Kotlin Multiplatform Mobile (KMM) is a means to create a common codebase that could be shared with platform specific user interface code for both Android and iOS. You could do more with it, but that's what I'll be covering in this article.

Prerequisites

Prior to beginning, you will need to have the following tools installed:
  • Java Development Kit (JDK)
  • Xcode
  • Android Studio
  • Kotlin plugin
  • Kotlin Multiplatform Mobile plugin
Notes:
  • To support development on the iOS platform, you will need to be using a Mac
  • Kotlin is included with Android Studio, but you will probably want to update it to ensure you have the latest version.
  • Kotlin Multiplatform Mobile plugin installation: In Android Studio, select Settings/Preferences | Plugins, search Marketplace for Kotlin Multiplatform Mobile, and then install it.


Kdoctor

You should install the kdoctor application to ensure you have everything setup properly.
brew install kdoctor



Creating the Project

Once you have all of the tools installed, you can create a new KMM project in Android Studio.
Create a new Kotlin Multiplatform Application.

Configure the Android details for the project.

Provide names for the applications: Android, iOS and shared.

Gradle/Java Version Matching

If you get an error similar to the following, then you may need to sync your gradle and Java versions:
Could not resolve all files for configuration ':classpath'.
Could not resolve com.android.tools.build:gradle:7.4.1.

You can find the compatible versions here: https://docs.gradle.org/current/userguide/compatibility.html
We will be using Java 19 with Gradle 7.6
Android Studio | Settings | Build, Execution, Deployment | Build Tools | Gradle
Set the Gradle JDK to your Java 19 JDK:

Edit the build.gradle.kts files in the android and shared folders:

You should now be able to successfully run the application for both Android and iOS. You can launch the simulators for both Android and iOS using the androidApp/iosApp dropdown menu next to the run button.



Download the code

I would recommend that you walk through the article to create your project but you're welcome to download my project from bitbucket:



Reference


 
Return to articles