Skip Navigation
Trulia Logo

Trulia Blog

Kotlin at Trulia: Leaner and Meaner on Android

If you’ve heard of Kotlin, you probably know the programming language provides some great advantages on the Android platform. Trulia has embraced Kotlin in order to give consumers more features and a snappier experience.

Trulia offers For SaleRentals and Mortgage apps for Android phones, tablets and wearables. Our team of nine engineers developed and actively maintain six different apps for the Android ecosystem. In building these apps, we mainly used Android’s software developer kit and tools, and wrote our code in Java.

So, when Google announced Kotlin as a fully supported first-class language for Android development at Google I/O earlier this year, we were thrilled. To us, this is a major milestone in Android development. In this post we’ll explain how and why we use Kotlin at Trulia.

First, some context for those who are new to Kotlin: It’s not new. Kotlin has been around since 2011, when JetBrains introduced it as a new language for Java Virtual Machine (JVM). Naturally, the developer community was curious, and with the help of third party tools and support, developers have been writing Android apps using Kotlin for a couple years now. Only in the past year or so has the excitement and adoption among the community grown to such extent that Google decided to officially support it.

Why Kotlin at Trulia?
We decide to use Kotlin for a number of reasons:

For starters, the language enables us to write easily understandable and brief code, as compared to Java, while still retaining the safety and performance of Java. Kotlin code compiles into bytecode and has the same compile time checks as Java.

For example, in Java, a standard value class is filled with constructors, getters, setters, etc. (see below).

public class Employee {
  private int id;
  private String name;

  public Employee(int id, String name) {
    this.id = id;
    this.name = name;
  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

In Kotlin, there are data classes that would turn the above 20-plus lines of code into one single line of code, but still offer the same functionality (see below).

data class Employee(var id: Int, var name: String)

As you can see, Kotlin’s data classes are more concise than Java’s value classes, but still easy to understand.

Kotlin essentially lets us write less boilerplate code, so we can spend more time writing actual functional code. When there is less code to maintain, there is more time to work on new features for consumers. Also, understanding code becomes easier with less useless code spewed around in the code base.

Unlike the Java version, the Kotlin language also has no dependency on Android OS. In the past, without Kotlin, we would miss out on the new language features and APIs that are introduced with the latest Java releases.

Second, Kotlin adds more compile time checks, namely the explicit handling of null. NullPointerExceptions are a pain for Android developers; they crash the app, which frustrates consumers and makes more work for developers. In fact, early adopters of Kotlin for Android have seen their users experience less than 1% crashes in their sessions, which is awesome.

Finally, Kotlin has extension functions that provide the ability to extend a class with new functionality, without having to inherit from the class or use any type of design pattern, such as Decorator. This allows us to write more natural code without having to create tons of utility or helper classes with static methods to do those tasks.

Trulia’s Approach to Kotlin
You might ask, since Kotlin is so awesome, are we rewriting all our apps entirely in Kotlin? No, we are taking a step-by-step approach at Trulia. Our team has decided to write any new code in Kotlin moving forward. For example, the Commute Time feature we recently added within our property details page in the app was completely written in Kotlin – we also recently rewrote our networking layer to use Retrofit in Kotlin.

Trulia Commute Time

Interested in adopting Kotlin? Here are some tips to keep in mind:

  1. Don’t try to boil the ocean. Let people take small units of code and refactor them in Kotlin to see how the code compares. This approach let our developers see the benefit of Kotlin and therefore encouraged them to use it more and more.
  2. Take advantage of extension functions in order to add more capabilities to existing utility classes.
  3. Finally, don’t forget to encourage your team to take every opportunity to learn. We’ve been using Kotlin since May and we still attended the KotlinConf 2017 in November – we learned some new things!

Refactoring components and modules is something we do regularly, so when we are refactoring code, we are writing it in Kotlin. The great thing about having first-class support from Google is the tools within Android Studio give us seamless interoperation with Java. Meaning that even while debugging, you can step from Java code to Kotlin and back to Java again without any problems. Android Studio also offers a Convert Java File to Kotlin File that does a fairly decent job converting your existing Java code to Kotlin.

The Trulia Android team has been using Kotlin as one of our programming languages since May 2017. We are all excited to be learning more, while writing less code and moving faster in shipping features to consumers.