Sharing Mobile Application Code Across Platforms with Kotlin Multiplatform Mobile for GraphQL Queries
Engineer's Planet
?? Unlock the Engineer's Multiverse ??Enhance Your Knowledge, One Smart Bit at a Time!
Since the release of the first smartphone, mobile teams have faced some common challenges:
These challenges are often related — differing implementations across clients means two or three times as much code needs to be written, leading to double or more the chances for bugs to arise, compared to. developing on a single platform. This difference in platforms also leads to more time spent coding, as developers create similar code against each client for every change. Furthermore, developing for multiple platforms requires specialized knowledge about different languages and technologies.
Unfortunately, the Premier Agent mobile team has not been immune to these challenges. In this blog post we’ll discuss our journey into sharing code across platforms using Kotlin Multiplatform Mobile (KMM), the difficulties we encountered, the lessons we learned along the way, and the steps we’ll take toward streamlining our development process.
For those unfamiliar with KMM, it enables the sharing of code logic across multiple platforms — including Android, iOS, and more — in the Kotlin language. By enabling the creation of platform-agnostic modules, KMM allows developers to write and maintain a single codebase, typically used in the data and network layers, as well as for business logic. This reduces redundancy, eases maintenance, promotes code reusability, accelerates development, and ensures consistent functionality across diverse platforms. Just the thing we’re looking for!
Once we identified KMM as a tool we could use to share code across platforms and tackle the problems mentioned earlier, our next step was to identify a use case. We considered where in our code bases we had code that could most readily be reused, and where doing so would address bugs we’d encountered on one or the other platform due to implementation differences.
This shined a light in the direction of our GraphQL queries, which are typically recreated across iOS and Android. If we could transition these GraphQL queries to KMM, we’d avoid having to write nearly identical queries on both platforms, reduce platform-specific boilerplate, and avoid bugs we’d recently bonked our heads on due to differing queries or expectations of those queries, that our mock server exposed when creating UI tests.
Our workflow
For context, we’ll summarize our architecture & workflow at a high level:
Here are a few of our observations and experiences from implementing new and existing GraphQL queries in KMM:
KMM’s Apollo Client’s is well-documented
Module Makeover
领英推荐
Reusing Build Logic
Apollo Client in KMM
?We encountered a number of issues that weren’t apparent at the outset of this project:?
Merge Mishaps
To avoid similar issues, moving forward we will:
Version Heck
Since switching to KMM for GraphQL queries, we’ve encountered no bugs due to differing GraphQL implementations across platforms, and we have saved the need for dozens of merge requests. Previously, we would need to develop merge requests? for both iOS and Android platforms. Now we can? develop against just one shared KMM repo. At this point in our KMM adoption, we’ve overcome growing pains that accompany working with a separate KMM repo, we’ve found success in the areas we set out to improve, and we have our sights set on pursuing new use cases for KMM.
As we continue with KMM we will explore increasing the amount of shared code beyond the network layer, potentially as far as moving our entire domain layer and the business logic contained therein, to KMM. On the extreme end of things, we could adopt a UI framework like Jetpack Compose Multiplatform, so almost the entire code base could be in the shared repository. This is an experimental framework, so taking such a drastic step could lead to unforeseen, and undesired, consequences (e.g. What if Apple changed something within their ecosystem to make such an approach impossible? We would be stuck re-writing the whole app in swift). Which is why we’re taking small,? incremental steps toward this possibility, and will continue to light our future path with KMM.
By- Himanshu Garg