Zillow Tech Hub

Sharing Mobile Application Code Across Platforms with Kotlin Multiplatform Mobile for GraphQL Queries

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:

  1. With the move to KMM, we created a separate repo for our KMM code, referenced by our iOS and Android application repos as an external dependency. Developers working on changes to shared code in the KMM repo will do so in their local development branch.
  2. When changes are ready to take effect for testing (before being merged or potentially before being pushed) they will run a manual publish step in order to publish artifacts to a local repository. This step will create build artifacts from that repo based on the commit hash; this version is temporary and separate from the normal semantic version.
  3. Then the developer will be able to update their main app repo to point to this temporary version, validating the changes or performing development on the app repo that depends on the KMM repo changes.
  4. Once the KMM changes are merged into main, the actual semantic version can be incremented, and the KMM changes will be deployed as artifacts to Artifactory. This new semantic version will include their changes and the app repo will be updated to point to this new semantic version.

For example, an engineer might check out the KMM repo on version 1.5.0, make changes in their local development branch, and publish those to generate version ‘3a7f9b2d45e84dab886b0ee3c001f82f7e3c109a’. Then they could use this temporary version in their app development branch until their KMM changes were merged into main. Once the KMM changes were merged into main, the version would be incremented to 1.6.0. This would be the next official semantic version, which would include their changes. The app repo would then be updated to point to 1.6.0.

Here are a few of our observations and experiences from implementing new and existing GraphQL queries in KMM:

 We encountered a number of issues that weren’t apparent at the outset of this project: 

Exit mobile version