Kotlin/Native v0.4 released: Objective-C interop, WebAssembly and more
We’re happy to announce the release of Kotlin/Native v0.4, KotlinConf 2017 edition! This release adds support for accessing Objective-C APIs on iOS and macOS, WebAssembly target platform, as well as introduces major changes making app development in Kotlin/Native way easier.
To enable access to underlying operating system interfaces, Kotlin/Native provides a set of platform-specific libraries available to any program targeting a particular platform. Previously, you needed to use the cinterop tool to generate the libraries yourself, and now they’re available out of the box.
The following program demonstrates the use of the new platform libraries in the v0.4 release.
It will read the contents of a file into a Kotlin
ByteArray and then will print it to standard output. So now the full power of operating system interfaces is at your hands without the need of explicit interop stubs generation.
Note that we can use Kotlin objects for storing a result of platform call
fread, see below in ‘Object Pinning’ section.
iOS and macOS framework interoperability
On Apple platforms, unlike most other platforms, access to system frameworks is provided in the form of Objective-C APIs. To support that, the Kotlin/Native team has implemented an Objective-C interoperability layer. For example, the following code, written in pure Kotlin/Native, will read an application’s resource on iOS:
And the following complete program will render a top-level window on macOS. Note that the window title is in Cyrillic, to show that we perform correct character set conversion in the interoperability layer.
See Kotlin/Native fullstack application iOS client for an example of a complete application for iOS. If you own an Apple or Android device, feel free to see this application in action on App Store and on Google Play.
To simplify using Kotlin objects with C APIs we provide new APIs for typed arrays (
FloatArray etc.), namely
unpin(). See Pinning.kt. They allow to ensure an object is locked in memory, and its data has a stable address, thus allowing to use Kotlin object data directly from C APIs and vice versa. For example, in
readFileData() function above we pass pointer to data in
Debugging with release v0.4 adds improved inspections, so most variables could be inspected in runtime. For example, if we take the program to read a file from the first section, and compile it with the debugging support (note
-g switch), we can perform symbolic debugging and variables inspections.
# # bin/konanc kat.kt -o kat -g # lldb kat.kexe (lldb) target create "kat.kexe" Current executable set to 'kat.kexe' (x86_64). (lldb) command script import tools/konan_lldb.py (lldb) b kfun:main(kotlin.Array<kotlin.String>) Breakpoint 1: where = kat.kexe`kfun:main(kotlin.Array<kotlin.String>) + 26 at kat.kt:25, address = 0x00000001000023ba (lldb) print args (ObjHeader *) $1 = [/etc/groups]
Kotlin/Native v0.4 have an experimental support of WebAssembly (
-target wasm32). WebAssembly target is mostly intended to showcase the technology, as it is still not fully production ready due to browser support limitations (mainly around more seamless DOM/HTML5 APIs access and performance of virtual machines executing WASM). However, we’re very interested in feedback from our users, to know how much interest in WASM support is there.
Last, but not the least, we have recently announced an experimental CLion plugin supporting Kotlin/Native, see this post for more details.
Getting the bits
Subscribe to Blog updates
Thanks, we've got you!
Kotlin 1.9.20 Released
Kotlin 1.9.20 has been released! Highlights include: Kotlin K2 for all targets is in Beta, and Kotlin Multiplatform is Stable. Learn more!
Kotlin 1.9.0 Released
Kotlin 1.9.0 has been released! Highlights include: Kotlin K2 for JVM is in Beta, other K2 compiler updates, new enum class values function, new operator for open-ended ranges, preview of Gradle configuration cache in Kotlin Multiplatform, changes for Android target support in Kotlin Multiplatform, preview of custom memory allocator in Kotlin/Native. Learn more!
Introducing Kotlin Notebook
The first experimental version of the Kotlin Notebook plugin for IntelliJ IDEA is now available! This post will offer insight into the plugin’s functionality and use cases, but before launching into that, we’d like to let the plugin speak for itself: https://youtu.be/2PLYlDJPelQ W…