Introduction to Kotlin for Android

What is Kotlin?

Kotlin is a statically-typed programming language that runs on the Java virtual machine. It is 100% interoperable with Java™ and Android™. Its primary development is from a team of JetBrains programmers based in Saint Petersburg, Russia. As of Android Studio 3.0 (Beta), Kotlin is a fully supported programming language on Android.

Why Kotlin?

Kotlin is a great fit for developing Android applications, bringing all of the advantages of a modern language to the Android platform without introducing any new restrictions:

Compatibility: Kotlin is fully compatible with JDK 6, ensuring that Kotlin applications can run on older Android devices with no issues. The Kotlin tooling is fully supported in Android Studio and compatible with the Android build system.

Concise: Drastically reduce the amount of boilerplate code.

In Kotlin we can achieve things by writing less code compared to Java. Also, Kotlin is comparatively more readable. For instance, consider this click listener in Java. Everyone uses click listeners in their Android apps, but it takes a lot of code and ceremony in Java just to perform an action on the click of a button.

Now consider how you do it in Kotlin:

Safe: Avoid entire classes of errors such as null pointer exceptions.

We all are fed up with the Null Pointer Exception (NPE) in java. Kotlin handles all NPEs during compile time. Any reference to null throws a compilation error.

Performance: A Kotlin application runs as fast as an equivalent Java one, thanks to very similar bytecode structure. With Kotlin’s support for inline functions, code using lambdas often runs even faster than the same code written in Java.

Interoperability: Kotlin is 100% interoperable with Java, allowing to use all existing Android libraries in a Kotlin application.

Compilation Time: Kotlin supports efficient incremental compilation, so while there’s some additional overhead for clean builds, incremental builds are usually as fast or faster than with Java.

Learning Curve: For a Java developer, getting started with Kotlin is very easy. The automated Java to Kotlin converter included in the Kotlin plugin helps with the first steps. Kotlin Koans offer a guide through the key features of the language with a series of interactive exercises.

Using Kotlin in Android Studio

Let’s see how can we develop Android apps in Android Studio using Kotlin.

In Android Studio >= 3.0

Android Studio 3.0 officially supports Kotlin. So it will not require any additional configuration to include Kotlin.

Creating new Project

To create new Project using Kotlin, select “Include Kotlin support” checkbox as shown below:

Create new Project using Kotlin

In Android Studio < 3.0

If you are still using Android Studio version previous to 3.0 we strongly recommend you to upgrade to latest version.

Installing Kotlin Plugin

In Android Studio previous to version 3.0, you need to manually download and install the Kolin plugin. To do so, launch Android Studio and in the Welcome to Android Studio screen click on Configure => Plugins. If you have any project open and wish to see Welcome screen, then you can use File => Close Project.

Installing Kotlin in Android Studio 2.2.3

In the window which opens, click on Install JetBrains Plugin as shown below:

Installing JetBrains Plugin

Another window will popup and here search for “Kotlin” in the search bar, select Kotlin from search results and click on Install.

Installing Kotlin in Android Studio from JetBrains

Wait till the installation completes and now either click on “Restart Android Studio” or Restart Android Studio manually.

Configuring Kotlin for the Project

Once you restarted Android Studio, you can create a new project. In order to use Kotlin in the newly created project, you need to Configure Kotlin to your Project.

Configuring Kotlin in Project in Android Studio

Now choose Android with Gradle as the configurator. From the popup that appears, choose whether you want to apply Kotlin for all modules or only for app module.

Configuring Kotlin in Project in Android Studio

Now open build.gradle from Project level, you can see Kotlin dependency added:

In app level gradle file also you will see Kotlin dependency added as highlighted below:

Converting Java code to Kotlin

You can convert code written in Java to Kotlin easily using the following option:

Android Studio: Convert from Kotlin to Java

Now you can see the file converted to Kotlin:

From above code, you observe the following:

  • Kotlin files have .kt extension
  • Semicolons to end a statement are optional in Kotlin
  • fun keyword is used to declare a function.

Creating a new file Kotlin File

To create a new Kotlin file, go to Files => New => Kotlin File/Class

Creating new Kotlin file

 

If you liked this article, then please follow AndroidDeft on Twitter and Facebook.

Stay Connected

No spam guarantee.

Abhishek

Abhishek

Abhishek loves coding. His favorite is Android Development. Apart from coding he loves traveling and reading.
Abhishek

Leave a Reply

Your email address will not be published. Required fields are marked *