Say “Hello” to Kotlin

Sergii Zhuk
4 min readMar 2, 2016

--

After the recent release of Kotlin version 1.0 buzz around this programming language is growing rapidly. I’ve spent few weekends exploring it features & maturity and want to share my observations with you.

One more JVM-based language

The keyword of Kotlin 1.0 release is “pragmatism”. Less verbose, more type-safety, zero-overhead null-safety, Java interop, compile to javascript. Yes, I know, it could be something you can find in other languages like Groovy.

But not every JVM-based language has such an excellent integration with IntelliJ/Android Studio IDE — obviously because both Kotlin and IntelliJ products were developed by JetBrains team.

And no other language proposes to change Android programming paradigms in the way that Kotlin does. It sounds like a new chance for Android programming which is full of complicated & redundant concepts now.

Kotlin for Android

You can develop Android apps with Kotlin using the compiling plugin called Anko. The key features of Kotlin & Anko :

  1. Fold AndroidSDK-specific code to be less verbose
  2. Take UI code away from xml layout files. Of course without Anko you also can achieve it, but by writing long and hard to read code

It looks like Android community is completely open for changes and important personalities are going through this topic. For example, amazing Using Project Kotlin for Android document by Jake Wharton.

Build the app

Kotlin build process looks pretty similar to the same flow in Java. It generates regular Java bytecode and works great together with all existing Java libraries and frameworks. The only thing we can highlight here is that Kotlin runtime library containing Kotlin’s own standard library classes and the Java extensions need to be distributed together with your app. The lib size is less than 300 kb.

Anko library for Android will take some extra space, the compiled apk file size for the demo project using anko-sdk15:0.8.2 and kotlin-stdlib:1.0.0 is approx 450 kb without minification and 90 kb with proguard enabled. APK method count shows 6584 methods for package kotlin.* and 1691 methods for org.jetbrains.*. So totally you will get about 8300 extra methods.

Sweet examples

Some parts of Kotlin code could look pretty similar to Java. Meanwhile a lot of useless statements can be omitted, let’s check some examples.

Calls in chains

My favorite one is about safe calls in chains. For example, if Bob, an Employee, may be assigned to a Department (or not), that in turn may have another Employee as a department head, then to obtain the name of Bob’s department head, if any), we write the following:

bob?.department?.head?.name

Such a chain returns null if any of the properties in it is null. By pushing nullability into the type system, traditional workarounds like optional are not needed.

Not specify variable type

In many cases, the type of a variable can automatically be determined from context, and in those cases Kotlin allows you to omit the type.

val x = 1

Same as Java, Kotlin is a statically typed programming language. But Kotlin is able to determine the types from the context automatically during the compilation.

Extension Methods

Kotlin allows the declaration of both static and instance methods into types which you do not control–including those from the Java standard library. Unlike C#, however, these methods are statically resolved using imports which provides clarity on their origin (just as a statically imported helper method on a utility class would).

fun String.last() : Char {
return this[length — 1] // Using built-in extension for array index
}
val x = “Hey!”
println(x.last()) // Prints “!”.

Android: define UI in code

Here is a code example from Anko demo project that shows how to define a layout inside your Activity and specify click behaviour on it. Please take into account that the latest Android Studio version available for now (2.0 Beta 6) you still need to install Kotlin plugin.

override fun createView(ui: AnkoContext<MainActivity>) = with(ui) {
verticalLayout {
padding = dip(32)

imageView(android.R.drawable.ic_menu_manage).lparams {
margin = dip(16)
gravity = Gravity.CENTER
}

val
name = editText {
hintResource = R.string.name
}
val
password = editText {
hintResource = R.string.password
inputType = (TYPE_CLASS_TEXT
or TYPE_TEXT_VARIATION_PASSWORD)
}

button("Log in") {
onClick {
ui.owner.tryLogin(ui, name.text, password.text)
}
}

}.style(customStyle)
}

Documentation available

Small conclusion

In this post I’ve reviewed Kotlin opportunities for JVM and Android and shown some short code examples. I think it will be hard to push backend developers who are using modern JVM-based languages (i.e. Scala or Groovy) to begin working with Kotlin. These guys are already enjoying most of the benefits Kotlin could give. But for Android world it can be a breakthrough in case if Kotlin reaches a solid amount of users.

Develop with pleasure! :)

--

--