Android Development, Kotlin

Today in Mobile: You Should Build Android apps in Kotlin

Building Android apps just got easier with Kotlin.

Since 2017, Kotlin became an official language to build Android applications in. It is a modern, expressive and statically typed programming language that can be picked up easily if you have prior coding experience.

 

Why you should code in Kotlin

 

1. Kotlin would boost productivity by 100%:

Kotlin reduces boilerplate code.  Imagine having to write a custom Java class for a person:

public class Person{
   private String firstname, lastname, gender;
   private int age;

   public Person(String firstname, String lastname, String gender, int age){
      this.firstname = firstname;
      this.lastname = lastname;
      this.gender = gender;
      this.age = age;
   }

   //getters
   public String getFirstname(){ return this.firstname; }
   public String getLastname(){ return this.lastname; }
   public String getGender(){ return this.gender; }
   public int getAge(){ return this.age; }

   //setters
   public void setFirstname(String firstname){this.firstname = firstname;}
   public void setLastname(String lastname){this.lastname = lastname;}
   public void setGender(String gender){this.gender = gender;}
   public void setAge(int age){this.age = age;}
}

When we could have done this in Kotlin using data classes:

data class Person(var firstname: String, 
                  var lastname: String, 
                  var gender: String,
                  var age: Int)

Pretty straightforward. Right? We are more productive when we use less time to do work, this just means we can do a lot more and meet deadlines faster.

 

2. Kotlin and Java are interoperable:

This is also a key thing. Java classes can be called in Kotlin and vice versa. This means that if you have an existing Android application written in Java, you really don’t have to change all the code to Kotlin (if you don’t want to). You can simply start writing Kotlin within your Java Android application. Kotlin is also converted to bytecode and also runs on the Java JVM. Android studio also provides an easy way of converting Java code to Kotlin if you’re interested. Simply go to Code -> Covert Java File to Kotlin File.

convert java to kotlin image

 

3. Goodbye, NullPointerException:

Kotlin provides inbuilt null-safe operators. This makes it easy to catch nulls during compile time and this further prevents the app from crashing at runtime. Nullable objects can be declared with a “?”.

var name: String = "Adora"
name = null //compile error. name cannot hold null

var phrase: String? = "To be or not to be"
phrase = null //this is okay

When a variable is defined, it is important to tell Kotlin whether the variable is nullable or not. Kotlin compiler returns an error when you call a method from a nullable variable. This makes it safe.

val l = phrase.length // error: variable 'phrase' can be null

However, there are some situations when we have to call these methods. If we MUST do this, we can check if the variable is null or use the safe call operator.

Checking for null
val l = if (phrase != null) phrase.length else -1  
//this would return the length of phrase when it is not null and -1 when it is

 

The Safe Call Operator

The second way to handle this is to append “?” to the variable name before calling the method

println(phrase?.length)

As we can see, whichever route we follow, there is no room to get a NullPointerException.

 

4. Tool friendly:

Android Studio 3.0 and later provides full tooling support for Kotlin.

 

Who uses Kotlin?

Some brands have started using Kotlin to build their Android apps:

  1. Slack
  2. Pinterest
  3. Udacity
  4. Reddit
  5. Camera360
  6. Adobe Reader
  7. Twitter
  8. WordPress
  9. Netflix (and more)

 

As a developer, you don’t have any productivity obstruction anymore. Life just got easier. From saying goodbye to NullPointerExceptions, to reducing boilerplate code and writing code that can be easily read.

The Kotlin documentation is also pretty straightforward if you would like to get started.

You can also follow my new series, Zero to Kotlin Hero.

Happy Coding!

 

Spread the love
Tagged ,

2 thoughts on “Today in Mobile: You Should Build Android apps in Kotlin

  1. Very nice post. I just stumbled upon your weblog and wished to say that I have really enjoyed browsing your blog posts.
    After all I’ll be subscribing to your feed and I hope you write again very soon!

Leave a Reply

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