Programming with Kotlin in Visual Studio Code

Laxman Sahni
4 min readJul 2, 2018

--

Kotlin is all the rage now that it has been deemed a first-class programming language for developing Android apps.

Actually, it’s been raging all on its own for a while now, as evidenced by the Stack Overflow Trends chart below.

Kotlin Gains in Popularity (source: Stack Overflow Trends)

System requirements

To install and run Kotlin, your development environment must meet these minimum requirements:

  • Operating Systems: macOS (64-bit)
  • Disk Space: 700 MB (does not include disk space for IDE/tools).
  • Tools: Visual Studio Code

Install Command Line Compiler

on macOS I recommend you to install the compiler via Homebrew.

$ brew update
$ brew install kotlin
brew update
Kotlin compiler installation

Install extensions

Support for programming languages in VS Code is typically provided through extensions, which can be found in the Visual Studio Code Marketplace.

  1. Kotlin Language comes from developer Mathias Fröhlich, providing Kotlin language support for VS Code — basically a syntax highlighter, Code snippeter, Region code folding for the Kotlin language. Fröhlich credits the open source Kotlin Sublime Text 2 Package, so his extension is likely based on that.

As of this writing, the extension has been installed in VS Code 89,504 times. It has been rated 4.5/5 by users.

Kotlin Language Extension

On the project’s GitHub site, it has garnered 78 stars, with 64 commits coming from three contributors. It was last updated in April, 2018.

Kotlin language source code on GitHub

To install it,

  1. Start VS Code
  2. Invoke View>Command Palette… or Command+Shift+B
  3. Type ‘install’, and select the ‘Extensions: Install Extension’ action
Extensions: Install Extension in Command Palette

4. Enter Kotlin in the search field, select ‘Flutter’ in the list, and click Install

Kotlin extension

5. Click ‘Reload’ to reload VS Code

2. Code Runner

Coming from developer Jun Han, this tool isn’t Kotlin-specific — rather, it lets VS Code users run code snippets or code files coming in 35 different programming languages. Kotlin is the latest addition to that extensive list.

Code Runner

Code Runner provides the following features:

  • Run code file of current active Text Editor
  • Run selected code snippet in Text Editor
  • Run custom command
  • Stop code running
  • View output in Output Window
  • Set default language to run
  • Select language to run
  • Support REPL by running code in Integrated Terminal

Code Runner’s new support for Kotlin was announced in a blog post in summer 2017 on the Microsoft developer site.

The Code Runner extension has been installed 3,173,581 times and has an average rating of 4.6/5 from 96 reviewers.

The project’s GitHub site shows 336 stars, 117 commits, 70 releases and 19 contributors. The last commit was on March 19, 2018.

Code Runner source code on GitHub

Install it by the steps,

  1. Start VS Code
  2. Invoke View>Command Palette… or Command+Shift+B
  3. Type ‘install’, and select the ‘Extensions: Install Extension’ action
  4. Enter Code Runnerin the search field, select ‘Flutter’ in the list, and click Install
Code Runner extension

Coding

I’m about to write code.

  1. Press Cmd+N
  2. Write
fun main(args: Array<String>){  
println("test")
}

3. Press Cmd+S & save it as test.kt on some location

test.kt file

4. Right click test.kt & choose Run Code

Run Code

5. Output is in OUTPUT console

Output console

Under the hood

Because what Code Runner does is just:

cd "/Users/laxmansahni/Documents/Workspace/" && kotlinc test.kt -include-runtime -d test.jar && java -jar test.jartest

A lot faster. Cao ~ 👋

As always, any feedback is appreciated, so feel free to comment down here or reach out on twitter — and, as always,

--

--