Considering reusing Android code for iOS?

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

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. The latest survey on Stack Overflow reveals Kotlin with 72.6% as one of the most loved languages after Rust, Python and TypeScript.

  • Rust 83.5% 83.5%
  • Python 73.1% 73.1%
  • TypeScript 73.1% 73.1%
  • Kotlin 72,6% 72,6%

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 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 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 “build” button on shared module in Android Studio. After library files are build we can import them and use in Android Studio and XCode projects.

It is rapidly rising and there are already plenty libraries supporting Kotlin Multiplatform like HTTP requests library, JSON serialization, asynchronous operations, logging etc. All this libraries uses specific implementation for specific platform and supports 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. 

    Further research
    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 in 2019

    • 2,2 million users
    • Every year the number of Kotlin users doubles. By the end of 2019, we expect 4 million users 
    • 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

    Source: kotlinlang.org

     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 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 makes 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.

    Get in touch
    Is your company looking for a software development partner? Drop us a line at