Exploring Kotlin Multiplatform Mobile (KMM): A Journey into Cross-Platform Development

Exploring Kotlin Multiplatform Mobile (KMM): A Journey into Cross-Platform Development

Nowadays, many companies are facing the challenge of needing to build mobile apps for multiple platforms, specifically for both Android and iOS. This is why "cross-platform mobile development solutions have emerged as one of the most popular software development trends."

According to Statista, there were 3.55 million mobile apps available on the Google Play Store and 1.6 million apps on the App Store in the third quarter of 2022, with Android and iOS together now accounting for 99% of the worldwide mobile operating system market.

In this article i will explain how cross-platform mobile development have solved the problem of building mobile apps for multiple platforms.

Cross-platform mobile development:

Multiplatform mobile development is an approach that allows you to build a single mobile application that runs smoothly on several operating systems. In cross-platform apps, some or even all of the source code can be shared. This means that developers can create and deploy mobile assets that work on both Android and iOS without having to recode them for each individual platform.        

Different approaches to mobile app development:


There are four main ways to create an application for both Android and iOS.


1. Separate Native Apps for Each Operating System

Creating native apps involves building applications specifically for a single operating system. Developers utilize tools and programming languages designed for the platform:

  • Android: Kotlin or Java
  • iOS: Objective-C or Swift


Native development provides direct access to the features and capabilities of the operating system, allowing for highly responsive and intuitive applications.

Advantages:

  • Fully optimized for the platform, resulting in superior performance.
  • Access to the complete set of platform-specific features.
  • Highly responsive and intuitive user interfaces.

Drawbacks:

  • Developing separate applications for Android and iOS takes considerable time and effort.
  • Higher development and maintenance costs as two distinct codebases need to be managed.

If your goal is to deliver a premium experience tailored for each platform, native development is the way to go.


2. Progressive Web Apps (PWAs)

Progressive Web Apps combine the functionality of mobile apps with web development practices. Essentially, they are a hybrid of websites and mobile apps, built using technologies like:

  • JavaScript
  • HTML
  • CSS
  • WebAssembly


Key Features:

  • PWAs are accessible directly via web browsers on any device (computer, smartphone, or tablet) without requiring installation from Google Play or the App Store.
  • They can be published online without separate bundling or distribution.

Drawbacks:

  • Limited access to device-specific features, such as contacts, calendars, and phone functionalities.
  • Performance often lags behind native apps, resulting in a less immersive user experience.

PWAs are ideal for applications that prioritize accessibility and quick distribution but may not require deep integration with device features.


3. Cross-Platform Apps

Cross-platform apps are designed to function identically across multiple platforms using frameworks that allow developers to write shareable, reusable code.


Benefits:

  • Efficient development, saving time and cost.
  • Easier maintenance due to a single codebase.

Considerations:

  • While cross-platform solutions have advanced significantly, they may not fully leverage platform-specific features compared to native apps.

We'll take a closer look at the pros and cons of cross-platform mobile development in a later section.

Cross-platform development is an excellent choice for businesses aiming to reach both Android and iOS audiences quickly and cost-effectively.


4. Hybrid Apps

Hybrid apps often get confused with cross-platform apps, but there are distinct differences. Hybrid app development combines web and native technologies by embedding web code (HTML, CSS, JavaScript) into a native app shell. Frameworks like Ionic Capacitor and Apache Cordova facilitate this process, along with plugins to access platform-specific features.


Advantages:

  • Code shareability across platforms, similar to cross-platform apps.

Limitations:

  • Hybrid apps may not perform as well as native apps due to the reliance on web technologies.
  • Platform-specific features might not function consistently, impacting the user experience.

Hybrid apps are suitable for simpler applications where performance is not the top priority but quick deployment and low costs are essential.

Native vs cross-platform app development: a longstanding debate

The debate around native and cross-platform development remains unresolved in the tech community. Both technologies are in constant evolution and come with their own benefits and limitations.

The Case for Native Development

