A Quick Dive into Kotlin for Android.

Step 1: I made a thingy.

Darrellii
4 min readDec 14, 2015

--

Seeing as how my last post was just an explanation as to why I’m blogging, I’ll count this as my first Official post. As I mentioned towards the end of College Ends. Learning Begins, I wanted to start by building an Android app in Kotlin.

I decided to make a simple calculator application that did the four main operations and only dealt with integers. Clearly the next million dollar app idea, I’ll leave my PayPal below for you to send “investments”.

In all seriousness, I wanted something that I could build in an hour or two in Java and then rewrite it in Kotlin. Over the course of this project I learned allot about Kotlin, and especially how it works with Android dev, but be warned, I’m NOT an expert. Yet.

Java vs Kotlin. Round 1!

First impressions: Kotlin is Awesome.

Functional, Strictly Typed, and Inferred, Kotlin has the makings of every great modern language. While writing this app I often felt like I was writing in Swift all the sudden, which is an awesome language, but personally I’m always a fan of things run on the JVM with garbage collection and all that.

As far as comparing Java to Kotlin for Android, the biggest advantages to Kotlin are going to be found in your onCreate methods, with the help of kotlinx.android.synthetic.ui_filename.* import that lets you reference all of your UI elements by their id field.

Java anonymous Class OnClickListener via findViewById()

Button b0 = (Button) findViewById(R.id.b0);
b0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
appendNumber(0);
}
});

Kotlin set OnClickLister function via Synthetic

b0.setOnClickListener { appendNumber(0) }

Wasn’t that beautiful?!

As an Android Dev, I love being able to reference my UI elements as pre-made instance variables! But that’s not the only place you’re going to see your code streamline; the geniuses at JetBrains (I’m not a fan boy) have worked hard to remove all unneeded bits of code.

Some of my favorites include they removed are:

  • No “;”
    You can still use them, but is there ever really a reason to?
  • No “new” keyword!
  • No anonymous classes.
    This is arguably already true for java. With Java 8 we now have lambdas that work just like those in Kotlin and Scala, and with the help of Intellij we can collapse anonymous classes into lambdas, at least from a readability stand point while working with Android on Java 7.

There’s tons more that can be said on difference between the two, but it seems that’s a bit blogged out. If you want to see more give it a quick google, very worth it.

IntelliJ has an old site listing some of the major differences, click here to see it. (Hopefully the site is still up when you read this.)

Should We Make the Switch?

With so much going for Kotlin, it seems like the logical next step for Android devs. What do you think? It’s worth noting that Kotlin is still technically pre production at the time of this post, however, I think there are situations in which to switch (and not to switch) to this new language.

When to Use Kotlin?

If you’re part of a larger cooperation or team, with plenty of skilled devs, I think switching Kotlin would be a great idea! It will save time and energy as you create new features to the app which obviously translates to money saved. And the best part? The way Android studio builds the project, you can keep all of your legacy code in java working as it was, and build everything new in Kotlin.

So When Not To?

I just recently started a new contract where I’m building an Android app and an iPhone app that should have an initial version pushed out this Christmas. When planning out how my partner and I would build the Android app, we both thought it would be fun and insightful to build this app in Kotlin.

In the end we decided against Kotlin and chose to stick with good ol’ Java. This was because as independent contractors, we don’t know who is going to be maintaining this code in the future. We don’t know if they’re brand new to Android, just learned Java, or if they’ve been writing code longer than I’ve been alive. We decided that on such a small new project it wasn’t really our place to decide what future devs are going to be working with.

If you are going to make the switch sooner rather than later, I would base that decision on the skill of your team. If they are already comfortable with Android, I would strongly suggest making the move to Kotlin. Its may only be a matter of time before we all are.

Whats next?

I posted the project I talked about to Git Hub here. Feel free to fork it, make changes, fix my bugs (There are several). Give me a Pull Request if you want, this project is meant to help learn, and the more, the merrier.

Feel free to make comments below or reach out to me on twitter, @Darrellii, about this post or if you think of anything worth learning for my next post. I’m doing allot with iPhone right now but sadly its all proprietary so I can’t exactly post anything I’m currently learning here.

I was thinking next I might try and make a project with OpenGl ES and Android. I noticed there isn’t any good Log burning apps and I thought I might try and simulate some fire and maybe even cast it to my TV. Tis the Season right?

Merry Christmas and Happy Holidays!

Keep On Learning
#devPunk

--

--