An Introduction to Kotlin for Android Development

Do you like Java but wish you didn’t have to write so much boilerplate code?
Do you think Java 8 lambdas and streams are cool but not up to par with other functional programming languages?
Are you sick of sprinkling null checks throughout your code and still dealing with NullPointerExceptions?
If you answered yes to any of these questions, then you’re not alone.

My name is Dan Nizri and I’m an Android Developer at Yodle. Recently I came across Kotlin, which is an open source programming language developed primarily by JetBrains, the company responsible for IntelliJ. Kotlin aims to address many of the issues and rough edges that are common with Java development, while making your code more concise, safe, and expressive. Also, it’s 100% interoperable with Java so you can use it in your existing Java project without having to worry about rewriting any of your classes.

Some of the most useful language features include:

  • Data Classes, which let you easily define mutable or immutable model classes and generate the equals(), hashCode(), toString(), and copy() functions for free
  • Null Safety, which helps to eliminate the possibility of a NullPointerException in your code
  • Proper Function Types and Higher Order Functions, which let you write expressive and concise code
  • Extension Functions, which let you add functionality to existing classes (e.g., Activity, Context, TextView) instead of creating Utils classes
  • Type Inference and Smart Casting, which makes dealing with variables easier and more concise

and much more!

Below you can watch the talk I gave at the New York Android Developers Meetup, which was hosted at the Spotify NYC office and covers a few of the concepts mentioned above:

Here are the slides from the talk, and you can also check out the sample Android App I wrote in Kotlin, which shows how to make use of many of the libraries and concepts that are common in Android development such as:

◦ Dagger
◦ Retrofit
◦ Picasso
◦ Palette
◦ RxKotlin
◦ RxLifecycle
◦ RxBinding
◦ Custom View
◦ RecyclerView
◦ CardView
◦ CoordinatorLayout
◦ Circular Reveal Animation
◦ Kotlin Android Extensions
◦ Lambdas
◦ Higher-Order Functions
◦ Data Classes
◦ Extension Functions
◦ Espresso Testing & Dagger mocking
◦ Unit Testing
Sample App GIF

Getting started with Kotlin for Android development is really easy! All you have to do is install the Kotlin plugin through Android Studio, run the Configure Kotlin task in your project, and you could be writing Kotlin code in minutes!

This article was originally posted on the Yodle Tech Blog