Written by Damian Petla
Android Developer
Published August 14, 2017

Kotlin - lessons learned from early adoption

There is a lot of information recently about Kotlin, what it is, how better it is from Java, should you move to Kotlin, who created it and above all that Google officially support it since I/O 2017. You can easily find materials teaching you all great features Kotlin has. But you probably wonder:

Design of the bulb by Freepik.com Design of the bulb by Freepik.com


Note: this article was previously published on Damian’s Medium.com profile. The author agreed to republish the article on the Schibsted Developers Blog.

Should I already move to Kotlin or wait for everyone else?

How should I start? Should I move all my code? Do I have to learn all those new features?

How does it work for other developers?

In this post, I will try to answer those questions and share my experience.

The beginning

I started using Kotlin in late 2014 with a new Android project in my new job. I had freedom to choose technologies I want. I worked on Android since it was released for the first time but never full-time. It was going to change, and the world was open to me 🙂

Why did I decide to use Kotlin?

I was the only Android developer in my team. After one month an iOS developer joined us. At the same time Swift was entering into iOS world and it’s similar to Kotlin — not the other way ;) — so I thought it would be great to do code reviews between mobile platforms. Unfortunately, although it was a great idea, we couldn’t find time or motivation to actually do it. We merely discussed our solutions from time to time :/

Another more important reason was my strong urge to learn something new. I worked with Java for years, and I had enough. It was not moving any further. From the JVM languages, Kotlin seemed to be the best choice because of it creators — JetBrains. Same people were responsible for IntelliJ and indirectly Android Studio. So I expected IDE to work smooth and it did eventually.

Was it hard for me?

I checked my first Git commits. It was Kotlin 0.9.66. There was almost no questions on StackOverflow, no one in the office knew what am I talking about – “Ketchup!?” they asked.

In Poland, we do have a ketchup called Kotlin. Most Poles think of this brand when you ask them about it 🙂

It wasn’t the only new thing for me. I also started using RxJava 1.x and some other libraries I never used before. So there was a lot to learn. I don’t recommend to jump into too many things at once. I don’t regret it but if I had a chance to travel in time I would wait a bit with RxJava.

Most difficult part was changing my body habits. When I was thinking about class or function and started typing, my fingers were possessed and typed Java syntax. It took around one week to change it. After a month I felt being at home and decided never go back to Java.

 

Did I have any problems? Do they still exist?

Of course. Kotlin wasn’t officially released yet, and with every new version breaking changes were introduced. Fortunately, it wasn’t that hard to update and Kotlin plugin had an option to clean up the code and apply changes, really cool! Today we have version 1.1.2–4 and 1.1.3 is coming soon. Since 1.0 JetBrains promised to keep backward compatibility and so far they do.

The Most annoying issue was the compilation time, today not an issue! However, it doesn’t work with Instant Run. Personally I don’t care about that but maybe you do.

I wasn’t the only person in the world using Kotlin 🙂 , there was already a lot of contributors and JetBrains was really actively working on issues. I reported few myself and every time got feedback super fast.

I wasn’t able to implement everything in Kotlin. I had problems with libraries like Dagger 2 or Android Data binding. Today my code is 100% Kotlin, although I removed data binding – for reasons not related to my favourite language.

Not that far ago

In 2016 company hired another Android developer for a different project. There was a discussion whether Kotlin should be used. They took my advice and went for it. New Android developer had no experience in Kotlin at all, and he was working on it alone — with my support, of course. Right now I can’t recall how often he needed my help, but I think it was few times. It was very easy for him to simplify business logic that would be a hassle in Java. When I asked him today what he thinks about it, he says:

At first, I was sceptical about “some new” programming language on the Android platform.
New job, new project, new people are already enough for me. It seemed to me that
Java is one good way 🙂 But I was told: try Kotlin, start with a little steps, discover the language
and its possibilities, and you will never want to return to Java again … and that’s what happened 🙂
From that moment on, I use Kotlin for all my projects. Marcin Kitowicz

In late 2016 another Android developer joined our company, this time my team. Again, someone without Kotlin experience. And again it was super easy for him to learn it. And that’s what he says:

Kotlin is my favourite language now, and I don’t want to use Java ever again. Also, I have converted my entire private project into Kotlin. Works great! @Michał Kawałko

