I was curious about Kotlin Native and wrote a hello, world program that targets macos. To make things a little more interesting I’ve also introduced ncurses to render a simple implementation of the game of life.

Here’s the result:

hello-kotlin-native

The project is using the multiplatform gradle plugin. I’ve only kept the macos target.

macosX64("macos") {
    binaries {
        executable {
            entryPoint = 'com.cybergstudios.dlife.main'
        }
    }
}

Generating the ncurses bindings was also straightforward. Integrating cinterop in the build system took only a few lines.

macosX64("macos") {
    // ...
    compilations.main {
        cinterops {
            ncurses {
                defFile project.file('./src/nativeInterop/cinterop/ncurses.def')
                compilerOpts '-I/usr/local/opt/ncurses/include'
            }
        }
    }
}

The def file contains the rules on how to build the kotlin ncurses bindings.

headers = ncurses.h
headerFilter = *

staticLibraries = libncurses.a
libraryPaths = /usr/local/opt/ncurses/lib

ncurses was installed using homebrew

brew install ncurses

Overall kotlin native looks promising. The multiplatform feature is experimental but bootstrapping a simple project was not complicated and the documentation was very good if not complete.

I’ll explore next how the C constructs are expressed in kotlin.