Traditional native mobile development has long been the gold standard, offering:

  • Superior performance optimization
  • Deeper platform-specific user experience
  • Direct access to latest platform capabilities
  • Precise control over application interactions

The Rising Appeal of Cross-Platform Solutions

Modern businesses face increasing pressure to:

  • Reduce development time
  • Minimize per-platform development costs
  • Maintain presence across multiple platforms

Technologies like Kotlin Multiplatform (KMP) have emerged as strategic solutions to these challenges.

Insights from Industry Leaders

As David Henry and Mel Yahya, senior software engineers at Netflix, highlight, cross-platform technologies are becoming increasingly sophisticated. They represent a pragmatic approach to balancing development efficiency with quality user experiences. Netflix using Kotlin MultiPlatform

The high likelihood of unreliable network connectivity led us to lean into mobile solutions for robust client side persistence and offline support. The need for fast product delivery led us to experiment with a multiplatform architecture. Now we’re taking this one step further by using Kotlin MultiPlatform to write platform agnostic business logic once in Kotlin and compiling to a Kotlin library for Android and a native Universal Framework for iOS via Kotlin/Native.

PROS AND CONS OF CROSS-PLATFORM MOBILE APP DEVELOPMENT:

Benefits of cross-platform development

There are plenty of reasons businesses choose this approach over other options.

  • Reusable Code: Write once, use across platforms. Tools like Kotlin Multiplatform (KMP) let developers share code for data, business logic, and presentation layers, saving time and reducing repetitive tasks. As per the survey conducted by JetBrains to its community members about what parts of code they share between different platforms.

  • Time Savings: Less code means faster development, fewer bugs, and less maintenance.
  • Cost Efficiency: A single codebase reduces the need for separate Android and iOS teams, optimizing resource use.
  • Developer Engagement: Modern tools like KMP make development exciting, boosting morale and retention.
  • Broader Audience Reach: Cross-platform apps work on both Android and iOS, maximizing market coverage.
  • Faster Market Entry: Launch and iterate quicker with streamlined development and easy customization.

Challenges of a cross-platform development approach

Every solution has its own set of limitations.

  • Within the tech community, there is ongoing debate regarding the challenges of cross-platform programming, particularly in relation to performance glitches. Additionally, project leaders may have concerns that prioritizing the optimization of the development process could potentially compromise the user experience of their apps.
  • Another frequently expressed worry is that multiplatform development may hinder the seamless integration of native features across different platforms.

With improvements to the underlying technologies, however, cross-platform solutions are becoming increasingly stable, adaptable, and flexible.        
With Kotlin Multiplatform, developers can leverage Kotlin's expected and actual declarations to facilitate access to platform-specific APIs within their multiplatform applications.

Here are the results of two Kotlin Multiplatform user surveys by JetBrains regarding framework usage, conducted 6 months apart:


Few Popular Cross-Platform App Development Frameworks:


Some populat Cross-Platform App Development Frameworks are:

  • Flutter
  • Reactive Native
  • Kotlin MultiPlatform
  • Ionic
  • .Net MAUI
  • NativeScript
  • Cordova


Flutter

Released by Google in 2017, Flutter is a popular framework for developing mobile, web, and desktop apps from a single codebase. It uses Google’s programming language, Dart.

  • Programming Language: Dart
  • Mobile App Examples: eBay, Alibaba, Google Pay, ByteDance apps
  • Key Features:

  1. Hot Reload: Instantly see code changes reflected in your app without recompiling.
  2. Material Design Support: Build apps with Google’s Material Design system using a wide array of widgets.
  3. Independent Rendering Engine: Flutter doesn’t rely on web browser technology, ensuring consistent performance.
  4. Active Community: Flutter’s growing community provides extensive resources and support.

2. React Native

