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

Nikolay Igotti

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:


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

Comments below can no longer be edited.

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

  1. Amr Eniou says:

    December 19, 2017

    I wonder if that’s mean kotlin native could now make high-performance cross apps than react native?

    • Nikolay Igotti says:

      December 19, 2017

      You may try and see, how well it behaves for your usecases.

    • Andrew says:

      December 20, 2017

      Not exactly, but now you can share business logic between multiple platforms and use RN as mere glue

      • Marcel Bradea says:

        January 9, 2018

        So long as your business logic is not tied to any platform-specific dependencies, for example your entire model layer using Realm and thus subclassing off of iOS/Android-specific subclasses… making the entire promise of shared business logic moot.

        The world has tried going down this route before (hello, Xamarin?), little do we seem to learn.

  2. Bruno says:

    December 19, 2017

    Hey, does it support passing a lambda/closure from Swift to Kotlin and vice-versa? Thanks

  3. Valentin Slawicek says:

    December 19, 2017

    Amazing, really looking forward to the Swift interoperability 🙂

  4. Kevin says:

    December 19, 2017

    I tried to run the sample but got this error when build

    KtFile: Parser.kt
    exception: java.lang.Error: No such file or directory

  5. kotlinikov says:

    December 20, 2017

    tl;dr How do you deal with kotlin native code being called from different platform threads?

    I did read in the comments of v0.4 release that object heaps are not shared between threads and explicit ownership has to be transferred. Now in v0.5 the calculator shows a framework implemented in Kotlin called from objc/swift, but there are no thread jumps involved. In the past I’ve tried to use a language with similar memory characteristics unsuccessfully in the following scenario:

    Main UI thread calls framework method to initialise some globals. As such, these globals are attached to the UI thread.
    User clicks a button to perform a long operation. iOS side shows a HUD, switches to a background thread and calls a framework method to do calculations. This method requires modifying previously initialised globals.

    In this scenario, the global state initialised early is not accessible from the different background thread and the language crashed. How does kotlin/native work in this situation? Will the background thread crash in the kotlin code presuming to access those early globals/state?

    • Nikolay Igotti says:

      December 20, 2017

      Current Kotlin/Native globals are thread local, so no crash will happen, but other thread init will not be seen. However, this model may and likely will change in future releases. To see how relatively complex MT code could be written, see video player example written in Kotlin/Native: https://github.com/JetBrains/kotlin-native/tree/master/samples/videoplayer

  6. Ildar Sharafutdinov says:

    December 20, 2017


    is it possible to use coroutines in kotlin-native?

  7. Vasilis says:

    December 20, 2017

    You probably get asked about this a lot but are there any plans for integrating Kotlin/Native with IntelliJ? Thanks.

    • Nikolay Igotti says:

      December 20, 2017

      Plugin for CLion is available. IDEA mostly targets VM/managed runtime development, not native one.

  8. Geordie J says:

    December 22, 2017

    Hi, this is very interesting. We are running Swift code on Android and use Kotlin to develop the JVM/Dalvik part. Is it possible to use this interop when the Kotlin code is not running “natively” itself? Or do we need to continue to use the JNI?

    • Nikolay Igotti says:

      December 22, 2017

      Kotlin will run natively in both cases, actually. On Android your Kotlin code will compile to Java bytecode, and then to DEX (https://source.android.com/devices/tech/dalvik/dex-format) and then will be executed by VM. On iOS, your Kotlin code will compile into machine code and as a framework could be used on device directly.

  9. Fabian says:

    December 23, 2017

    Awesome project, will try it out soon.
    Is there any integration into AppCode?

    • Nikolay Igotti says:

      December 25, 2017

      We are working in this direction, stay tuned.

  10. Fabian says:

    December 23, 2017

    It there bridging between data classes and the Codable protocol in Swift?

    • Nikolay Igotti says:

      December 25, 2017

      Not yet.

  11. Ildar Sharafutdinov says:

    December 23, 2017


    any ETA on beta/1.0 version?

    • Nikolay Igotti says:

      December 25, 2017

      Not yet, however, being v0.x is mostly an internal thing, in general both compiler and runtime are pretty well tested and known to work on rather complex codebases, such as
      Video Player: https://github.com/JetBrains/kotlin-native/tree/master/samples/videoplayer

      • Ildar Sharafutdinov says:

        December 26, 2017

        But I guess, there is a sort of roadmap for the nearest releases. Is it published anywhere?

        I’m particularly interested in 2 things: persistent storage and network layer.
        Although there are samples with wrappers around sqlite and libcurl, are there any plans to ship them with kotlin native?

        • Nikolay Igotti says:

          December 26, 2017

          There are two possible dimensions to your question:

          • to just have persistent storage and network layer in the pure K/N program platform libraries on most targets are sufficient
          • to have a portable layer for persistent storage and network layer (and few other things) on all Kotlin flavours (JVM/JS/Native)
            there’s an ongoing activity in libraries direction
  12. kangmo says:

    December 26, 2017

    Thanks for the fast and frequent release cycle!
    As a big fan of Kotlin, I am evaluating Kotlin/Native for my project.

    BTW, do you have any plan to use Clang distributed with Microsoft Visual Studio 2015 community edition for generating binaries?

    Another question:
    Can I link object/library files produced by (1) Mingw(by Kotlin/Native) and (2) visual studio native c++?

  13. Brandon Lamb says:

    December 28, 2017

    I am interested in the idea of Kotlin/Native integration with Godot Engine (godotengine.org) as a GDNative language (native bindings for c++, c#, nim, rust, etc).

    I would love to use Kotlin for game scripting

    • Steve Thompson says:

      January 9, 2018

      Brandon, I would love to hear of your progress in this direction, as I’m starting to get into Godot and am very impatient for 3.0 to come out. While I’ll probably do some scripting through Nim, I think Kotlin would also be very interesting to experiment with.

  14. Solomon Sun says:

    December 30, 2017

    Thanks for the fast and frequent release cycle! +1

    Most common memory leak trap in Swift:

    I found above trap, caused by Swift ARC. Should we worry about the cyclic reference trap in Siwft-Kotlin interop ? Should we modify kotlin code with keywords like unowned/weak ?