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
Data Classes only contain state and don’t perform any operation. The advantage of using data classes instead of regular classes is that Kotlin gives us a ton of self-generated code. For instance, the properties declared in the constructor help avoid the boilerplate code of getters, setters, the constructor and copy() method. Let’s see an example -
A Java Class used for storing data about a car:
The equivalent single line class in Kotlin:
- Null Safety
Null Safety is one of the most useful features of Kotlin. In Java, any object can be null. This means that runtime checks must be added throughout a codebase in order to prevent NullPointerException crashes, which has often been called a Billion Dollar Mistake by language designers.
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
The Kotlin Standard Library contains some very powerful functions. They are concise, intuitive and help consolidate the code. Some of the most commonly used ones are:
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.
The apply() function calls the closure passed in parameter and then returns the receiver object that closure ran on. In the code fragment below, we create a create a new ‘Person’ object with ‘name’ argument, call the method foo() in the closure on it and return the the resulting object instance afterwards, all in a single line!
The with() function is used to factor out the variable when multiple different methods are called on the same variable. In code fragment below, we call the 3 methods on object w without having to repeat ‘w’ for each method call.
- Extension functions
Extension functions enable you to add functions to a class without actually writing them inside the class. They are used like regular functions and are statically resolved. They come in especially handy when working with library code that you cannot change. For instance, here’s how you can extend the View class to add a method that makes a view object visible:
- Kotlin Android Extensions
When using Java for Android Development, in order to reference a view from its layout file, you have to call the findViewById() method.
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
Kotlin boasts 100% interoperability with existing Java code. All Kotlin classes can inherit from Java classes, implement Java interfaces, call Java methods, and so on. Conversely, Java code can inherit from Kotlin classes, implement Kotlin interfaces and call Kotlin methods. This means that you could write code in Kotlin, without having to jump through hoops to make it compatible with pre-existing Java code or frameworks. It is one of the key reasons why experimenting with Kotlin in large codebases is possible.
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.