iOS Multiplatform Native Releases

Kotlin/Native v0.5 released: calling Kotlin from Swift and C, LLVM 5 and more

We’re happy to announce the release of Kotlin/Native v0.5, Christmas edition! This release adds support for using Kotlin/Native code from C, Objective-C and Swift, supports development using iOS simulator, along with LLVM 5 support and creating WebAssembly from Linux and Windows hosts.

Reverse interop from Objective-C and Swift

In the previous release of Kotlin/Native we introduced calling Apple frameworks from Kotlin/Native, assuming they provide Objective-C headers. Now we go another way around, and add support for calling Kotlin code from Swift and Objective-C. For that, a new compiler option, -produce framework, has been implemented. It generates a self-contained framework, which could be used from other parts of your application, as if it was written in Swift. Let’s take a look at the calculator example. It has UI written in Swift along with calculator logic written in Kotlin. Swift code intermixes with Kotlin transparently. For example, this line of Swift code:

private let parser = KAPPartialParser(composer: KAPCalculator(), partialComposer: PartialRenderer())

creates an instance of the Kotlin class PartialParser, and gives it an instance of a Swift class PartialRenderer implementing a Kotlin interface ExpressionComposer.

Note, that basic types like numbers and strings are transparently mapped between Swift and Kotlin worlds.

To build the project, just open it in XCode and compile it for either a real device or the simulator, see the README.md for details.

Screen Shot 2017-12-18 at 18.40.21

And Kotlin code in IntelliJ IDEA:

Screen Shot 2017-12-18 at 19.44.38

Reverse interop from C

For other platforms we also support reverse interoperability, allowing to call Kotlin/Native code from the outside world. The lowest common denominator of modern languages is C, so this was the language we interoperate with. Compiler option -produce dynamic produces a dynamic library (such as .dylib on macOS, .so on Linux and .dll on Windows) containing everything needed to work with Kotlin/Native code. To make things fancy we decided to demonstrate this interoperability by creating Python extension. Python calls to C implementations and they call Kotlin implementation like this:

if (PyArg_ParseTuple(args, "Lss", &session_arg, &string_arg1, &string_arg2)) {
       T_(Server) server = getServer();
       T_(Session) session = { (void*)(uintptr_t)session_arg };
       const char* string = __ kotlin.demo.Server.concat(server, session, string_arg1, string_arg2);
       result = Py_BuildValue("s", string);
       __ DisposeString(string);
    } else {
        result = Py_BuildValue("s", NULL);
    }

The Kotlin/Native compiler produces a dynamic library, and then Python distutils build tool produces another dynamic library, depending on that one. So Python launcher code calls Kotlin/Native objects via C bridge and gets both object and primitive types properly converted.

Other improvements

  • In Kotlin 1.2, the kotlin.math package was added to the Kotlin standard library. With v0.5 Kotlin/Native supports operations available in kotlin.math package as well
  • LLVM 5.0.0 is supported with this release, as both clang toolchain and bitcode codegenerator and optimizer
  • Code for WebAssembly targets (-target wasm32) now can be produced from Linux and Windows hosts (before only macOS hosts were supported)
  • Workers API was improved by allowing easier consumption of worker execution result and adding ability to pass primitive values from and to worker
  • Bugfixes and improvements

Getting the bits

Binaries could be downloaded below:

Feedback

Please report bugs and issues in the Kotlin bug tracker. Questions are welcome on #kotlin-native channel on Slack (get invite here).

image description