Visual Studio Code Kotlin



Kotlin IDE for Visual Studio Code. Smart code completion, linting, debugging, formatting and more for Kotlin in VSCode using the Kotlin language server and the Kotlin debug adapter. To use, open a Kotlin file inside a Gradle or Maven project. Support for Kotlin source files with a standalone compiler (kotlinc) is experimental. The Visual Studio Intellicode is one of the most amazing Visual Studio Code extensions that you can ever use. It is built for helping developers and programmers with smart code completion suggestions and has pre-built support for a wide array of programming languages. Kotlin IDE for Visual Studio Code. Smart code completion, linting, debugging, formatting and more for Kotlin in VSCode using the Kotlin language server and the Kotlin debug adapter. To use, open a Kotlin file inside a Gradle or Maven project. Support for Kotlin source files with a standalone compiler (kotlinc) is experimental. The language server will then automatically launch in the background.

[Total: 1 Average: 5/5]

Kotlin is quickly becoming one of the most popular programming languages. Most programming bloggers now recommend Kotlin as one of the must learn languages of 2020.

But before we go into why this makes Kotlin multiplatform special, we need to dig into what multiplatforming is.

Visual studio code spring boot kotlin

The idea of multiplatform is not new. It applies only to the compilation time and once that process is completed, nothing else really matters.

Unlike scripting languages like JavaScript, Python, and Ruby, Kotlin is a programming language. This means that there is no pre-existing interpreter available to turn your code into what you want it to be. A programming language is executed differently from what we’re used to, if you’re coming in from a web development perspective.

When we say the words ‘mobile’ and ‘multiplatform’ in the same sentence, we often associate with third party libraries and frameworks that create structures that wrap around the code to create native applications. However, if you look at the actual code, it’s all still JavaScript under the hood.

Non-browser based applications are a bit different and require translation to run on the machine it exists on. The exe files you see on your computer is a packaged set of translated source code that is compatible for your particular device. This translated code often boils down into machine code.

For multiplatform scripted-based outputs, the wrapper often creates a mini translator

For multiplatform scripted outputs, the wrapper often creates a mini interpreter to make your produced view possible. The code is not truly ‘native’, in that the actual code is sitting inside a generated construct that’s forcing it to work. Kotlin, however, creates true native code and is able to separate itself from platform specific things.

Commitment is pricey, Kotlin reduces it

For a lot of other multiplatform languages, frameworks, and libraries out there, the act of committing to it can be a costly exercise. In part, it’s because they tend to lock you into their ecosystem. This means if you code in something like React Native or Flutter, your entire app is often stuck inside it. It’s the same with wrapper JavaScript based libraries.

You can’t exactly pick and choose what goes where.

Not only that, when using React Native or Flutter you’re often tied to certain UI restrictions and accessibility to native platform features are limited based on the language, library or framework’s support and the size of its ecosystem. This means that if that if your particular chosen language falls out of favor, or the ecosystem doesn’t grow as quickly as you need to in order to cover new features and functionalities, you become instantly disadvantaged.

This is why Kotlin is different to Flutter, for example. Kotlin doesn’t lock you in through an entire application. It focuses on a logic first approach, meaning that your application’s business layer is cleanly separated from the UI. This is important because modern applications tend to focus on a UI first approach, with business logic and rules tightly coupled into the interactions.

This causes a problem for creating truly multiplatform apps, especially in the mobile space. There are major differences in native UI defaults between iOS and Android, and navigating between the two can be difficult. When the code becomes tightly coupled with the UI, it requires sweeping changes to make it compatible and efficient for that particular platform.

Kotlin’s multiplatform approach creates an enforced modularization, forcing you to write your applications in a way that shields you from the allure of creating pixel perfect widgets. Rather, you are thinking about how all the pieces are going to fit together, dealing with how to call in external services, querying APIs, organizing returned data, and dealing with the kinds of interactions you want to achieve.

While the UI is the thing that interfaces with the user and creates the most impact, an app’s ultimate usefulness is determined by the logical rules it wants to implement. The separation of UI and logical layer makes Kotlin a less risky option than fully committing to something like React Native, because you can pick and chose what you want and integrate it with native app languages if required.

Kotlin’s allowance for hybridization results in a flexible migration for existing applications, whilst creating a high level of reusability between different types of developers. This is a useful feature for when we operate in two separate teams – one of Android and one for iOS – as it allows developers to share knowledge domains and reduce duplication of native code.

How to write a Kotlin code in Android Studio

Kotlin is the common code between Android and iOS. However, you might want to work with your Kotlin code in Android Studio. Here’s a quick guide on how to get started with Kotlin in Android Studio – which is an alternative and free way of creating Kotlin code.

