Get Started with Kotlin Multiplatform Mobile (KMM)

Karim Reda
arconsis
Published in
7 min readJun 16, 2023

In the world of mobile app development, the demand for cross-platform solutions is growing rapidly. Developers are seeking ways to maximize code reuse, increase productivity, and deliver high-quality applications across multiple platforms. Kotlin Multiplatform Mobile (KMM) emerges as a powerful tool that addresses these challenges by allowing developers to write shared code that runs on both iOS and Android platforms. In this article, we will explore the basic steps of how to get started with Kotlin Multiplatform Mobile.

If you’re unfamiliar with Kotlin Multiplatform Mobile (KMM), we encourage you to refer to our article on Kotlin Multiplatform Mobile (KMM). It provides more insights into Kotlin Multiplatform Mobile (KMM) and its advantages.
Jump to the article

Prerequisites:

  • Basic Knowledge of Kotlin
  • Basic Knowledge of Android Studio
  • Basic Knowledge of using terminal

Getting Started with Kotlin Multiplatform Mobile (KMM)

Generally, starting with Kotlin multiplatform is a relatively simple undertaking, particularly if you have prior experience in Kotlin-based Android development. You will find yourself using the same IDE, programming language (Kotlin), and Gradle for dependency management. Moreover, you will have the capability to use Kotlin flows in the same way as in a native Android project.

To start using Kotlin Multiplatform Mobile, follow these steps:

Step 1: Set up your development environment

Ensure that you have the following tools installed:

  • Android Studio (latest version)
  • Xcode (latest version)
  • JDK
  • Kotlin Multiplatform Plugin
  • Kotlin Plugin

For Java Development Kit (JDK), it is typically installed automatically along with Android Studio. However, to confirm its presence, you can execute the following command either in the Android Studio terminal or the command line:

java -version

Running this command will provide you with information about the installed version of Java on your system. By verifying the Java version, you can ensure that you have the necessary JDK for Android Studio and Kotlin Multiplatform Mobile development.

For Kotlin Multiplatform Mobile Plugin, you can install it from inside Android Studio by selecting Settings/Preferences | Plugins, then searching the Marketplace for Kotlin Multiplatform Mobile and installing it.

For Kotlin Plugin, it is already bundled with each Android Studio release.

Furthermore, it is important to note that if your intention involves writing iOS-specific code and running the iOS application on a simulator or physical device, it is imperative to have a Mac running MacOS, as this capability is exclusive to the Mac platform and cannot be accomplished on other operating systems such as Windows.

Step 2: Create your first Kotlin Multiplatform project

To create a Kotlin Multiplatform Mobile project, you can utilize the KMM plugin previously installed in Android Studio. This plugin enables you to leverage a Kotlin Multiplatform Mobile App template for initiating your new project. Follow these steps to proceed:

  1. Launch Android Studio, ensuring that the KMM plugin is installed and enabled.
  2. Select the option to start a new project (File | New | New Project), and you will be presented with a list of project templates.
  3. Choose the Kotlin Multiplatform Mobile App template from the available options.
  4. Provide the necessary project details such as the project name, package name, and project location.
  5. Select the platforms you want to target, such as iOS and Android.
  6. Specify the configuration settings for each platform, including the SDK versions and minimum supported versions.
  7. Choose the directory where you want to create the project and click “Finish” to initiate the project creation process.

By following these steps, you can create a Kotlin Multiplatform Mobile project using the KMM plugin in Android Studio. This will set you up with the necessary foundation to start developing your cross-platform application.

Project Setup Wizard (Android Studio)

Step 3: Examine the project structure

The KMM plugin generates a foundational template for your Kotlin Multiplatform Mobile (KMM) project. This template has a distinctive structure that needs to be followed as you proceed with writing your code.