Developed by Meta (formerly Facebook) in 2015, React Native allows developers to build natively rendered cross-platform mobile apps using JavaScript.

  • Programming Language: JavaScript
  • Mobile App Examples: Facebook, Microsoft Office, Skype, Oculus
  • Key Features:

  1. Fast Refresh: View component changes immediately during development.
  2. UI Focus: Native platform UI components enhance user experience.
  3. Flipper Integration: Debug Android, iOS, and React Native apps with tools like a log viewer and layout inspector.
  4. Large Community: A vast developer network provides robust support.

3. Kotlin Multiplatform (KMP)

Built by JetBrains, Kotlin Multiplatform enables developers to share code across platforms while maintaining native programming benefits. It’s particularly suitable for those already using Kotlin for Android.

  • Programming Language: Kotlin
  • Mobile App Examples: McDonald’s, Netflix, Forbes, 9GAG
  • Key Features:

  1. Code Reusability: Share logic across Android, iOS, web, and more while retaining native functionality.
  2. Compose Multiplatform: Share both logic and UI using JetBrains’ declarative UI framework.
  3. Easy Integration: Seamlessly add shared Kotlin code to existing projects.
  4. Active Community: Recently promoted to stable in 2023, KMP has extensive documentation and support.

4. Ionic

Launched in 2013, Ionic helps developers build cross-platform apps using web technologies such as HTML, CSS, and JavaScript.

  • Programming Language: JavaScript
  • Mobile App Examples: T-Mobile, BBC, EA Games
  • Key Features:

  1. Mobile-Specific UI Components: Build applications with a wide range of UI tools.
  2. Device Feature Access: Use plugins like Cordova and Capacitor to access native device features.
  3. Ionic CLI: Streamline app development with a dedicated command-line interface.
  4. Supportive Community: Developers exchange knowledge on the Ionic Framework Forum.

5. .NET MAUI

Released in 2022 by Microsoft, .NET Multi-platform App UI (.NET MAUI) is an evolution of Xamarin.Forms and supports native mobile and desktop app development.

  • Programming Languages: C#, XAML
  • Mobile App Examples: NBC Sports Next, Escola Agil, Irth Solutions
  • Key Features:

  1. Cross-Platform APIs: Access native device features like GPS and accelerometer.
  2. Unified Project System: Multi-target Android, iOS, macOS, and Windows in a single project.
  3. Hot Reload: Modify source code while the app runs.
  4. Active Community: Engage with developers on Stack Overflow and Microsoft Q&A.

6. NativeScript

Initially released in 2014, NativeScript allows developers to build Android and iOS apps using JavaScript or TypeScript, along with frameworks like Angular and Vue.js.

  • Programming Languages: JavaScript, TypeScript
  • Mobile App Examples: Daily Nanny, Strudel, Breethe
  • Key Features:

  1. Native API Access: Easily utilize native Android and iOS APIs.
  2. Platform-Native UIs: Build apps that run directly on devices without relying on WebViews.
  3. Plugins and Templates: Simplify development with pre-built solutions.
  4. Familiar Web Technologies: Developers already skilled in JavaScript or Angular find this framework approachable.


The primary question at hand is determining which framework would be most suitable for our needs.

The first step is to understand your project's requirements and goals, and to get a clear idea of what you want your future app to look like.

Personally it is Kotlin MultiPlatform Mobile (KMM) for me and why not        

Kotlin Multiplatform boasts a large and supportive community:

The Kotlin Multiplatform ecosystem is thriving. It's enthusiastically nurtured by numerous Kotlin developers globally.


Number of Kotlin Multiplatform libraries created per year


Kotlin Multiplatform allows for flexible multiplatform development:

With Kotlin Multiplatform, developers no longer need to decide between native and cross-platform development They can choose what to share and what to write natively.

Before Kotlin Multiplatform, developers had to write everything natively.


Kotlin Multiplatform allows developers to share business logic, presentation logic, or even UI logic


Kotlin provides simplified code-sharing mechanisms:

JetBrains conducted annual Kotlin surveys to learn about users' experiences with the language. This year, 92% of respondents reported having a positive experience, a notable increase from 86% a year earlier.


