Part 1 : Picking Kotlin for Android — The Reason Behind

Overview

This is a long article and will be divided into four parts:

  • Introduction of Kotlin & The Reasons behind the Choice
  • Kotlin’s Killing Features (with details Java comparison)
  • How is Kotlin in Android Development compared to Swift Development in iOS?
  • Kotlin Pro Tips and Our Kotlin Use Case sharing on Android Development (To be continued)

Who am I ?

I am a senior mobile developer in Harborx. Harborx targets to build a simplified forex trading platform which provide fun, smooth and intuitive trading experience for everyone.

Before I had met with ❤ Kotlin, I have a rich experience with Java & Scala. Every time when a new comer asked me to compare Java with Scala, there are some “Java impressions” straightly come to my head:

  • Java is too easy and verbose
  • Null Pointer Exception (NPE, The Billon dollar mistake)
  • Java 8 is better. But far from enough
  • Writing functional style code in Java is comparatively costly — Difficult to use immutable object, no tuples, no type inference, clueless Function<T,R> , BiFunction<T,U,R> , Consumer<T> … , default mutable Collection, Map, Set.

In 2016 Aug, we started to build a new Harborx Android app. It is written by 100% Kotlin with Java Library.

After that, I think these “Java impressions” are still valid when comparing Kotlin to Java.

Kotlin Introduction

Kotlin is a relatively new and statically typed programming language for the JVM, Android and the browser. It is backed by JetBrains —The biggest IDE manufacturer in the world.

Brief Timeline

Interesting Facts

  • JetBrains Official supports Kotlin for Android development.
  • JetBrains claims that Kotlin is a programming language built for industrial instead of general purpose. The basic difference should more likely about thinking of “is this function commonly used in software industrial ? ” instead of “does this function increase the size of std-lib ?”.
    For example, some useful functions like string.replace, collection.find, collection.filterNotNull,are provided in Kotlin standard library while seldom provided in general purpose programming language like Swift and Java.
// Kotlin Standard Library provided

String.replace(
    oldChar: Char, 
    newChar: Char, 
    ignoreCase: Boolean = false
): String

// directly return a object in a collection 
// which is matched with the predicate
fun <T> Iterable.find(predicate:(T) -> Boolean): T?

// filter all null object in the collections
fun <T: Any> Iterable<T?>.filterNotNull(): List<T>
  • Kotlin generate JVM byte code instead of source-to-source transpile to Java — No performance degradation if the generated byte code of Java & Kotlin are the same.
  • Kotlin 1.1 support from Java 1.6 — Most of the legacy Android or Backend Project are supported.
  • “Method Count on a Hello World Android Project(Disable Proguard)”* — Java(17,416), Kotlin 1.1.1(23,587), Scala 2.11(67,608)
  • “Method Count on a Hello World Android Project(Enable Proguard)” * — Java (7,608), Kotlin 1.1.1(7,656), Scala 2.11(11,671),
  • “Compile Time” of Kotlin is much faster than Scala/Groovy and very close to Java*

* Thanks to https://github.com/SidneyXu/AndroidDemoIn4Languages

The Reasons behind the Choice

  1. The belief of ReactiveX. ReactiveX is a combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming. After spending two month of learning, researching and playing toy projects with RxJava and functional, we compared side-by-side with Java implementation. we found that Kotlin can fundamentally provide a much more intuitive and inexpensive way than Java on writing functional style codes, so that we can maximize the power of ReactiveX.
  2. The belief of one of the best Android developer in the world Jake Wharton. If you have gone through a lot of serious Android development, you must know this man (in Harborx, we use Cantonese “Jake 大神” to represent his name lol, “大神” in english is likely mean “Master”). Jake built a lot of high quality libraries for Android such as ButterKnife, Retrofit, okHttp, RxAndroid, RxBinding, Timber, etc. You can easily find his public Kotlin and Android talk on YouTube/Google plus/Twitter.
  3. Kotlin has already reached the 1.0.0 milestone. In 2016 Aug, We expect 1.x.x version API is stable for production. It is the right time to start!
  4. Learning curve of Kotlin is not high. Picking up Kotlin by a traditional Java developer should be less than 2 week.
  5. Swift is very similar to Kotlin. We can nearly apply the same abstraction (Data structures, Extension, Rx…) to model the business flow. Rewriting same business logics between Android & iOS platform become easier and faster. In contrast, switching from Swift to Java would be very painful. For example, Swift developers usually use a lot of extensions to power up existing class while Java developers use “Utils” patterns and inheritance to give extra functionality on existing class.

Next — Part 2 : Picking Kotlin for Android — Killing Features