BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Kotlin Native Adds Objective-C Interop, WebAssembly Support

Kotlin Native Adds Objective-C Interop, WebAssembly Support

Bookmarks

Kotlin/Native 0.4 makes it possible to build native apps for iOS and macOS, writes Nikolay Igotti, Kotlin/Native tech lead at JetBrains. Additionally, it introduces experimental support for the WebAssembly platform.

Objective-C interoperability is the key to Kotlin/Native supporting iOS/macOS development. JetBrains has gone so far as publishing a very simple demo app written in Kotlin both on the Apple Store and Google Play.

This snippet shows how you can access iOS frameworks:

import kotlinx.cinterop.*
import platform.Foundation.*
import platform.objc.*
import platform.osx.*

fun readResource(resourceName: String): ByteArray {
    val filePath = NSBundle.mainBundle.pathForResource(resourceName, ofType = null)
  val fileData = NSData.dataWithContentsOfFile(filePath!!)
  ?: throw Error("failed reading resource $resourceName")
  return fileData.bytes!!.readBytes(fileData.length.toInt())
}

The magic is provided here by the new platform. libraries, which enable access to underlying operating system interfaces. This makes it also possible to use the POSIX OS layer on a platform that provides it, e.g., to call fopen, fread, and so on. Previously, interoperability with the underlying platform would require the explicit generation of interop stubs.

Another new feature, aimed to simplify the use of Kotlin objects with C API, is object pinning, which can be used to ensure an object is locked in memory when consumed from C APIs.

Kotlin/Native now supports WebAssembly, which means Kotlin can be used for browser-based apps. Support is only experimental, due to browser support limitations, says Igotti.

On the tooling front, debugging now supports inspection on most variables at runtime. And, last but not least, a new plugin provides support for Kotlin/Native in CLion.

Kotlin/Native is a recent development in Kotlin that allows to compile Kotlin, originally a JVM-based language, to native binaries that run without any VM. This makes it especially suitable for platforms such as iOS and embedded targets, where VMs are not allowed or desirable. Kotlin/Native currently supports Windows, Linux, macOS, iOS, Android, and WebAssembly.

Rate this Article

Adoption
Style

BT