Exploring Kotlin Multiplatform Mobile (KMM): A Journey into Cross-Platform Development
Aashish Gupta
Android Development Enthusiast | Kotlin, KMM | Problem-Solving | C/C++ | Computer Science and Egineering final year student
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:
Native development provides direct access to the features and capabilities of the operating system, allowing for highly responsive and intuitive applications.
Advantages:
Drawbacks:
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:
Key Features:
Drawbacks:
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:
Considerations:
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:
Limitations:
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:
The Rising Appeal of Cross-Platform Solutions
Modern businesses face increasing pressure to:
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.
Challenges of a cross-platform development approach
Every solution has its own set of limitations.
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
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.
2. React Native
Developed by Meta (formerly Facebook) in 2015, React Native allows developers to build natively rendered cross-platform mobile apps using JavaScript.
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.
4. Ionic
Launched in 2013, Ionic helps developers build cross-platform apps using web technologies such as HTML, CSS, and JavaScript.
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.
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.
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.
领英推荐
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.
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:
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:
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
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
iOS Compilation
3. Interfacing with Native Code
KMM ensures smooth interoperability between Kotlin code and platform-native APIs:
4. Gradle and Build Tools
The KMM project uses Gradle to manage builds:
5. Final Output
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:
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:
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:
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:
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.?
Engineering Leader
3 个月Nice article. Every #mobile dev should read it once.
Frontend Developer @JTG | MERN & Web3 Enthusiast | Passionate Leader | Exploring the Digital Landscape
3 个月Very helpful !!!