Kotlin is way more than just an Android programming language. And to show you I’ll explain how you can run Kotlin Applications on a headless Raspberry Pi.
Preparing a Raspberry Pi
I won’t go into much detail on how to setup a Raspberry Pi nor how to configure it’s network settings or connect remotely. For that you can go to this official raspberry pi getting started documentation. Not all system images come with a
JDK included, you can check with
java version or install using the package manager.
sudo apt update sudo apt install default-jdk
default-jdk fails you’re probably on an older Raspberry Pi that doesn’t support the latest Java SDK. To fix this install
openJdk 8 instead with below command. I had to do so on my Raspberry Pi Model B.
sudo apt install openjdk-8-jdk
By the way this is how you find out what model you have
pi@raspberrypi2:~ $ cat /proc/cpuinfo processor : 0 model name : ARMv6-compatible processor rev 7 (v6l) ... Hardware : BCM2835 ... Model : Raspberry Pi Model B Rev 2
Preparing Development Environment
Next step is to prepare the development environment. In similar fashion as to how you would be programming with
Android Studio IDE and publish apps to an
Android Emulator or
adb you can get an
IntelliJ installation with the
Embedded Linux JVM Debugger Plugin. That step is technically not required but will make this development so much easier.
On your development machine install IntelliJ Community edition after downloading it from here. So far so good. Once installed use this link to the Embedded Linux JVM Debugger Plugin or you should be able to find it from the IntelliJ launch screen picking the
Plugins tab searching for “embedded linux”. See below screenshot.
Creating your First Project
From that same launch screen you can also create a new project form the first
Projects tab. In doing so you can enter a
name for your project, select
Kotlin language and
gradle build system and so on. For this project to work as a headless kotlin application on a Raspberry Pi you should go to
Kotlin Multiplatform tab next to pick the
Console Application JVM project template. At this point you’re ready to hit
next followed by
Finish on the next screen.
Now in the Project view select the
src > main > kotlin > Main.kt file to start. In order to launch this app you can use the green arrow next to the
Configurations drop down on the top right. Note that this will give you an execution on your development machine. To run it on the Raspberry Pi you’ll have to create a new Configuration using
Edit Configurations from that list. In the new screen use the
add new... options or the
Then pick the
Embedded Linux JVM template and complete with all the connection details for your Raspberry Pi. If that option is not enabled make sure you’ve restarted IntelliJ after the plugin install and that the gradle system has finished building.
That’s it. If you execute that run configuration you’ll now see a run screen publishing code to the raspberry pi, compiling and running it there.
I ran in a Java version issue still when executing due to my older Raspberry Pi model. The reason was I installed the
default jdk first and then
openjdk-8. Instead of showing the me
Hello World! sentence it would render an error
Server VM is only supported on ARMv7+ VFP.
In order to specify which java version to compile and run with by default you can use the command below.
sudo update-alternatives --config java
For my personal usecase I’ll be looking at this bluetooth multiplatform library. It includes a Raspberry Pi specific example but a quick look reveals that it is using JavaFX so requires a screen for rendering the result. Instead this is how I’ll start working on my BLE Energica connection from the Raspberry Pi that remains on the bike while riding.
To control GPIO pins on your Raspberry Pi you can look into Pi4J and Pi4K libraries. Publishing can be done as a
.jar file using the ShadowJar Gradle plugin.