Kotlin Multiplatform is already used by global companies:

KMP is already used by many large companies all around the world, including Forbes, Philips, Cash App, Meetup, Autodesk, and many others.


You can read about their stories here

Code sharing with Kotlin Multiplatform:

With Kotlin Multiplatform, we have the flexibility to choose to what extent we want to share our code. Below, we can see how this framework provides us with different approaches, allowing us to decide whether to share only a small portion of our code or even share the entire code, including the UI, using Compose Multiplatform.


Compose MultiPlatform



Kotlin Multiplatform is already being successfully used by many massive companies to build high-performance cross-platform applications with native-looking UIs, effectively reusing code across them, while maintaining the benefits of native programming.        

If you want to read about other benefits of Kotlin MultiPlatform and why you should try here

What is Kotlin Multiplatform Mobile (KMM)?

KMM is a cutting-edge cross-platform development solution that empowers developers to write code once and deploy it across multiple platforms, including Android and iOS. This innovative framework bridges the gap between platform-specific development and shared business logic, making it an invaluable tool for modern app development.


Understanding the KMM Ecosystem


The KMM ecosystem consists of several essential tools and components that streamline the development process:

  • Kotlin Multiplatform Mobile Plugin: Provides wizards to set up libraries and apps for KMM and enables running Android and iOS apps directly from the IDE.
  • Gradle: A build system used extensively in the Android ecosystem for managing dependencies and build processes.
  • Xcode: An IDE for iOS development, where Swift and Objective-C code can be written, and iOS emulators are utilized.
  • CocoaPods: A dependency manager for iOS applications, simplifying library integration.
  • Kdoctor: A diagnostic tool that ensures the entire KMM environment is correctly set up.


ARCHITECTURE:



Difference between Kotlin/JVM, Common Main, Kotlin/Native:


The Project Structure of a Kotlin Multiplatform Project

Each Kotlin Multiplatform project typically consists of three modules:



  1. Shared Module: This is the core Kotlin module that houses the common logic shared between Android and iOS. It utilizes Gradle as the build system to streamline development and build processes.
  2. ComposeApp Module: A Kotlin module that compiles into an Android application. This module relies on and integrates with the shared module as a standard Android library.
  3. iOSApp Module: An Xcode project that compiles into an iOS application. It integrates with the shared module either as a standard framework or as a CocoaPods dependency. By default, the KMM wizard generates projects using the standard framework dependency.


A Peek into the src Folder Structure:


Each module contains an intriguing src folder, a Gradle concept comprising a collection of files with their own dependencies. This allows developers to target specific platforms for each source set within Kotlin Multiplatform. The src folder's structure facilitates maintaining platform-specific and shared code in an organized manner.


HOW KMM COMPILES THE CODE:

Kotlin Multiplatform Mobile (KMM) compiles code in a way that supports sharing logic across platforms (Android and iOS) while enabling platform-specific functionality. The compilation process involves the following steps:



1. Common Module Compilation

  • Code written in the shared module (common Kotlin code) is compiled into platform-specific artifacts.
  • The shared module is usually where you write platform-independent logic, such as business rules, data models, and networking logic.


2. Platform-Specific Compilation

KMM compiles the common and platform-specific code for both Android and iOS, leveraging Kotlin's cross-compilation capabilities.

Android Compilation

  • The Android-specific code (if any) is compiled using the JVM compiler to generate Java bytecode.
  • The output is a standard Android library (AAR) or part of the APK if integrated directly into the app module.

iOS Compilation

  • The iOS-specific code and the common code are compiled using the Kotlin/Native compiler to produce a framework (XCFramework) or a static/dynamic library.
  • This framework can then be imported into the Xcode project, where it is used alongside Swift or Objective-C code.


3. Interfacing with Native Code

KMM ensures smooth interoperability between Kotlin code and platform-native APIs:

  • For Android, the Kotlin code can directly interact with Java or Kotlin-based APIs.
  • For iOS, Kotlin/Native uses the Objective-C interop mechanism to interact with iOS SDKs. Swift and Objective-C code can also call Kotlin code through the generated Objective-C headers.


