Porting android code to iOS: kotlin multiplatform

6 min read
November 5, 2019

Considering reusing Android code for iOS?

This article explores the current state of Kotlin Multiplatform and emphasizes why you should consider it over j2objc and C++ in your next mobile app project.

kotlinlogo

Kotlin Multiplatform

Since Google announced Kotlin a priority programming language for building android apps with full support back in 2017, Kotlin enjoyed huge growth in popularity among the developer community. 

Services01

Need a dedicated team for your Android app? —
Here’s how we do it →

The latest survey on Stack Overflow reveals Kotlin with 72.6% as one of the most loved languages after Rust, Python and TypeScript.

Kotlin Multiplatform, the rising star in the multiplatform space, is an experimental feature that allows you to run Kotlin in JavaScript, iOS, native desktop applications and more.

It makes possible code sharing between all these targets and reducing the amount of time required for development. 

Compile code for multiple platforms

Kotlin Multiplatform uses Kotlin/JVM and Kotlin/Native tools to compile code for multiple platforms. Kotlin/Native is a technology for compiling Kotlin code to native binaries which can run without a virtual machine.

It is an LLVM based backend for the Kotlin compiler called Konan and native implementation of the Kotlin standard library.

LLVM is a library that is used to construct, optimize, and produce intermediate and/or binary machine code.

Further, LLVM can be used as a compiler framework, where you provide the “front end” and the “back end” (code that converts LLVM’s representation to actual machine code). 

Share Kotlin code between multiple platforms

Shared code is placed in a common module and is used by all platforms. Sometimes we need to access platform APIs and Kotlin Multiplatform allows you calling platform-specific code at any time through the expect/actual mechanism.

Platform-specific code is placed in platform-specific modules eg. ios, android and is used only by the specified platform. 

Plenty libraries supporting Kotlin

To use Kotlin multiplatform library in XCode or Android Studio project we have to build library files, for iOS .framework files are built and for Android .jar file is built.

Generating library files is straight forward, we just have to click the “build” button on the shared module in Android Studio. After library files are built we can import them and use them in Android Studio and XCode projects.

It is rapidly rising and there are already plenty of libraries supporting Kotlin Multiplatform like HTTP requests library, JSON serialization, asynchronous operations, logging, etc.

All these libraries uses specific implementation for a specific platform and support iOS and Android platform. 

Ktor
Ktor is a framework to easily build connected applications – web applications, HTTP services, mobile and browser applications. Modern connected applications need to be asynchronous to provide the best experience to users, and Kotlin coroutines provide awesome facilities to do it in an easy and straightforward way.

Kotlinx Serialization
Kotlin serialization consists of a compiler plugin, which automatically produces visitor code for classes, and runtime library, which uses generated code to serialize objects without reflection.

Kotlinx Coroutines
Kotlinx Coroutines is library for asynchronous operations.

Napier
Napier is library for logging useful app data. Specific implementation for Android uses Log class and specific implementation for iOS uses NSLog class for logging data.

Multiplatform preferences
Multiplatform Preferences is library for using local storage. Specific implementation for Android uses SharedPreferences class and specific implementation for iOS uses NSUserDefault class.

SQLDelight
SQLDelight generates typesafe APIs from SQL statements. It compile-time verifies schema, statements, and migrations and provides IDE features like autocomplete and refactoring which make writing and maintaining SQL simple. SQLDelight currently supports the SQLite dialect and there are supported SQLite drivers on Android, JVM and iOS.

As part of this document, small library is developed using Kotlin Multiplatform to test how it works. Library contains Math class written in Kotlin which contains functions for simple mathematical operations and can be used both from Android and iOS. 

Also, library is using Ktor library to perform HTTP request and query elephants api.

Querying is written in Kotlin and is 2 running on both iOS and Android. Expect/actual mechanism for running platform specific code is tested also. 

.jar and .framework files are successfully generated from project, imported in iOS and Android project and everything is performing well. 

More research could be done, it would be good to test importing two independent libraries for iOS and Android with same functionality and make them Kotlin Multiplatform supported.

Some facts about Kotlin

  • 2,2 million users
  • Every year the number of Kotlin users doubles.
  • 57% of users use Kotlin in production 
  • For 39% of users, Kotlin is their primary language, which means the main or only one they use at work
Facts about Kotlin


j2objc


J2ObjC is an open-source command-line tool from Google that translates Java source code to Objective-C for the iOS (iPhone/iPad) platform.

This tool enables Java source to be part of an iOS application’s build, as no editing of the generated files is necessary. The goal is to write an app’s non-UI code (such as application logic and data models) in Java, which is then shared by web apps (using GWT), Android apps, and iOS apps. 

When comparing with Kotlin Multiplatform, this technology does not have prons except its maturity.

It is older technology based on Java focused on transpiling code. It could create issues in case there is need of platform specific code as j2objc does not have mechanisms for handling it and it should be done manually, what makes Kotlin Multiplatform better option. 


C++


Android and iOS can both run C++ code. Android offers NDK to support native compiled applications while iOS offers Objective-C++ which is a C++ dialect that can call the Cocoa APIs. 

Creating a shared library between iOS and Android in C++ would take too much time. C++ is older generation language which requires C++ expert, a lot of things should be written from scratch, accessing platform features is not easy and maintenance of C++ library would be much harder and bug-prone.

All of these issues make Kotlin Multiplatform a better solution. 

Although Kotlin Multiplatform has some downsides, it’s definitely worth to have a closer look and consider it in your next project. If you have any questions or feedback, please feel free to get in touch.

Categories
Written by

Ivan Trogrlic

Android Team Lead

An Applied Sciences graduate and a true connoisseur of tech, Ivan is a software developer with a genuine love for exploring new technologies. QAs love his code, and his fellow developers always value his input. For Ivan, there is no issue too small to talk over, and no problem that can’t be solved together. When he is not coding, Ivan is usually hiking or playing football. His ideal workspace? Probably a cottage in the mountains, with a serious gaming setup and fast internet connection.

Related articles