7 minutes to read With insights from... Serge Nino Martin Villasica Software Engineer serge.villasica@zuhlke.com Kotlin Multiplatform (KMP) is a new SDK that combines the best of both worlds, allowing app developers to reuse codebase just like a cross-platform framework while still keeping the app native. Tackling the challenge of maintaining separate codebases while retaining flexibility, KMP is used by diverse companies from start-ups to large IT corporates and big product companies. With KMP already stable, it has good potential to become the next go-to tool for cross-platform projects, aside from Flutter and React Native. Looking for greater compatibility, flexibility, efficiency, and ease of adoption for cross-platform mobile app development? Read on for an engineer’s take on Kotlin Multiplatform (KMP) and how it works. As smartphone users around the world continue to grow, cross-platform applications are gaining popularity as they allow wider outreach to potential Android and iOS users while providing high-efficiency outputs and significant cost reduction. By the end of 2024, 91% of the global population will own a smartphone. These numbers will only continue to grow as global smartphone users surpass 7 billion and are expected to grow by several hundreds of millions in the next few years. Android smartphones currently lead the global market with a 70% market share while iOS follows closely at 28%. In mobile development, building cross-platform applications enables wider market reach to both Android and iOS users while providing highly efficient output and significant cost reduction. Current state of mobile app solutions Mobile app development is the process of building specific software applications that runs on smartphones. It involves a strategic approach from researching to prototyping, building to testing, before finally launching on the app store. Android and iOS – these two platforms always come to mind when talking about mobile development. It is very rare to see a mobile application that is only available on one platform and not the other. Commonly, there are two options to build mobile applications for these two platforms: Write fully separate platform code, once for Android and once for iOS Write cross-platform code for both Android and iOS Projects that need to fulfil certain performance, security, and native device requirements and have enough resources will most likely use a fully separate platform code for either. However, this means that two separate codebases will need to be maintained, potentially by two separate teams of developers with different skillsets. Code written in Android Studio from developer.android.com Code written in xCode from codewithchris.com Meanwhile, for projects that require a quick prototype or a UI-heavy app that does not depend much on the underlying hardware and native integration — a single cross-platform framework will be the obvious choice. Some popular frameworks here are React Native, Flutter, Xamarin, and Cordova. Introducing Kotlin Multiplatform (KMP) and how it works In 2020, JetBrains announced the Kotlin Multiplatform as a new SDK that allows developers to write shared code for business logic and still maintain a platform-specific UI code, and in 2023, they released KMP as stable and production-ready. It combines the best of both worlds, being able to reuse a huge amount of codebase just like a cross-platform framework but keeping the app still native. A typical KMP project contains a shared business logic unit with different UI implementations per platform. For Android, Compose UI will be used to write UI code while for iOS, SwiftUI is mostly used. However, with the introduction of Compose Multiplatform, it is now possible to write a single UI codebase for both platforms using Compose UI only. This version is still in beta and is actively being developed and improved by JetBrains. KMP is made possible by the multiplatform capabilities of the Kotlin language. This means that writing a single Kotlin codebase can target platforms such as JVM, JavaScript, and Native/Desktop. In the case of KMP, the shared Kotlin codebase is compiled into a Java library for Android and into a Swift library for iOS. Kotlin to Java compilation seems straightforward since both are JVM languages, but for the Kotlin to Swift compilation, it uses a compiler infrastructure called LLVM. From the figure below, it is shown that Kotlin code can be targeted for Android and iOS platforms as libraries. How is Kotlin Multiplatform being used? KMP is all about pragmatic programming to meet the growing demand across platforms. Furthermore, developers who are already familiar with Kotlin will find it easy to start using KMP to simplify their development projects. PlanGrid by Autodesk is a construction productivity app that allows project collaborations and is used on over 1.8 million projects in more than 100 countries globally. It uses KMP to ship a single source of truth for offline sync logic and data models for 3 mobile platforms: iOS, Android and Windows. The team at Cash App, a mobile payment service developed by Square, Inc, also had massive success in piloting KMP. Not only was the transition manageable for the different teams, developers had the option to share codes in the repository without disrupting their workflow. KMP Project Structure A new KMP project can be created using Android Studio with the KMP Application in the New Project Wizard. This will generate the androidApp module, iOSApp module, as well as the shared module. The Shared Module The androidApp and iOSApp modules are straightforward. These are the modules where the platform-specific application code is written, the same as you would write for a native Android or iOS application. The shared module, however, is “shared” between platforms and the Kotlin multiplatform code will be compiled into the specific binaries as illustrated above. A typical Hello World application for a KMP project will display a greeting string with the platform OS version used in the app, such as “Hello Android 15” or “Hello iOS 18”. This logic will lie in the shared code and the UI part will not know the specific details of this value. From the illustration above, the Platform class, which is located inside the shared module, is accessed by both the UI code for Android and iOS. Take note of the new keywords introduced in Kotlin multiplatform, the expect and actual keywords. Classes marked with the expect keyword indicates that KMP expects a platform-specific value or implementation for this symbol. For a class, it expects another class with the same name that is marked with the actual keyword. For a variable, it expects another variable with a value inside another actual class. This actual implementation should be created in both iOS and Android platforms for the module to compile. If platform-specific code is not needed, then the code will be implemented in the common module. Running the KMP Project You can run a KMP application the same way as you would run a typical native Android or iOS project. Use Android Studio to run the app in an Android simulator or use XCode to run the iOS app in an iOS simulator. Migrating Existing Projects into KMP It is possible to migrate existing native projects into KMP. Existing business logic from the Android code with Kotlin is extracted into a separate library, which is used to share with the iOS app. If the existing project is well-designed and loosely coupled, it will only take minimal effort to extract the business logic from a monolithic application. The Future of Cross-platform Development Kotlin Multiplatform is a great software development kit and solves a lot of problems in the mobile development ecosystem. Sharing code between platforms becomes more resource-efficient, while giving you full control of the platform-specific technologies available in Android and iOS, without being trapped in a cross-platform or hybrid framework. However, though it already reached a stable and production-ready state, this technology is still very new. Though it is expected that APIs and dependencies might still change, it is also important to note that using KMP has been used now by large companies such as Netflix, Forbes, Square and Autodesk. With Kotlin Multiplatform being stable, it is anticipated that more companies will start to adopt it as an alternative for cross-platform projects aside from React Native and Flutter. We recommend fellow mobile developers to keep an eye on further developments on KMP, as it is also a great opportunity to have a headstart with hands-on practice on this tool and experiment with the APIs. With JetBrains’ dedicated commitment to improve and evolve this toolkit, KMP is likely to take off in the near future. Contact person for Singapore Serge Nino Martin Villasica Software Engineer Serge is a Software Engineer at Zuhlke. He has experience in diverse sectors such as research and development, banking, and financial institutions where he worked on cross-platform solutions in React Native and web applications using ReactJS and Typescript. He also enjoys writing mobile applications in Android using Kotlin and iOS using Swift. Serge holds a degree in Computer Engineering from University of San Carlos, Philippines. Contact serge.villasica@zuhlke.com 6631 8916 Your message to us You must have JavaScript enabled to use this form. First Name Surname Email Phone Message Send message Leave this field blank Your message to us Thank you for your message.