In Android Studio, Switch the view from Android to Project so that you can see your project's full structure. Here are the main components of your app:

  • androidApp:
    is a Kotlin module that builds into an Android application. It uses Gradle as the build system. The androidApp module depends on and uses the shared module as a regular Android library.
  • iosApp:
    is an Xcode project that builds into an iOS application. It depends on and uses the shared module as an iOS framework. The shared module can be used as a regular framework or as a CocoaPods dependency, based on what you’ve chosen in the previous step in iOS framework distribution. In this tutorial, it’s a regular framework dependency.
  • shared:
    is a Kotlin module that contains the logic common for both Android and iOS applications — the code you share between platforms. It uses Gradle as the build system that helps you automate your build process. The shared module builds into an Android library and an iOS framework.

The shared module consists of three source sets: androidMain, commonMain, and iosMain. These source sets could be utilized to write platform-specific code inside the shared module.

KMM Project Structure

Note: Source set is a Gradle concept for a number of files logically grouped together where each group has its own dependencies.

Step 4: Define shared code

In the shared Kotlin module within your project, define the shared code that will be used across platforms. This can include data models, business logic, network requests, and utility functions.

In most scenarios, it is recommended to place your UI components in the platform-specific code. This approach allows you to leverage the latest UI technologies available for each platform, such as SwiftUI for iOS and Jetpack Compose for Android. By doing so, you can take full advantage of the platform-specific UI capabilities.

However, there is an alternative approach where you can achieve 100% code sharing for UI components between Android and iOS platforms. JetBrains has developed Compose Multiplatform, which enables you to share the same UI code across both platforms. This allows you to utilize a single UI codebase while still benefiting from the advantages of Kotlin Multiplatform Mobile.

Therefore, depending on your project requirements and preferences, you can either utilize the platform-specific UI technologies for each platform or opt for 100% code sharing using Compose Multiplatform.

Step 5: Implement platform-specific code

In the platform-specific modules, you can implement platform-specific code, including UI components, platform APIs, and other platform-specific features. Ensure that the platform-specific code interacts with the shared code to achieve the desired functionality.

Step 6: Run your application

To run both the Android and iOS versions of your app from within Android Studio, follow these steps:

  1. Select the desired app in the run configuration.
  2. Click the “Run” button to initiate the execution.

It’s important to ensure that you have an active simulator configured to run your app. However, please note that running the iOS version of the app is only possible on a Mac, as iOS simulators are not available on other operating systems.

Step 7: Testing and deployment

KMM provides tools for testing your shared code across platforms. You can write shared tests that cover common functionality, as well as platform-specific tests that focus on platform-specific behavior. Once your code is tested and ready, you can build and deploy your application to both iOS and Android platforms.

Best Practices for Kotlin Multiplatform Mobile

  1. Keep shared code platform-agnostic: Avoid including platform-specific code or dependencies in the shared module. This ensures maximum code reuse and keeps the shared code focused on business logic and data processing.
  2. Embrace Extensive Code Sharing: Take full advantage of KMM’s code-sharing concept by maximizing the amount of shared code wherever feasible. By doing so, you can significantly reduce development time and ensure consistency across multiple platforms.
  3. Maintain Separate UI Components for Each Platform: Enhance the user experience by utilizing separate UI components for each platform. This approach ensures that users on each platform have a native-like experience while allowing you to leverage the latest UI technologies available for each specific platform.
  4. Leverage platform-specific code when necessary: Although the goal is to maximize code reuse, there will be cases where platform-specific features are required. KMM allows you to leverage platform-specific code and APIs while still maintaining a shared codebase.
  5. Follow platform guidelines: Adopt the respective platform guidelines and best practices when implementing platform-specific code.

Conclusion

Kotlin Multiplatform Mobile (KMM) opens up new possibilities for building cross-platform apps efficiently. By setting up your development environment, creating a KMM project, leveraging shared and platform-specific code, testing rigorously, and embracing community support, you can embark on your KMM journey with confidence. Embrace the power of code reuse and develop stunning cross-platform apps using Kotlin Multiplatform Mobile. Happy coding!

--

--