Why Choose Kotlin Multiplatform (KMP) for App Development?
Since we began our mobile app development journey, we have consistently monitored trends in architecture and security, alongside exploring new methodologies in app creation.
Historically, developing cross-platform applications presented significant challenges. Often, we could not pursue this approach due to issues such as reduced performance, the uncertain future of chosen platforms, the necessity for direct hardware access, and frequent updates complicating maintenance.
More recently, our focus has shifted towards the development of cross-platform applications. We have been actively testing the capabilities and exploring the limitations of Kotlin Multiplatform (KMP).
Why Did We Choose KMP (Kotlin Multiplatform) Framework?
1. Developed by the established software company JetBrains
In the beginning, one significant advantage we perceived was that KMP (Kotlin Multiplatform) is a framework developed and maintained by a large company. The platform was introduced at the KotlinConf conference in 2017 and has been actively developed since then. Since 2020, there has been a noticeable shift towards focusing on code-sharing between iOS and Android platforms.
2. Offers many flexible options
KMP (Kotlin Multiplatform) allows us to customize the application's architecture to meet the project's specific needs.
Shared UI Multiplatform App
This approach aims to maximize code sharing by implementing shared UI using Compose Multiplatform and sharing business logic and data handling code across platforms.
Native UI Multiplatform App
This approach allows for creating native UIs per platform with a focus on maintaining the natural appearance of the application for a given operating system. Native UI is implemented using Jetpack Compose and SwiftUI. Code for business logic and data handling is shared across platforms.
Multiplatform Library
The final approach is optimal when there is a requirement to share specific logic across multiple target platforms.
3. Easy integrated with existing projects
A significant benefit of KMP is its ability to facilitate a gradual transition to a shared codebase within existing projects.
4. Utilizes Kotlin
KMP employs Kotlin, a language that has gained substantial popularity primarily among Android developers. We have been utilizing Kotlin for several years and value its practical design, which promotes concise code—despite minor limitations, such as the absence of a ternary operator. Kotlin effectively addresses many of Java's shortcomings and maintains readability, making it accessible even to those who do not regularly use it, such as iOS developers.
5. Strong community support
A key attribute of any successful project is robust community support. KMP benefits from an actively growing array of libraries written in Kotlin, reflecting a positive trend in community engagement.
Project Catalog
JetBrains provides a list of inspirational projects available at JetBrains Multiplatform Samples (source).
Notable examples include:
Image Viewer: An ideal starting point for exploring KMP, this fully functional app features a shared UI across all platforms [GitHub].
People in Space: Demonstrates KMP's versatility beyond mobile phones and tablets, such as applications for WearOS [GitHub].
Github
For a comprehensive overview of projects utilizing KMP, visit KMP Awesome on GitHub (https://github.com/terrakok/kmp-awesome). This resource includes well-known libraries from native Android development like Ktor and SQLDelight.
Stack Overflow
The Kotlin Multiplatform tag on Stack Overflow includes over 1,577 questions, indicating a highly responsive and knowledgeable community. Questions in this category are rarely left unanswered, ensuring support is readily available.
6. Extensive documentation and guidelines
The KMP documentation is comprehensive, well-maintained, and highly organized, providing essential support for developers. Additionally, JetBrains offers a project template generator, which can be accessed at KMP Template Gallery. This tool allows developers to easily create various types of projects tailored to specific needs.
7. Diverse build targets
KMP supports a wide range of build targets, enabling the development of applications across key platforms. These include: native Android applications, native iOS applications, Web applications (WebAssembly), Desktop aplications (JAR), and server applications.
8. Used in major existing projects
KMP (Kotlin Multiplatform) is an established technology adopted by large companies for successful projects. Notable examples include:
9. Robust tooling
For developing Kotlin Multiplatform Mobile (KMM) projects, developers can utilize Android Studio and Fleet:
Android Studio: This well-established IDE supports KMP through a dedicated plugin, providing a familiar environment for mobile app development. It offers comprehensive development tools such as a debugger, profiler, network inspector, and database inspector. By default, it supports building applications for Android and iOS; for other targets, developers utilize console and Gradle commands.
Fleet: JetBrains' next-generation IDE is under development and supports KMP projects directly through its user interface, without the need for additional plugins. All build targets are readily accessible. However, testing on Windows has revealed significant issues, including multiple NullPointerExceptions on startup and failure to regenerate resources when modifications are made.
What Were We Testing?
To assess our capabilities, we developed a simple application tailored to common requirements from client projects.
Requirements:
The application was designed to:
persistently stores user data
communicate via a REST API
enable real-time communication through WebSockets
support dark mode and other display preferences across various devices, including those with large screens
We aimed to build this application for as many platforms as possible to maximize compatibility and reach.
Development phase:
For development, we chose the newly released IDE, Fleet.
Libraries:
The first challenge involved selecting viable alternatives to the libraries we traditionally use. The application's foundation relied on the following libraries:
Ktor: Used for network communication.
Decompose: A screen navigation library.
SQLDelight: Employed for database management.
Coil: An image-loading library.
Architecture: We opted for a simple architecture rather than a multi-module approach typically used in standard applications.
UI Layer:
The UI layer utilizes the Compose Multiplatform framework, posing the smallest challenge as its implementation is nearly identical to that in native Android development.
Decompose proved exceptionally useful, providing all necessary classes for native development such as Component -> ViewModel and classes required for creating a navigation graph.
Domain & Data Layer:
The Domain and Data layers heavily relied on Ktor and SQLDelight:
Ktor is a reliable and well-tuned library with intuitive usage, commonly employed in many Android projects. It supports HTTPS and WebSockets.
SQLDelight operates differently from standard ORM libraries, focusing on .sq files where database objects and operations are defined. For instance, here is a snippet from a file defining the Player object used in our application, from which Kotlin data classes are generated.
CREATE TABLE player (
name TEXT NOT NULL,
token TEXT,
avatar TEXT NOT NULL
);
selectPlayer:
SELECT * FROM player LIMIT 1;
insertPlayer:
INSERT INTO player (name, token, avatar) VALUES (?, ?, ?);
deletePlayer:
DELETE FROM player;
What Have We Learned?
Technology readiness:
KMP is production-ready, and it's feasible to create applications for mobile systems like Android and iOS, as well as desktop applications in the form of a JAR file.
Development efficiency:
The development phase was rapid and did not require significant additional time to learn the new technology.
Build time considerations:
However, a notable difference was observed in the build time during the initial project setup. Even a relatively powerful machine, such as the MacBook Pro with an M1 Pro processor and 32GB of RAM, experienced delays.
Ongoing development:
KMP is undergoing rapid development. During a three-week testing period, new APIs for accessing resources and a preview feature that allows views to be rendered without launching the application were added. Support for navigation, currently missing, is already planned in the roadmap.
Our Conclusion
Development efficiency:
The overall time required to develop the application was comparable to that of a native mobile application for Android; however, the output was a cross-platform application. For an Android developer, transitioning to KMP feels very natural. Even for iOS developers, adopting this technology does not represent as significant a change as switching to Flutter or React Native might.
Community support and code simplicity:
One of the greatest advantages we found with KMP is the simplicity of the code and the substantial support from the community. These factors make KMP a compelling choice for developers familiar with Kotlin.
Areas for improvement:
Despite its many benefits, KMP still has room for improvement. Continuous development and community feedback are shaping its evolution, enhancing its functionality and usability over time.
Future plans:
We plan to integrate KMP into existing projects soon to unify the business layer across platforms. Our testing reassures us of KMP’s reliability, and we are confident in using it for entirely new projects without concerns about future viability.
Risk assessment:
KMP is a relatively low-risk technology choice. Should we decide to revert to native development, a large portion of the code developed for Android in KMP can be reused, mitigating potential losses.
Suitability:
Is KMP suitable for every type of project? No, but that is true for any development approach. While there is currently no one-size-fits-all solution for all project types, having KMP as an option is reassuring given its capabilities.