Last week we announced the availability of the first technology preview for Kotlin/Native, something that we’ve been working on since September 2016.
With this first preview release, you can now compile Kotlin to run natively on various platforms, including
- Mac OS
- Ubuntu Linux (and other Linux flavours)
- Apple iOS (arm64)
- Raspberry Pi
I decided to give it a whirl and see what it takes to get a simple Hello World! running on the Raspberry Pi, which has been sitting in my
desk draw for months now (albeit with a custom-made Lego box)
My device is the Raspberry Pi 2, running NOOBS. No other updates or software installed.
Setting up the compiler
To target Raspberry Pi, we need to compile our applications on a Linux machine, targeting the Pi. In my case I’m using a VM running Ubuntu Desktop 64bit.
First thing is to download the right compiler distribution, in our case Linux. The zip file comes with a command line compiler, Gradle support and some samples.
Given that the compiler requires the JVM, we need to install Java on the Linux distribution. In my case I’m running Java 8.
Once we’ve unzipped the compiler, in the bin folder we have access to three script files
- kotlinc
- kotlinc-native
- konanc
The first two are in fact just proxies to konanc (codename).
Compiling for Linux
To compile the following code to target the platform we have the distribution for (in our case Linux)
fun main(args: Array<String>) {
println("Hello, World!")
}
we simply run the following command from the bin folder
./konanc <path_to_source>/sample.kt -o hello
we can optionally provide an output filename using the -o argument. Otherwise it defaults to the input filename and extension kexe.
Compiling for Raspberry Pi
Now that we have it working on Linux, let’s do the same thing but this time target the Raspberry Pi. For this all we need to do is pass the -target parameter to the compiler
./konanc <path_to_source>/sample.kt -target raspberrypi -o hello
If we try and run this on Linux, we’ll get an expected error
Let’s transfer this over (in my case using scp) to Raspberry Pi and run it there.
scp hello pi@192.168.1.36:~/work
Once we have it on the device we can simply execute the binary
(you’ll notice a small message appear before the output. The team is working on i18n support which will resolve this. In the meantime you can set the locale on your machine to en_US.UTF-8)
Next Steps
This is the first preview of Kotlin/Native and there is still a lot to do (big kudos to the team for making the on-boarding experience smooth), but there is already quite a few things you can do with it beyond a simple
Hello World application. Under the samples folder you can find numerous examples, including interop with C libraries.
Have a play and give us your feedback!