The What, Why And How Of Kotlin For Android

Do you use Uber? Do you like Coursera? Do you enjoy the effects offered by Evernote? What do all these successful apps have in common? They all use Kotlin in one way or another. But what is Kotlin? And why is that important for Android programmers?  Read further because we are about to tell you all about it!

What Is Kotlin

Java isn’t the only official language on Android. Now developers can use Kotlin for building their Android apps. In May, last year, Android team announced support for Kotlin. After a few months, in November a new blog post highlighted that more than 17% of the projects built with Android Studio 3.0 were using Kotlin. As we were saying above, by this time many famous companies integrated Kotlin into their apps. It sounds very interesting but we should start with the official definition. Kotlin is a statically typed programming language for modern multiplatform applications. Kotlin was developed by JetBrains, who also built IntelliJ. Even though the company announced it in 2011 and released a preview in 2012, the first version was launched in 2016. So, it is a mature programming language. Kotlin is a JVM language and it is open sourced under Apache2. Therefore all developers can contribute to making it better. This is the simplest form of a program built with Kotlin.

Why To Use Kotlin When You Build Android Apps

Let’s make it clear from the start. We are not trying to convince you to forget all about Java and to start using Kotlin. We just want to discuss an alternative for building native Android apps. Even if Kotlin is very appreciated by developers, Google won’t stop adding new features for Java and C++. Actually, Android Oreo contains more Java 8 APIs and the team also continues to improve the support for C++ 17 in the NDK (Native Development Kit). So, why Google says you need to pay attention to Kotlin?

  • You won’t have to struggle with NullPointerExceptions anymore.
  • You won’t have to write findViewById() every time for the layout.
  • With default and named arguments, you will write less overloaded functions.
  • You can use lambda expressions for simplifying your code.
  • You can forget about inheritance because you have extension functions.
  • It is more concise than other languages.
  • You can use Kotlin classes in your Java code and Java classes in your Kotlin code.
  • You are able to convert Java code into Kotlin without any problems.
  • Google added support for Kotlin as a first-class language; so, it is suitable for building native apps.

How To Write Your Android App In Kotlin

There are a few steps you need to follow for creating an app using Kotlin.
1. First of all, like with any other programming language, you need to begin by learning the basics of Kotlin. You can find a detailed documentation on the official site and it is good to start from there. You will be able to download it in PDF format for reading it offline. It is easy to see how Kotlin works because you can write your code directly into the browser. And you should try Kotlin Koans tutorial specially created for teaching developers how to code. You must also check Android Kotlin Guides for a better understanding of Kotlin for Android. More details you will find in Resources To Learn Kotlin, a special folder on Android developers site.

2. It is good to know that Kotlin works with any Java IDE but because you want to build an Android app you need to have Android Studio installed on your computer. It is recommended to use Android Studio 3.0 because it offers support for Kotlin. For older versions, you should install the plugin. While the newest stable version at the moment of writing is Android Studio 3.1, Google already announced the preview for Android Studio 3.2.
3. At this point, you have more options as you can see in the official tutorial. So, you can:

  • Create a new project using Kotlin. This can be done very easy with the New Project Wizard, but you need to check Include Kotlin support and after a few steps you will be able to see your new app with Kotlin code. These files are saved in the same folder as Java files.
  • Add Kotlin code to your app. Inside your project you can select a template with File -> New and to choose Kotlin for Source Language in the wizard window. Again, click Next until you finish the process. Or, you can go to File -> New -> Kotlin File / Class for inserting a simple file.
  • Convert your Java code to Kotlin code. Open your Java file and click on Code -> Convert Java File to Kotlin File. In case you create a new Kotlin file (as explained above) and you copy your Java code to that file, you will see a message asking if you want to convert it to Kotlin code. Of course, select Yes and you will have your Java code transformed in Kotlin code.
  • Use Android APIs with Kotlin. When you call Android APIs in Kotlin you will find a lot of similarities with the same action in Java, the main difference between them would be the syntax. You will find more details about the interoperability with Java on Kotlinlang.org.

4. For further information we recommend you to go through the samples offered by Google for Kotlin.

Final Thoughts

We just presented you some elements that make Kotlin suitable for building native apps. The conclusion is that Kotlin is a powerful programming language and it is good to know that you have alternatives supported by Google. Now it is definitely up to you to decide if you are going to use Java, Kotlin or both (since it’s interoperable with Java) for writing your Android apps. Meanwhile, we would be happy to hear about your experience with Kotlin. What do you think about this additional solution offered by Google?


Mobile app owners: 

You can create, manage, and measure your own campaigns to acquire qualified and engaged users via App Samurai.

Start growing your business today! 


 

No Comments

Post A Comment