Setup Kotlin in Android Studio

Kotlin is a statically typed programming language runs on the Java virtual machine developed by JetBrains. At Google I/O 2017, Google has announced Kotlin an official language on Android.

Welcome to the second chapter of our course “Begin Android Application Development with MindOrks”. Here, you are going to see why kotlin is getting favourite of every Android Developer nowadays. We were also going to configure our Android Studio to make it use kotlin.

Why Kotlin?

  • Concise — Drastically reduce the amount of boilerplate code.
  • Safe — Avoid entire classes of errors such as null pointer exceptions.
  • Interoperable — Leverage existing libraries for the JVM, Android, and the browser.
  • Tool-friendly — Choose any Java IDE or build from the command line.

Kotlin fully supported in Android Studio 3.0 and higher, so it’s easy to create new projects, add Kotlin files to your existing project, and convert Java language code to Kotlin. You can then use Android Studio’s existing tools with your Kotlin code, such as autocomplete, lint checking, refactoring, debugging, and more.

Kotlin Plugin Setup:

Go to File > Settings > Plugins > Install JetBrains plugin and search “Kotlin” and install it. When installing complete, Android Studio will restart. If you are at “Welcome to Android Studio” screen, choose Configure > Plugins > Install JetBrains plugin and then search “Kotlin” and install it.

Create New Project in Kotlin

Now, Let’s create a new project. You will have to fill all required things like application name, company domain name etc.

Here you will see an option to get Kotlin support which says “Include Kotlin Support”. Checking this option will setup all kotlin dependencies and configure them into your project, you don’t require to do any hard work. Select Next.

Select your targeting Android Device. What is minimum Android SDK and Targeting Android Device? Select Next.

Select your required Activity Module and Select Next.

You can give the name to your activity file which will be your kotlin file
(example: MainActivity.kt) and layout file and Select Finish.

Convert Java to Kotlin

In your Android Studio, you will get an option to convert your Java files into Kotlin Files. Open your Java file which you want to turn into Kotlin and go to Code > Convert Java File to Kotlin File.

Your class will get converted into kotlin file with an extension of .kt.

Note: When you put any Java code into your kotlin file, it will automatically get converted into Kotlin code. That’s the beauty.

Congratulations!! Now, you are ready to code in kotlin and get all advantages for Android Application Development.

In this chapter, you learn about how to make your Android Studio work for Kotlin programing language. In the next chapter, you are going to run your first Android Application on your device with us, and you will be going to learn how to create an emulator (Android Virtual Device).

Congratulations!! Let's start with next chapter.