First, open up your Android Studio, click on Configure and select Plugins.

Select Marketplace and search for KMM. This stands for Kotlin Multiplatform Mobile.

Once that’s done, it’ll prompt you to restart the IDE. Start a new Android Studio Project and in the Select a project Template section, select KMM Application. You might need to scroll down to the bottom to find it.

Give your project a name, select the minimum SDK support, save location and package name.

Click on the finish button and viola! You now have the barebones set up for Kotlin.

This setup is for writing an entire mobile app in Kotlin. However, if you only want to write libraries, Visual Studio Code also has a plugin that supports Kotlin.

Setting up Kotlin in Visual Studio Code

To get Kotlin to work in Visual Studio Code, you’ll need two plugins: Kotlin Language and Code Runner. Open up your VS Code, head over to plugins and install it.

Next, add Kotlin and Java to PATH. This will let you run Kotlin and Java from the command line. To add a PATH, do the following:

  • Open Start Search and type in env. Choose Edit the system environment variables.
  • This will bring up the System Properties panel. Select Environment Variables.
  • This will bring up a new panel. Under System variables, scroll down until you find Path. Click on edit.
  • Add the following paths to your environment variables (or where you saved it):
    • C:Program FilesJetBrainsIntelliJ IDEA Community Edition 2019.1.3pluginsKotlinkotlincbin
    • C:Program FilesJavajdk-12.0.1bin

You can now run Kotlin in VS Code. To test that everything works, create a new folder with main.kt. Here’s a simple function to get you started:

To run your code, right click on the main function and select run code. Your code should compile and execute. During this process, you’ll also see an additional main.jar file.

Final thoughts

That’s basically it to get set up with Kotlin. Multiplatform itself is a concept that applies at compile time. However, Kotlin does it better than other options out there by creating a system that allows you to keep your Kotlin code modular and not locked into a particular way of writing your app.

When you write in Kotlin, there’s no heavy lifting required to transition or migrate your applications out. It’s compilation process also means that you’re not going to run into problems that JavaScript based applications have with memory, threading, and third party plugin compatibility.

This codelab is part of the Kotlin Bootcamp for Programmers course. You'll get the most value out of this course if you work through the codelabs in sequence. Depending on your knowledge, you may be able to skim some sections. This course is geared towards programmers who know an object-oriented language, and want to learn Kotlin.

What you should already know

You should be familiar with:

  • The basics of a modern, object-oriented, statically typed programming language such as Java or C#
  • How to program with classes, methods, and exception handling in at least one language
  • Using an IDE such as IntelliJ IDEA, Android Studio, Eclipse, or Visual Studio

  • How to work with the Kotlin REPL (Read-Eval-Print Loop) interactive shell
  • The basic syntax of Kotlin code

  • Install the Java Development Kit (JDK) and the IntelliJ IDEA, and become familiar with some Kotlin features.

Kotlin is a new, modern programming language created by programmers, for programmers. It's focused on clarity, conciseness, and code safety.

Mature platform

Kotlin has been around since 2011, and was released as open source in 2012. It reached version 1.0 in 2016, and since 2017 Kotlin has been an officially supported language for building Android apps. It's included with the IntelliJ IDEA as well as Android Studio 3.0 and later.

To see which version of the JDK you have installed, if any, type javac -version in a terminal window.

You can see what the latest version of the JDK is on the Java SE Downloads page. If you have the latest version, skip ahead to Install IntelliJ IDEA.

Note: We recommend that you install only the latest JDK and JRE.

JRE or JDK?

The JRE (Java Runtime Environment) is needed for running Java and Kotlin programs. The JDK (Java Development Kit), on the other hand, includes the JRE, plus the development tools that you need for writing and running Java programs. You need the JDK for writing Kotlin programs.

Step 2: Download the JDK

You can download the JDK for free here: http://www.oracle.com/technetwork/java/javase/downloads/index.html

  1. Click on JDK Download.
  2. Under Downloads, choose the link for the JDK for your operating system.
  3. Accept the license agreement.
  4. Click on the Download button.

From either the Downloads window of the browser, or from the file browser, double-click the .dmg file to launch the install file.

  1. A Finder window appears with an icon of an open box and the name of the .pkg file.
  2. Double-click the package icon to launch the installation app, and follow the prompts as they appear.
  3. You might need to enter the administrator password to continue.
  4. After the installation is complete, feel free to delete the .dmg file to save space.

Visual Studio Code Kotlin Debug

  1. Run the downloaded installer (for example, jdk-14.0.1_windows-x64_bin.exe), which installs both the JDK and the JRE. By default, the JDK is installed in the C:Program FilesJavajdk-14.0.1 directory, but it depends on the latest version.
  2. Accept the defaults, and follow the on-screen instructions to install the JDK.

