How-To – Build and run a Kotlin/Native application with Visual Studio Code on macOS

A short but hopefully complete introduction of how to setup Visual Studio Code (VSC) to build and run Kotlin/Native apps on macOS (High Sierra as of the date this How-To is written). Some basic developer knowledge is assumed, like knowing how to start the macOS Terminal, having XCode and the command line tools installed, and using a text editor like vim or nano. Please use the comments section if something is unclear and I will adjust this tutorial if necessary.
Install Visual Studio Code and all necessary tools
-
- Load and install the fabulous and free IDE Visual Studio Code from
- Switch to the plug-in menu, search and install the following plug-ins
- Load the latest official Kotlin/Native compiler release for macOS from
- Extract the downloaded compiler package and move the content into a folder of your choice.
- Switch to that folder using Finder, mark the containing bin subfolder and hit the OPTION + COMMAND + C key combination on your keyboard. This will copy the absolute path of that folder, instead of copying the content like COMMAND+C does, without the OPTION key.
- Open up the Terminal and run
-
sudo touch /etc/paths.d/kotlinnative
This creates a new file where you can define system-wide environmental variable entries.
-
- Open that file in vim or nano (or which editor suits you best) with sudo rights and past in the copied path from step 5.
- Save the file and exit the terminal
- Restart Visual Studio Code.
You most probably had to reload VSC after the plug-in installation, but this is not enough to make VSC aware of the new environmental variable with the path leading to the Kotlin/Native compiler. Therefore, restart the application by closing it completely and starting it anew.
Configure Visual Studio Code to run the Kotlin/Native compiler
- Open VSC’s settings under Code -> Preferences -> Settings
- In the search bar search for
code-runner.executorMap
- Click on the pencil icon and then on Copy to Settings.
This icon is on the right-hand side. The settings should now appear on the right side where you can edit them. - Search for „.kt“ and replace the bash-command on the right side with the following line:
"cd $dir && file=`baseName $fileName .kt` && konanc -o $file $fileName && ./${file}.kexe",
- What this does is, to changes the compiler’s working directory to the current directory of the source code file, which is currently selected inside the editor…
- … then it extracts the basename of the file without the file extension and stores the value to the variable called file …
- … after that, it runs the Kotlin/Native compiler konanc with the -o option so that the output file will be called like the selected input file but with the extension .kexe …
- … and finally, it runs the resulting executable file.
Write your first Kotlin/Native terminal application for your Mac
- Setup a new project by opening a folder on your hard drive (create a folder if necessary)
- Create a new Kotlin file called e.g. hello.kt and save it (it should reside inside the folder created in the previous step)
- Write a Kotlin program inside hello.kt like the following and save the file.
fun main(args: Array<String>){ println("Hello Kotlin Native on my MacOS X") }
- Press the key combination CONTROL + OPTION + N to invoke the Code Runner plug-in with the settings made in step 11.
- On the first run, the compiler will download all macOS dependencies from the internet, which can take a while. Thereafter, every subsequent build is a lot faster.
- You should see the output of your application on the same output terminal.
- Congratulations, you wrote, built and run your first macOS application written completely in Kotlin.
The next step is to transfer the build process to a more advanced build-framework like Gradle or Maven to make a multi-file project more manageable. However, for learning and exploring Kotlin(/Native) this is a good starting point if you are on a Mac.
If you like what I do and want to support me, please consider buying your next tech over one of the following partner links. No drawbacks for you, but some free coffee for me. ☕
Stay productive,
Martin
Eine Antwort
[…] my last How-To – Build and run a Kotlin/Native application with Visual Studio Code on macOS I showed you how to setup, build, and run your first Kotlin/Native application using the barebone […]