Creating and Running a Kotlin Program in Android Studio

A Kotlin program or Kotlin project consists of a group of classes; each file performs a part of the Kotlin program.

In this example, we are going to add a Kotlin class into the existing project. Then try to run the program and display “Hello Kotlin!” message to the console.

To create a Kotlin class follow the following steps.

  1. Right-click the application name in the java package (com.example.hellokotlin) > New > Kotlin Class/File.

3. Enter the class name as HelloKotlin, make sure “File” is selected and the press “Enter” key.

4. You will see a new file added to package com.example.hellokotlin as illustrated below.

5. To test the newly created Kotlin program, add a main method. Main method is the starting point of your Kotlin program. Add codes below to the class.

6. To run the program, click on the Run button on the line number bar and then select “Run ‘HelloKotlinKt”.

7. When you run the program you will get see the “Hello Kotlin!” message displayed in the console.