Skip to main content

Introduction to Kotlin on Android



I'm studying Kotlin, and I thought this site would be an excellent place to share things that I've learned and hopefully help someone that is on the same journey.

In this blog post, I'll explain what is Kotlin and review some of the few nice features of the language, and we'll create a Hello World Android app using Kotlin. Let's get started.

What is Kotlin?

Kotlin is a new programming language created by JetBrains that targets Java platform.

Kotlin is:

- Concise: Kotlin syntax clearly expresses the intent of the code you read and doesn't obscure it with boilerplate code.
-Pragmatic: Kotlin is a useful language intended to solve problems real-world problems.

-Interoperable: Kotlin can work with Java seamlessly. No tricks required: Kotlin classes and methods can be called exactly like regular Java classes and methods. The IDE provides full support for cross-language projects. Making it easy to navigate freely between Java and Kotlin source files, debug mixed-language projects and step between code written in different languages.

-Safe: Kotlin help developers find possible errors by adding checks at compile time instead of failing at runtime.

Most important, Kotlin attempts to remove the NullPointerException from your program. Let's take a glance at the next example:


By adding only one single character; a question mark at the end, Kotlin's type system keeps track of values that can and can't be null and forbids operations that can lead to an NPE, helping in reducing the number of application crashes.

Kotlin also helps avoid the ClassCastException by combining the check and the cast into a single operation. Let's see an example:


Kotlin can be utilized almost everywhere Java is used today. Kotlin works excellent with all existing Java libraries and frameworks and runs with the same level of performance as Java.

Let's analyze a few exciting features of Kotlin by using the following example:


This example defines a Puppy class, creates a collection of puppies, finds the last one, and prints the result. The features of Kotlin language used on the example were:

- Data class
- Top-level function
- String template
- Nullable type ?; default value for arguments
- Named argument
- Lamda expression; Elvis operator
- Autogenerated to String

Kotlin on Android

- Kotlin language features and Android Studio turn Android development into a more productive and pleasurable experience. Adding listeners, binging layout elements to fields, can be done with less code, or sometimes no code at all thanks to the compiler that works for us and generate the necessary code.

- Anko is a library built by the Kotlin team, improves Android development by adding Kotlin-friendly adapters around many standard Android APIs.

-Handle & track nullability in your code. When using Kotlin, it is easier to keep track of null values that may lead to a NullPointerException. Most of the code that would cause an NPE in Java fails to compile in Kotlin.

- From a performance standpoint, Kotlin code is executed as efficiently as Java code.

Let's build in 5 minutes a Hello World app using Kotlin on Android.

For this tutorial, you need to have Android Studio already installed on your computer. If you haven't installed AS yet, I created a quick tutorial how to Install Android Studio. 




Conclusion

We've reviewed Kotlin and some of its cool traits, that I can tell you have been helping my team and me at work to write code faster and in a more idiomatic fashion. I believe Kotlin is the future of Android development. If you are serious about the Android development ecosystem, you should give it a shot, and I can guarantee you, after writing a few classes using Kotlin you won't miss Java.

In the next blog post, we'll review functions, variables, classes, enums, and properties. As well as control structures, smart casts, and throwing and handling exceptions and how to apply those concepts in our Android application.

Comments

Popular posts from this blog

How to use gradle to find dependency tree

A quick guide to creating an easy to read report of your dependency tree using Gradle.  Inspecting Dependencies  Gradle provides enough tools to navigate large dependency graphs and solve situations that can lead to  dependency hell . You can choose to render the full graph of dependencies as well as identify the selection reason and origin for a dependency. Listing dependencies in your project Rendering the dependency tree is particularly useful if you'd like to identify which dependencies have been resolved at runtime and get valuable information in case any dependency conflict resolution occurred. The dependency report will contain declared and transitive dependencies. Every Gradle project provides the task dependencies for rendering the so-called dependency report from the command line. By default, the dependency report presents dependencies for all configurations. To print information for a specific configuration, you need to provide the optional parameter