Share code in existing Android and iOS apps with Kotlin Multiplatform Mobile

An effective and simple approach to add benefits of cross-platform development to your existing native app projects

NanoGiants
Published in
5 min readDec 18, 2020

--

In the end of August JetBrains announced that Kotlin Multiplatform Mobile (KMM) reached alpha status. Their new framework reinvents cross-platform development, as it is now possible to share code between already existing native apps with almost no cost.

There has always been a dilemma when it comes to mobile development. Android and iOS both have their specific SDKs, tools and programming languages which are not compatible with each other. Developers have to choose between maintaining two separate native applications or using frameworks to combine the development process for both platforms and deal with their limitations and compromises. A prime example illustrating this problem is the company Airbnb, which switched during the last years from native Android and iOS development to the framework ‘React-Native’ and finally back to native development (see AirBnb, sunsetting React-Native ).

With Kotlin Multiplatform Mobile JetBrains offers a new promising solution which is fundamentally different from the variety of already existing cross-platform approaches. The idea of KMM is very simple: to provide the best possible user experience and performance, the UI should be native and specific to each platform. Moreover, this ensures that you have access to the latest system features (e.g. darkmode, widgets) from the very first day they were released. However, apart from the user interface and device hardware, most parts of mobile applications do not depend on the underlying Android or iOS operating system. For Example, suppose you develop the game ‘Tic Tac Toe’. All the classes representing the game rules, a player, allowed actions and board states (often referred to as entities, use-cases or domain models) are the same for each platform. Accordingly, KMM allows you to write this business logic once in Kotlin and compile it to native binaries for each platform. This reduces the time you spend developing and maintaining the same code twice, even though you still use native technologies.

To be clear, KMM does not solve the mobile dilemma for everyone. For example, apps with almost no business logic or heavy use of device specific APIs do not really benefit from this approach. Moreover KMM is still in alpha status and not every possible use case will be supported currently. Also it should be mentioned that this concept is not completely new. Indeed, some years ago Dropbox tried a similar approach called Djinni but stopped their project because of ongoing difficulties that were too hard to handle. In summary they had problems finding as well as keeping developers and there was great effort necessary for custom frameworks, a custom development environment and the differences between mobile platform. However, JetBrains provides promising solutions for these challenges. They use a programming language that is already used in the field of app development with special features such as “actual/expect” for platform differences, they are experts in the development of IDEs and already offer an impressive variety of third-party libraries for KMM and Kotlin/Native.

Reusing code between already existing Android and iOS is easy

The effort to use KMM in your existing native app projects is very small. You still use Android Studio and Xcode, so there is no need to learn any new tooling. Android developers are already familiar with Kotlin and, because of its similarity to Swift, iOS developers can understand and learn the programming language easily. Moreover, Kotlin Multiplatform is fully interoperable to your already existing codebases. It is not necessary to rewrite any of your existing functionalities. This gives you the flexibility to use KMM only for an upcoming new feature and refactor parts of your business logic to Kotlin at some time in the future. So, the migration to KMM is an overall low risk process with minimal time and financial expenditures. This will encourage developers and stakeholders to actually try out KMM and it is likely that the community will grow during the next years making Kotlin Multiplatform even more powerful.

Getting started

The following steps are necessary to use KMM with your existing Android and iOS apps. Our goal is to have only one single git project containing both native apps as well as their shared logic. This is the most common and recommended project structure for KMM applications because it facilitates the maintenance of shared resources such as strings (see twine) or assets.

Step 1: Merge the iOS and Android repository

First, create a new empty git repository which will later contain our final mobile project. To preserve your git history and avoid merge conflicts, proceed as follows: Add your Android repository as a remote, pull its master branch and rename the folder “app” to “android”. Go into your existing iOS-project, move all files into a single folder “ios” and commit the changes. Afterwards add your iOS repository as a remote, pull and merge its master branch. Finally you can remove all remotes from your mobile project again.

Step 2: Create a KMM shared module

Open the new mobile project in Android Studio. Use Android Studio (at least Version 4.1 with KMM-Plugin installed) to create a new KMM module. For that, go to Module Settings -> Add -> KMM Module and make sure “generate packToXcode” is checked. Now the project should have the following directory structure.

Project directory structure

To tell the KMM plugin the location of your iOS project, add the following lines to the gradle.properties file.

This allows you to start an iOS simulator in Android Studio. Finally perform a build of the project, this should create a .framework file in the directory shared/build/xcode-frameworks.

Step 3: Configure iOS Project in Xcode

We need to tell Xcode the location of our shared code. Open the new iOS project in Xcode, go to your Target iOS -> General, under Frameworks, Libraries, and Embedded Content select add and then add other -> add files and add your framework here (normally located under shared/build/xcode-frameworks).

Also Xcode needs to be able to rebuild the framework whenever there have been changes. Go to your Target iOS -> Build Phases and add a new phase after Dependencies with the following run Script:

Step 4: Build great multi-platform features

The shared module contains the class Greeting we can use to validate that everything works correctly per default. It provides different actual implementation for each platform. You are now able to instantiate this class in the Android and iOS app code to display or log the string.

Have a look at the official documentation to develop your first feature with KMM, helpful resources I recommend are:

Conclusion

Kotlin Multiplatform Mobile allows you to share native code between your existing Android and iOS applications. As I showed in this article, the effort to use the technology in your projects is minimal. Therefore, I am convinced we will start to see KMM develop, becoming an important part of native app development in the next few years.

Is Kotlin Multiplatform Mobile an option for your app projects?

--

--