Sharing our technical trials in terms of Ubuntu software installs, Blogger hacks, Android and Kotlin learning.
How to set up IntelliJ IDE for Kotlin coding
How to set up IntelliJ IDE for Kotlin coding

How to set up IntelliJ IDE for Kotlin coding

The post briefs on how to set up IntelliJ IDE before you start coding in Kotlin.

I. Install the IDE

For installing it on Ubuntu machines you can follow our post How to install IntelliJ IDE on Ubuntu machines and complete the installation.

You can also refer to the YouTube Video on the same

II. Configure the IDE

Step 1 : Privacy policy & Data sharing

  • After the installation is complete, launch the IDE, you will be presented with a Privacy Policy dialog
  • accept the terms by checking on the checkbox provided at the bottom
  • continue button will be activated, then click on continue.
  • Next you get a Data Sharing dialog, go through the content, based on whether you want to share the content or not, choose “Don’t send” or “Send Anonymous Statistics”.

Step 2 : Welcome screen & choosing project

  • If you are creating a project for the first time choose “New Project”.
  • In case you want to open an exiting project choose “Open”.
  • Want to get the project from version control like GitHub or others, choose “Get from VCS”.
options to choose a project from
  • We will focus on creating new project in our post

Step 3 : Project set up

  • Once you select “New Project” you will be seeing Project setup screen
  • As intelliJ can be used to create projects in different languages, we need to choose the kind of project we want to create
  • We are focusing on Kotlin, hence from the option on the left choose Kotlin
  • You will then be shown options to configure your project
  • Specify a name to your project in the Name field
  • Next is Location, indicating where you want to save your project, you can leave it to default, if you want to change click on the browse icon and browse to a location of your choice
specify project name and location
  • Rest fields like Project template and Build System you can leave it to default values.
  • Build system should be “Gradle Kotlin” for Kotlin programming.
default options for project template and build system
  • If at all you have <NO SDK> showing under Project JDK, click on the drop down and choose Download JDK.
In case of no SDK download JDK
  • Once the JDK download is complete click on “Next”.
  • In the next screen, you can leave the options to default and click on “Finish”.
  • If you wish to change options from this screen, you can do so by clicking on the down arrow and choosing
Fields in New Project window
  • This completes the process of creating a project. And you will be able see the project
  • Next we will see how to add files to the project.

Step 4 : Adding files to project

  • On expanding the project you will be able to see src folder, any new file is to be added under src folder.
  • Click to expand on src folder – > expand Main -> Kotlin – > Right click on Kotlin -> New – > Kotlin Class/File
creating a kotlin file
  • Give a name and choose the type from the options based on you want it to be file, class or interface.
  • File by the name would be created under src folder, and you can start writing code in this file
Give name and choose type for the file

Step 5 : Add a main method & run the code

  • Main method marks the entry point into the application
  • When you run your code JVM looks for the main method, which starts the application add this method in the file
 fun main (args:Array<String>)
{
   println("Hello World")
}
  • Write a piece of code in the main function, for example printing to the standard output
  • Click on the Green icon and choose Run to execute the code
green icon to instantly run code
  • If the code has no errors, it complies and run’s successfully and you will be able to see the output on the console.

Now, here is what happens in the background when you run the program

  • The command first compiles your Kotlin source code into JVM bytecode
  • Compiling First.kt creates a file called FirstKt.class
  • The IDE then starts the JVM and runs FirstKt.class and executes the code written in the file.

Additionals :

I. Print and Println

Print – Prints to the standard output

Println – Prints to the standard output and adds a new line, so whatever is printed next will be on a new line

 fun main (args:Array<String>)
{
   println("Welcome")
   print("to world of programming")
}

II. Commenting code in Kotlin

 fun main (args:Array<String>)
{
   // This is used for single line comment
   /* This is used for multiple line comments  */
   println("Welcome")
   print("to world of programming")
}

That brings us to the end of how to set up IntelliJ IDE for Kotlin coding. Happy coding 🙂

For more posts on Kotlin check out our Kotlin-Hacks section.

Check out the YouTube video on the same