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:

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:

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).

About Nikolay Igotti

Kotlin/Native Tech Lead
This entry was posted in iOS, Native, Releases and tagged . Bookmark the permalink.

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

  1. Amr Eniou says:

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

    • Nikolay Igotti says:

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

    • Andrew says:

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

      • 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:

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

  3. Valentin Slawicek says:

    Amazing, really looking forward to the Swift interoperability :)

  4. Kevin says:

    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:

    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?

  6. Ildar Sharafutdinov says:

    hi,

    is it possible to use coroutines in kotlin-native?

  7. Vasilis says:

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

    • Nikolay Igotti says:

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

  8. Geordie J says:

    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?

  9. Fabian says:

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

  10. Fabian says:

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

  11. Ildar Sharafutdinov says:

    guys,

    any ETA on beta/1.0 version?

    • Nikolay Igotti says:

      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:

        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:

          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:

    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:

    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:

      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:

    Thanks for the fast and frequent release cycle! +1

    Most common memory leak trap in Swift:
    https://medium.com/@sergueivinnitskii/most-common-memory-leak-trap-in-swift-4565dbae5445

    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 ?
    Thank.

Comments are closed.