Kotlin Native introduces Objective-C interoperability and WebAssembly support
According to JetBrains Technical Director Nikolay Igotti, Kotlin/Native 0.4 can be used to develop Native applications for iOS and macOS. This version also provides experimental support for the WebAssembly platform.
Kotlin/Native supports iOS/macOS development. The key lies in implementing interoperability with Objective-C. JetBrains has released a very simple demo application, which is written in Kotlin and released to Apple Store and Google Play.
The following code snippet shows how to access the iOS framework:
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())}
All of this is through the newplatform
And access to the underlying operating system interfaces through the library. This allows you to use a platform that provides the posix OS layer to perform various operations, such as callingfopen
,fread
. Previously, Interop Stub was specially generated for interoperability with the underlying platform.
Another new feature of the new version, Object pinning, aims to simplify the use of Kotlin objects through the c api, so that the objects consumed through the c api can be locked into the memory.
Kotlin/Native now supports WebAssembly, so you can use Kotlin to develop browser-based applications. However, according to Igotti, due to browser support limitations, such support is still experimental.
In terms of tools, debugging now supports checking most variables at runtime. In addition, it is also important to use a new plug-in to allow CLion to support Kotlin/Native.
Kotlin/Native is the latest achievement of Kotlin and can be used to compile Kotlin, a JVM-based language, as a Native library that can run without a virtual machine. Therefore, this technology is suitable for environments that cannot or cannot run virtual machines, such as iOS platforms and embedded devices. Kotlin/Native currently supports Windows, Linux, macOS, iOS, Android, and WebAssembly.
Kotlin Native Adds Objective-C Interop, WebAssembly Support