Write Kotlin, run anywhere

Vivekanandan Sakthivelu
Toyota Connected India
4 min readAug 19, 2020
Image credits: https://www.jetbrains.com/lp/mobilecrossplatform/

In the world of mobile application development, developers have built frameworks trying to reuse the code across the platform thereby reducing the development cost. We have been trying to make the apps run within the browser context, rendering UI from binaries without any runtime, transpiling code from one language to another, sharing both user interface and domain/business logic. Performance has never been a concern in cross-platform development as long as your programming skills are up to snuff, but support to access and platform utilization is limited. Also, you cannot have fancy home screen widgets, PIP mode, multiple background mode, and support for wearables.

Shared UI is a history of pain and failure. Shared logic is the history of computers. -Kevin Galligan

Kotlin MultiPlatform Project aims to share the code among almost all the platforms (JVM, Android, JavaScript, iOS, Linux, Windows, Mac), which is an abstraction of compiler technology that does both source-to-source translation and source-to-target translation. Kotlin MPP (MultiPlatform Project) helps to build SDKs from a joint code base targeting different platforms, as well as building SDKs from a single code base. Let’s take a deep dive into Kotlin MPP & Native and understand how Kotlin ships the code for Android and iOS as one of the use cases. Koltin MPP builds .jar, and .framework from the system that opted for sharing across the platforms.

Image credits: https://www.jetbrains.com/lp/mobilecrossplatform/

For Android:

It’s pretty straight forward. All we will have to create is a new module aka Mini app, and add it as a dependency in the app module.

build.gradle (Module: app)

For the projects which are still in Java, adding Kotlin plugin and dependencies to Gradle files could make it interoperable to access the shared code.

Since Kotlin produces the same bytecode as Java and is interoperable, we can include the code in the main module or wherever required by building a jar out of it.

But how Kotlin ships to iOS and other platforms ??

Kotlin is not the first one in the league. Go-lang offers a go-mobile package to share code between Android (.aar) and iOS (.framework). Slack and Dropbox have been doing this for a decade by writing their SDKs in C++. Flutter ships x86, ARM, and MIPS native binaries with the header for accessing it. Kotlin Native project helps to convert the code written in Kotlin into native binaries.

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 and native implementation of the Kotlin standard library.

Kotlin to Native binaries

Kotlin Native acts as a backend for the compiler generating an intermediate representation used by LLVM to produce platform-specific binaries. These binaries are more often packed into .o or .so files, depending on the targeted platform. Now, Kotlin MPP generates header files with appropriate platform-specific bindings (For JS, Kotlin code is transpiled into Javascript code) and packs it into a structure (.jar, .arr, framework, .js) that can be included in the mainstream platform development.

build.gradle.kts ( Module: sharedsdk )

Executing the packForXcode task generates a framework file under the build/Xcode-framework of the shared module.

sharedsdk.framework

It’s not always the case you would want to share standard code among platforms, sometimes you need to access native API and types. Kotlin MPP is not about compiling all code for all platforms; it has limitations. Applications need access to unique features of the platforms that they are running. Kotlin native provides interoperability with C, Objective C, and Swift other than Java. Any platform-specific code called out as expect in commonMain, which is implemented in different platforms.

Kotlin MPP

Kotlin MPP — Alpha(1.3) is out and works well with the stable release of core components (Kotlin/JVM, Kotlin/JS, Kotlin/Native). Soon we can expect more such stable releases and we can hopefully migrate to the common code base. 😉

References

--

--

Vivekanandan Sakthivelu
Toyota Connected India

Conscious-being stuck in space-time continuum, who find solace in Kahlil Gibran writings. Sometimes I code,write;othertimes I enjoy melodrama of my own thoughts