PATH environment variable (system variable) for executable programs.

  1. In Settings for Windows, search for edit environment in Find a setting.
  2. Select Edit environment variables for your account in the list of matches.
  3. In the Environment Variables dialog in the User variables section, select Path and click the Edit... button.
  4. Add the path to the JDK's bin directory, for example, C:Program FilesJavajdk-14.0.1bin, after any existing items.

Note: This was tested for JDK 14 installed on Windows 10. Other versions of the JDK may use different directories, and the steps may be different for other versions of Windows.

  • To verify that the JDK was installed correctly, type the following commands in a terminal window:
  • Windows users: If you receive an error from either command, confirm you've added the correct path for the JRE.

  • Run the ideaIC.exe file that you downloaded.
  • Follow the instructions in the installation wizard.
  • Mac:

    1. To mount the macOS disk image, double-click the ideaIC.dmg file that you downloaded.
    2. Copy IntelliJ IDEA to the Applications folder.

    Linux:

    1. See Install-Linux-tar.txt in the downloaded .tar.gz file.

    For more information on how to install and set up IntelliJ IDEA, check out Install IntelliJ IDEA.

  • Start IntelliJ IDEA.
  • Install any updates and additional content you are prompted for.
  • Select Configure > Check for Updates until there are no more updates available.
  • Create a Kotlin project so IntelliJ IDEA knows you're working in Kotlin.

    1. In the Welcome to IntelliJ IDEA window, click Create New Project.
    2. In the New Project pane, select Kotlin in the left-hand navigation.
    3. Select Kotlin/JVM (JVM | IDEA in newer versions) in the right panel and click Next.
    4. Name your project Hello Kotlin.
    5. Click Finish.

    Now you can access the REPL (Read-Eval-Print Loop), Kotlin's interactive shell. Commands that you type into the REPL are interpreted as soon as you press Control+Enter (Command+Enter on a Mac).

    1. Select Tools > Kotlin > Kotlin REPL to open the REPL.

    The first time you run IntelliJ IDEA after installing, it may take a few moments before the Kotlin menu appears under Tools.

    1. Type or paste the code below into the REPL.
    1. Press Control+Enter (Command+Enter on a Mac). You should see Hello World, as shown below.
    2. Take a quick look at this Kotlin code. The fun keyword designates a function, followed by the name. As with other programming languages, the parentheses are for function arguments, if any, and the curly braces frame the code for the function. There is no return type because the function doesn't return anything. Also note that there are no semicolons at the ends of lines.

    Note: If you're used to putting semicolons at the end of lines, that's OK—Kotlin doesn't mind.

    Congratulations! You've written your first Kotlin program.

    • Kotlin is similar to other object-oriented programming languages.
    • Install the latest JDK for your operating system to use Kotlin.
    • Install the IntelliJ IDEA to work with Kotlin.
    • In IntelliJ IDEA, start the Kotlin REPL (Tools > Kotlin > Kotlin REPL) to practice in an interactive shell.
    • Enter code followed by Control+Enter (Command+Enter on a Mac) to run it.
    • Here is 'Hello World' in Kotlin:

    Kotlin tutorials

    The https://play.kotlinlang.org website includes rich tutorials called Kotlin Koans, a web-based interpreter, and a complete set of reference documentation with examples.

    IntelliJ IDEA

    Documentation for the IntelliJ IDEA can be found on the JetBrains website.

    This section lists possible homework assignments for students who are working through this codelab as part of a course led by an instructor. It's up to the instructor to do the following:

    • Assign homework if required.
    • Communicate to students how to submit homework assignments.
    • Grade the homework assignments.

    Instructors can use these suggestions as little or as much as they want, and should feel free to assign any other homework they feel is appropriate.

    If you're working through this codelab on your own, feel free to use these homework assignments to test your knowledge.

    Question 1

    Which of the following is NOT a benefit of using the Kotlin language?

    ▢ Kotlin distinguishes between nullable and non-nullable data types.

    ▢ Kotlin is a supported language for building Android apps.

    ▢ Kotlin is designed so you can write less code with fewer bugs.

    ▢ Your code compiles faster in Kotlin.

    repl on the command line.

    ▢ Create a Kotlin project in IntelliJ IDEA, then select Run > Kotlin REPL.

    ▢ Open IntelliJ IDEA, then select File > Kotlin REPL.

    ▢ Create a Kotlin project in IntelliJ IDEA, then select Tools > Kotlin > Kotlin REPL.

    2. Kotlin Basics

    Vscode Kotlin Intellisense

    For an overview of the course, including links to other codelabs, see 'Kotlin Bootcamp for Programmers: Welcome to the course.'