Kotlin for Android Development
Traditionally, native Android development was done using Java. Kotlin is a relatively recent JVM language developed by JetBrains and was first released in 2011. The Android team at Hootsuite was one of the earliest adopters of Kotlin. In April 2016, the first Kotlin commit was added by converting a Java class into a Kotlin data class. It was shipped to production, and Kotlin was officially introduced into the Hootsuite app. Kotlin turned out to be a boon for the codebase and it’s quickly becoming the go-to programming language for Android Development at Hootsuite. I’ve spent the majority of my time at Hootsuite working with Kotlin and I’ve absolutely enjoyed using it. The amount of thought that has gone into making the language is impressive and it’s got some neat features that come in very handy when coding. Here are some interesting features of Kotlin that I came across during my co-op term:
- Data Classes
A Java Class used for storing data about a car:
The equivalent single line class in Kotlin:
- Null Safety
The code fragment below shows an example of a null check in Java. They are cumbersome and easy to miss.
Kotlin’s type system is designed to eliminate the danger of null references from code. There are two distinct types of references in Kotlin – nullable references and non-nullable references.
As we can see from the code fragment above, if a developer attempted to pass a nullable object to the second class, it would result in a compile time error.
- Kotlin Standard Library
let() is a scoping function used whenever we want to define a variable for a specific scope of the code but not beyond. It can also be used to test for null. In the code fragment below, the variable is no longer visible outside the scope of the block.
- Extension functions
- Kotlin Android Extensions
The code fragment below shows how to set the text of a TextView (with id ‘text_foo’). It needs to be referenced using ‘findViewById’ before calling any method on it.
It gets very repetitive when you have to reference a large number of views this way. One common alternative is to use ButterKnife, a view binding library that uses annotation to generate boilerplate code. However, that has the drawback of introducing an additional library into the project.
Kotlin Android Extensions is a Kotlin plugin, that enables you to recover views from Activities, Fragments and Views in a seamless way. It allows you to access views in the layout XML, just as if they were properties with the name of the id you used in the layout definition. This no need for lengthy findViewByIds or additional third-party libraries. For instance, in the code fragment below, we simply address a TextView with its id, to set its text.
- Java Interoperability in Kotlin
In this blog post, I’ve presented some of the neat features of Kotlin that I enjoyed working with during my co-op term. Kotlin focuses on many important topics like interoperability, safety and clarity. I’d definitely recommend trying out Kotlin! Here are a few resources to get started:
About the Author
Shruti Basil is a Co-op Software Developer on the Core Android team. She currently attends University of Waterloo for Software Engineering.