My Review Of Official Kotlin Shift For Android

Google has recently announced that it is going to remove all sorts of expected ‘;’” errors from Android Studio in their next major update for Android Studio, in version 3. The previous sentence was a total pun, and it was the way I first interpreted their shift from Java codebase to Kotlin. I don’t get the point of why everyone is hating the semicolon so much. Everyone keeps introducing new languages, and the most notable feature that distinguishes from the older languages is the semi-colon removal.

Enough gossip! Now, let’s talk about reality in the post and have a look at what we have right now which has caused tremendous amount of havoc on the internet, after those famous cat videos of course. 🙂

Is Kotlin new to Android?

Android now officially supports the Kotlin programming language, along with Java and the native programming using C++ language.Android
Figure 1
: Kotlin logo

Typical question would be: Is this a new programming language to learn? Definitely no, and it is definitely not a new language for Android as well. Kotlin had been in the game for awhile and if you have not yet heard about it, then you did not surf around in the Android world enough. Kotlin has been around for a quite a long time and most of the Android developers have already started using Kotlin language as their default language over Java. To me, the entire concept came as a new thing and at the first look, I was like, what the heck is this? Am I going to learn all of this now?

But, as the time passed, and I was able to convert and see the difference betweenJava and Kotlin, I came to realize they shared most of the concepts and syntax, however, some of the stuff resembles Swift, and some C++ (or even C#), and some of the concepts were taken from “defensive programming”, and so on and so forth. Although the language was impressive but the news that came on the Internet was not — as I did not watch the Google I/O 2017, I did not know what happened until I did watch it. I came to easily realize that the Kotlin language was not a new one, or that Google did not say that they are going to leave Java out and everyone would be required to program in Kotlin, they both (Java and Kotlin, even C++) are all interoperable. Your current projects would continue to be in Java, until or unless you want to migrate the code base from one language to another one; new one. This provides the developers with a flexibility to migrate the code base, or work on the existing one: Your Android dev team can be split up into other sections, C++, Java and now Kotlin as well. Interesting right (hehe, can’t imagine I said this…).
Android
Figure 2: Android promoting Kotlin, or vice versa.

So, the benefits are amazing and still counting, there will be much more by Google that you can anticipate getting and enjoying soon.

How to get Kotlin for Android

In my own opinion, it was a great step by Google to integrate the Kotlin language in Android Studio. Why? For several reasons:

  1. Android developers were already using Kotlin language for their projects.
  2. There was a plugin previously, that they needed to install to migrate the code base from Java to Kotlin. Also, it required some of the tweaking to the current build systems; to support Kotlin.
  3. IntelliJ based system, and IntelliJ based language. Why not, eh?

Thus, Google announced that they will start shipping Kotlin support, built in. Android Studio 3 (which is, at the time of writing the post, a Canary version), would be the first of the versions to get the Kotlin packages pre-installed and a complete support to develop Android applications in Kotlin language.

There are several other blog posts that you should consider reading if you are new to the entire Kotlin concept.

References

  1. Kotlin on Android
  2. Get Started with Kotlin

Improvements that Kotlin brings

Someone migh say, this is a better language as compared to the older one, right? Sadly, I am not that someone. Too much syntax simplicity makes me “think”! If we ignore the common improvements that Kotlin brings and have a look at what Android Studio offers, we can summarize this by saying,

“Android Studio speaks Kotlin”.

The most notable features of Android Studio 3 are that now, you can simply code your Java code from a Java class, and try to copy it in a Kotlin file, Android Studio will automatically convert that code from Java to Kotlin and print it there for you!

Android
Figure 3: Dialog from Android Studio confirming to convert Java code to Kotlin.

I also got a chance to actually carefully study the current benchmarking, and some other references by other experts on Kotlin and/or the people who have been working in the Kotlin environment, and I learned that you can actually love Kotlin for various reasons, and while being developed on top of (or beside) Java there are several ways in which Kotlin can be useful:

  • It has a better syntax sometimes, and a worse syntax in most cases.

    • Shorter and concise syntax, functions are an example of this, but uglier use of generics, lambdas and the inheritance is shameful. Most of the features were “stolen” from various languages, and shamefully shown to be working.
    • Of course, if the code compiles, it means, the language succeeded! Hello world.

  • Acts as a wrapper around the JVM, so you can expect that it will not let you roam around the sensitive areas — areas where an exception will result in the app not responding or app crashes.

  • Kotlin doesn’t bring much of  an improvement as far as performance goes, the reason being that the languages both compile down to the bytecode and that bytecode is what executes. A good program, with a better logic would run faster, but the language (Kotlin or Java) doesn’t much matter here.

  • On the other hand, Kotlin itself, brings around 7000 of extra methods to the overall API and somewhat an extra ~1MB of the APK file size — but Google suggests that you can minimize it, using ProGuard or other services.

    • The compilation time is still almost the same, sometimes Java has a benefit of around 17 percent, sometimes Kotlin wins and it all depends on what you’re doing, not what the language has; besides, they both boil down to JVM bytecode.
    • Language improvements are the most notable feature; the first thing that you notice in this is the size of the programs that you will write. The lines of codes are less.

But the thing is, your life as a developer would be much simpler, neater and cleaner once you get your hands dirty in the programming fest with Kotlin as compared to Java.

Finally, there were a few bad things that I saw in Kotlin, some of us are living a happy life provided the existence of the “static” modifier, Kotlin doesn’t support that. Please review the references URLs and see “The Bad” section to know more on this.

References

  1. Kotlin vs Java: Compilation Speed
  2. Kotlin: The Good, The Bad, and The Ugly

Want to learn Kotlin?

If you are a beginner to Kotlin, then you will be wanting to learn about the Kotlin language, fortunately, there is a tutorial and an online compiler that you can use. The online Kotlin resources provide a basic Hello World program that you can use for your own main learning goals. You should consider trying it out.

Secondly, there is also a feature provided where you can convert your Java code and learn what sort of Kotlin code gets generated to understand the typical Java ↔ Kotlin interoperability for the language tokens.

For example, I was using the online terminal and I converted the following code from Java to Kotlin, 

  1. public class Person {  
  2.     private int age;  
  3.     private String name;  
  4.    
  5.     public int getAge() { return this.age; }  
  6.     public void setAge(int age) { this.age = age; }  
  7.     public String getName() { return this.name; }  
  8.     public void setName(String name) { this.name = name; }  
  9.    
  10.     public void sayHello() {   
  11.         System.out.println("Hello, my name is " + getName());  
  12.     }  
  13.    
  14.     public int older(int years) {  
  15.         return getAge() + years;  
  16.     }  
  17.    
  18.     private void breathe() {  
  19.         System.out.println("I am breathing...");  
  20.     }  
  21. }   

The Kotlin code was like the following code,

  1. class Person {  
  2.     var age:Int = 0  
  3.     var name:String  
  4.   
  5.     fun sayHello() {  
  6.         println("Hello, my name is " + name)  
  7.     }  
  8.   
  9.     fun older(years:Int):Int {  
  10.         return age + years  
  11.     }  
  12.   
  13.     private fun breathe() {  
  14.         println("I am breathing...")  
  15.     }  
  16. }  

The screenshot of the web interface that provided this feature is as follows,

Android
Figure 4: A GUI to convert Java code to Kotlin.

If you forget about the semicolons and a few other extra things, you will notice there are still a lot of changes to the language and some are interesting, some are unfathomable. Even by this very simple example of code, you can easily realize that the Kotlin language -

  • Supports classes, and they are public (see the Java alternative code).
  • It supports the properties

    • Much more like C#, it does not have that getter setter style of encapsulating the private fields in public getters and setters.

  • The functions start with “fun”, but then become painful. 😀
  • Rest of the code is similar, you write the print line function, you can perform operations on the language’s primitive types (integers being added here).
  • You must provide the private modifier, public is default — or am I missing something?

I also must figure out a few of the things here, most of the things are handy, and easy to understand, but a majority of the stuff is still confusing for me as to why I should use them, or why to stick to the stuff I already have.

So, do try it out and if you want to learn the bleeding edge technology, consider using Android Studio 3 and start building Android applications using Kotlin language, out of the box.