Based on these two I am pretty sure that you will have the same experience. An interesting fact is that for all of us it was much harder to learn how to use RxJava a proper way.

You will probably hear that you have to spend some time on it and there are some issues. Now point me a library/framework that doesn’t need a time or has no issues. I believe it’s comparable to applying any other dependency you have in your project. Maybe with a little bit more common sense 🙂 And that’s what I am going to write about next.

Get it done

I am not going to explain Kotlin features. You can find a lot of information on the web. I would just recommend starting with official documentation. No need to ask StackOverflow for basics.

Everything below is just my suggestion if you like to start easy with Kotlin. If you feel comfortable enough skip any tip you like.

Which feature should I start with?

None. It’s simple as that. It’s very easy to get carried away. Start with changing syntax to equivalent in Kotlin. For example, look at the Java code below:

String[] items = new String[]{"A", "B", "C"};
for (int i = 0; i < items.length; i++) {
    Log.d("TAG", "Log " + i);
}

Simple replacement in Kotlin could be:

arrayOf("A", "B", "C").forEachIndexed { i, _ -> Log.d("TAG", "Log $i") }

As you can see we have same thing done in one line. If it’s too big of a change you can do this:

val items = arrayOf("A", "B", "C")

for (i in items.indices) {
    Log.d("TAG", "Log " + i)
}

The purpose of the above code is to show that it can be written while maintaining a visually similar code structure. This approach is easier to digest at the beginning. It does not matter what the code is doing in this case.

Do I have to rewrite all my code line by line?

No. If you paste Java code into Kotlin file, Android Studio will ask you if you want to convert it to Kotlin on the fly. Choose YES. You can also open any Java class and choose from menu Code -> Convert Java File to Kotlin File. It’s a really good point to start if you are not sure how it should look in Kotlin. Of course, that tool is not perfect and most of the time converted code doesn’t apply all features automatically. You have to make optimisations yourself. If you look at my previous tip, it might be an advantage when migrating from Java.

Can I get rid of all NullPointerExceptions?

Kind of. Null Safety is one of the most advertised features. It forces you to declare whether an object can be null or not. But be careful:

  • when you implement classes from Java libraries always assume nullvalues are being passed, unless annotations are set stating that value is non-null
  • don’t overuse ?, it is often tempting to use it instead of checking if object is null. Your app may not crash but if your code in subsequent lines expect some logic to be executed your app may end up in an unstable state. Then it’s more difficult to investigate it. Use !! it’s the lesser of two evils.

If you feel in your guts that you should spend a little bit more time on deciding nullability, do it. It’s the right feeling, it was never forced in Java. That’s why you feel it now.

Would my RxJava experience matter?

Sure. If you are used to write reactive code you will find Kotlin collections really useful. You can try it out in places where you don’t need threading. Advantage of Kotlin is that you can get results directly from functions like mapflatMapfilter etc. without subscribing to the stream. By any means, Kotlin is not here to replace RxJava. They work together really nice, you just need to pick which one is better for specific situation. If I need threading or more complex operations I use RxJava, for everything else Kotlin.

I know basics, which feature should I start with?

I highly recommend data classes. This is most easy going and appreciated feature of all. All POJO classes you have, API models, view models etc. should be very easy to change into data classes. I am not going to explain here what it is, get used to visit documentation. Data classes are described here. I will only mention that it will reduce number of boilerplate code.

Anko to the rescue!

I never seen a project without utility classes. There is always something that doesn’t belong to specific business logic and it’s used across the app. Especially on Android platform there is a lot of complexity to communicate with the system, retrieve some information, calculate something and so on. There are out there libraries that already group those different common cases but if you want to move to Kotlin, you should definitely try Anko. It’s a library developed by JetBrains. It’s primary known for DSL used for creating views in the code. I prefer XML files but still use Anko for all great extensions it provides e.g. just call from Activity or Fragment

sendEmail("email@gmail.com", "Title", "Hello!")

and it will open email app with filled in values. Of course there is much, much more cool functions. They provide different modules of Anko, pick onethat you need.

Would I recommend to move to Kotlin?

Kotlin is my favourite language for some time. Since Google announced official support I am even more convinced that I did a right choice back then. It’s really easy to start. If you are still afraid, join community on official Kotlin slack and ask questions.

Written by Damian Petla
Android Developer
Published August 14, 2017