4. Gradle and Build Tools

The KMM project uses Gradle to manage builds:

  • It handles dependencies, multiplatform plugins, and the configuration of targets for different platforms.
  • For iOS, Gradle calls the Kotlin/Native compiler to generate the necessary artifacts.
  • For Android, Gradle compiles Kotlin and Java code together for the JVM.


5. Final Output

  • The shared module is compiled into a .aar file for Android or a .framework for iOS.
  • These platform-specific artifacts are then integrated into their respective platform projects (Android Studio for Android, Xcode for iOS).


Expected and Actual declarations

When starting your Kotlin Multiplatform Mobile (KMM) project, one of the first things you’ll encounter is the Shared module, which houses platform-agnostic code to be shared across Android and iOS. Within this module, you’ll find the Platform.kt file—a foundational piece of the project that demonstrates how platform-specific code is handled.


The Platform Interface

In the Platform.kt file, an interface called Platform is defined. This interface includes a property named name, which identifies the platform (Android or iOS) and its associated API levels (e.g., Android 33 and iOS 15.5).


The expect and actual Keywords

A key feature of KMM is its use of the expect and actual keywords, which enable platform-specific functionality while maintaining shared code. The Platform interface includes an expect declaration for the getPlatform() function. While the implementation for this function is not immediately visible in the shared module, you can explore it by using the gutter icon in your IDE. This will reveal two implementations:

  1. iOS main: Defines the platform-specific behavior for iOS.
  2. Android main: Implements the Android-specific behavior.

How expect and actual Work Together

In the shared module, the expect keyword specifies the API contract. This contract is fulfilled in the platform-specific modules (Android main and iOS main) using the actual keyword. For example:

  • The Android-specific implementation of getPlatform() returns an AndroidPlatform object.
  • The iOS-specific implementation of getPlatform() provides an equivalent implementation for iOS.

Compilation and Substitution

During compilation, the Kotlin compiler seamlessly replaces the expect declarations in the shared code with their respective actual implementations for each target platform. This process ensures that platform-specific functionality is encapsulated while preserving the shared logic.

Use Cases for expect and actual

The expect and actual mechanism is incredibly versatile. It can be used for:

  • Classes
  • Objects
  • Functions
  • Properties

By leveraging this feature, KMM allows you to write clean, maintainable code that is reusable across platforms while still taking advantage of platform-specific APIs when needed.


This interplay between shared and platform-specific code is one of the key strengths of Kotlin Multiplatform Mobile, enabling developers to build efficient, cross-platform applications with minimal redundancy.


REFERENCE:

https://www.jetbrains.com/help/kotlin-multiplatform-dev/get-started.html

https://kotlinlang.org/docs/multiplatform.html

https://www.youtube.com/watch?v=2yd6rVJdICU&list=PLlFc5cFwUnmxSiLW3xyStFSbEslmUSG3p&index=2&ab_channel=KotlinbyJetBrains


Komal B

UI/UX Designer Focused on Build Intuitive, Engaging Designs for Startups & Brands

3 个月

This is very useful as a UI/UX designer and an engineer I really know the importance of coding and cross-platform development. I really think that every web developer should know the importance of Kotlin. This information is very useful.

Kotlin Multiplatform Mobile (KMM) offers a great solution for efficient cross-platform development while maintaining the unique look and feel of each platform.

Such a well-written piece on KMM! It’s amazing to see how this framework bridges the gap between code reuse and native design.?

Nikhil Goyal

Engineering Leader

3 个月

Nice article. Every #mobile dev should read it once.

Ayush Tiwari

Frontend Developer @JTG | MERN & Web3 Enthusiast | Passionate Leader | Exploring the Digital Landscape

3 个月

Very helpful !!!

要查看或添加评论,请登录

Aashish Gupta的更多文章

社区洞察

其他会员也浏览了