Adding Kotlin to an existing IntelliJ project

Let's start by adding Kotlin to an existing Java project. To get started, walk through the following steps:

  1. Download or check out the code from the following GitHub link: https://github.com/PacktPublishing/Mastering-Kotlin/tree/master/Chapter06.
  2. Open the Chapter06 project with IntelliJ IDEA. Once the project is open in IntelliJ, you should see the following Main.java file with a simple Hello World example:

  1. To verify that it is building correctly, you can click on one of the green arrow icons in the editor and select Run Main.main(). On clicking, you should see Hello World! printed out to the console:

Now, let's add our first Kotlin file to this project:

  1. In the project pane on the left-hand side of the IDE, right-click on src, then click on NewKotlin File/Class:

Clicking Kotlin File/Class will open the New Kotlin File/Class dialog.

  1. From here, you can click the Kind drop-down menu to select the type of Kotlin file or class you wish to create:

  1. Select File and enter Main.kt as the filename, then click OK:

  1. After clicking OK, the IDE will create a new, empty Main.kt file. At this point, we have a Kotlin file and can start writing the Kotlin code, but the project will indicate that Kotlin is not yet configured:

  1. To start the configuration process, select Configure in the upper-right corner of the editor. In the modal that opens, select Java, because we want to work with a mixed Java/Kotlin code base without working with any JavaScript:

  1. After selecting Java, you should see a new modal open that says KotlinJavaRuntime library was added to module ch6:

  1. If you then open the ch6.iml file, you'll find a newly added <orderEntry /> instance that adds KotlinJavaRuntime to the project:

When using IntelliJ or Android Studio, you may notice one or more .iml files added to your project. This file extension is used by the IDE to store metadata related to a specific project module. This metadata can include elements such as the module path and module dependencies as in the case of the added KotlinJavaRuntime dependency we just saw added to our project. While these files are important to the IDE, you'll likely want to avoid checking them into source control and instead let the IDE regenerate them on a local machine when the project is cloned.

Now that Kotlin is configured for the project, we can add and run our Kotlin code:

  1. Start by adding a simple main() function and a println statement.
  2. Then, click on the green arrow icon, and select Run Main.kt.

This will run the main function and print out Hello Kotlin to the console:

From here, you are free to write in both Java and Kotlin, mixing them as desired. We will dive deeper into the Java/Kotlin interop experience in Chapter 7, Crossing Over – Working across Java and Kotlin. Before diving deeper into Kotlin and Java interop details, we're going to quickly explore how to easily convert Java into Kotlin in the next section.