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

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

Kotlin programming 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

    1. Load and install the fabulous and free IDE Visual Studio Code from
    2. Switch to the plug-in menu, search and install the following plug-ins
      1. vscode-icons (gives us nice pictograms for every file extension instead of the generic ones)
      2. Code Runner (allows us to run custom bash commands, like the Kotlin compiler)
      3. Kotlin Language (gives us syntax highlighting for Kotlin)Needed VSC plugins
    3. Load the latest official Kotlin/Native compiler release for macOS from
    4. Extract the downloaded compiler package and move the content into a folder of your choice.
    5. 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.
    6. Open up the Terminal and run
      1. sudo touch /etc/paths.d/kotlinnative

        This creates a new file where you can define system-wide environmental variable entries.

    7. 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.sudo vim /etc/paths.d/kotlinnative
    8. Save the file and exit the terminal
    9. 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

  1. Open VSC’s settings under Code -> Preferences -> Settings
  2. In the search bar search for
    code-runner.executorMap
  3. 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.
  4. 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",
    1. 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…
    2. … then it extracts the basename of the file without the file extension and stores the value to the variable called file
    3. … 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
    4. … and finally, it runs the resulting executable file.VSC Settings for Kotlin/Native

Write your first Kotlin/Native terminal application for your Mac

  1. Setup a new project by opening a folder on your hard drive (create a folder if necessary)
  2. Create a new Kotlin file called e.g. hello.kt and save it (it should reside inside the folder created in the previous step)
  3. 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")
    }
  4. Press the key combination CONTROL + OPTION + N to invoke the Code Runner plug-in with the settings made in step 11.
  5. 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.
  6. You should see the output of your application on the same output terminal.
  7. Congratulations, you wrote, built and run your first macOS application written completely in Kotlin.First run of a Kotlin/Native application on macOS

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

Tags: , , , , , , , , , , , , , ,

Eine Antwort

  1. […] 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 […]

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert