Skip to main content

An introduction to Kotlin for Android development

Kotlin for Android development

For years, Java was the one official language for Android development. While other options were available through alternative tools like Unity or Xamarin, Java with the Android SDK was still the clear choice for those wanting to learn Android development the “proper” way. Then came Kotlin for Android.

Also read: I want to develop Android apps – What languages should I learn?

As of 7 May 2019, Kotlin has been the official “preferred” language for Android development according to Google. It has been available as a built-in feature of Android Studio and an official language for Android for longer than that.

So, should you use Kotlin for Android development? How is it different from Java? What is the learning curve like? Let’s find out.

Kotlin for Android development vs Java

Kotlin is similar to Java in many ways and runs on the Java Virtual Machine. It is 100% interoperable with Java, and thus there is not hit in performance when choosing to use Kotlin for Android development, nor increase in file size.

Kotlin Android development

Credit: Adam Sinicki / Android Authority

Kotlin does involve some changes in the way code is handled, however. As a general rule, Kotlin is a little simpler to read and easier to get to grips with as compared with Java. Kotlin requires less “boilerplate code.” This means there is less repetitive code necessary just to implement basic features. In many cases, two lines of Java code can be compressed into a single line of Kotlin.

Kotlin also does away with null pointer exceptions, and even lets you off the hook when it comes to ending lines of code with semi-colons!

For these reasons, Kotlin represents an easier jumping-on point for many new Android developers. And the fact that it is now the preferred option according to Google, means that you can guarantee there will be great support for the language going forward.

Why you might still choose Java

That’s not to say that Kotlin for Android development is perfect.

While Kotlin might be the preferred official language, the fact remains that Java has been around as a primary option for much longer – since way back when most people used Eclipse IDE to make their apps!

What this means is that many of the big apps on the Play Store will be built using Java. There’s not a huge amount of incentive for those developers to rewrite their code in Kotlin, and as such, they are more likely to hire developers that know Java.

Java vs Kotlin for Android develpoment

Not only that, but Java is used much more widely outside of Android development. In fact, Java is regularly listed as one of the most in-demand programming languages for employers, alongside Python. Kotlin is further down the list.

Simply: more people know Java than know Kotlin. And with so Java so heavily entrenched, it may be easier for companies to start new projects in Java too. It certainly makes more sense for a developer to learn Java if they hope to work in the industry and don’t want to focus solely on Android.

An introduction to coding in Kotlin

If you want to have a go at Kotlin development for Android, the good news is that it’s now extremely simple to get set-up. Simply start a new project in Android Studio and choose Kotlin as your preferred language.

Also read: A guide to Android app development for complete beginners in 5 easy steps

When you start that new project, you’ll be greeted with some code by default. This is the code necessary to display “Hello World” on the screen and assign a starting point for your program along with a layout file. In other words, this is the boilerplate we were talking about earlier! Let’s take a look at what’s here and what it can teach us about Kotlin.

First, you may notice that you declare classes using class just as you would in Java. The difference is that there’s no public keyword, which is because all classes in Kotlin are public and final. For those unfamiliar with what this means, it means that other activities outside of this file can access the functions you create. You might also notice that we aren’t using extend to inherit the properties of the superclass, either. Instead, we use a colon which does the same thing.

What about that fun command? This is short for “function”; so instead of writing public void you’ll now write fun. Arguments are specified in brackets following the function name. You’ll also need to know how to define variables, which is a little different in Kotlin for Android development. To create a string, you might write:

var text: String = “Hello”

Kotlin is usually smart enough to identify a variable’s type on its own though. just like Python, so you can generally just write:

var text = “Hello”

To create an integer, you could simply write:

var num = 3

This is how you would create a mutable (changeable) variable, whereas val is used to create constants.

For a much more in-depth introduction to Kotlin syntax and structure, and how it differs from java, check this post:

How Kotlin saves you time and busywork

A lot of the time, code will look a fair bit simpler and shorter in Kotlin for Android as compared with Java. Consider the following example of adding an onClickListener to a floating action button (FAB). Here is how you would do it in Java:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
 fab.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
     ...
     }
 });

And this is the same thing in Kotlin:

val fab = findViewById(R.id.fab) as FloatingActionButton
 fab.setOnClickListener {
 ...
 }

Like I said: less boilerplate. In fact, Kotlin developers can do away with ever having to write findViewByID again! To try this out, first apply a plugin.

findViewByID in kotlin for android

Add the following to your module-level build.gradle:

apply plugin: ‘kotlin-android-extensions’

Click “sync” and you’ll then be able to import references to your views right at the top of your code, like so:

import kotlinx.android.synthetic.main.<layout>.<view-id>

Now you can access the view directly with no need to use its ID. This makes life much simpler and can save you writing a lot of arbitrary code.

arbitrary code becomes obsolete with kotlin for android


Combining these techniques you can save yourself a whole lot of busywork and you’ll find many more useful time-saving strategies going forward.

Hopefully, this gives you a basic introduction to Kotlin for Android development, and an idea of how it differs from Java. Which is right for you will depend on your personal preferences and goals. But more choice is never a bad thing!



source https://www.androidauthority.com/kotlin-for-android-development-1132127/

Comments

Popular posts from this blog

5 tips to Voice Speech Recognition in Android Marshmallow

Android Marshmallow landed on our Android devices. The opportunity for us to offer a small compilation of tricks to try immediately. The latest version of the Google OS starts (very gently, unhurriedly) to be offered on other devices as Nexus. You will find on Android TM, in the coming weeks, a compilation of the best tips for Android 6.0 Marshmallow. It starts slowly with a video featuring the 10 essential tips & tricks of this new version of the little green robot OS. To continue, we offer a selection of five "tricks" on the management of the battery on Android 6.0 Marshmallow. To enjoy longer your newly updated mobile. Follow the guide. then continue with 5 tips to tame the super-assistant Google Now on Tap. Here you will find 5 "tips" to manage in the best way your applications. We then discuss the quick tips to navigate more easily on this version of the Google OS. We enchanters with features focused on safety and the protection of personal data. We co...

Energy Android TV Play turns your TV into a Smart TV

ENERGY SISTEM Android Energy TV Play, you have a smart TV with Android operating system allows you to convert any traditional TV has announced the launch of a new product. Energy Android TV Play can be connected to the TV to enjoy f the size of a flash drive, a smart phone, a tablet and a computer unconsolidated is a lightweight device. System 1.6 GHz, DDR3 1GB of RAM and a dual-core processor can be expanded using external USB devices, which is the internal memory of 8 GB. It also integrates WiFi and a USB port for connecting external devices. One of its outstanding features, it is easily connected to the TV screen by screen cast application to display the contents of any terminal, making any phone or tablet is synchronized with iOS or Android. All ENERGY SISTEM products one click In addition, through streaming media service applications, images, video or other multimedia content, and game play is the ability to share. With integrated WiFi, the device you want from t...

How to run Python apps on any platform

Credit: Adam Sinicki / Android Authority Want to know how to run Python? It sounds simple, but it can actually be tricky to figure this out. In this post, we’ll discuss how to test your Python code, as well as how to run Python in other contexts: online for example, or as a packaged app. Sometimes, the thing holding you back from learning to code can be extremely simple. I remember wanting to learn to program when I was younger – or learning to take what I’d learned from BASIC on the ZX Spectrum and apply that to a modern environment. My problem? I didn’t know “where” to program. Once I understood C# or Java, where would I enter the code and how would I run it? And whenever I asked someone, they would look at me blankly. What kind of a question is that? Thing is, I had never needed an IDE or an interpreter before. Machines like the ZX Spectrum and Tatung Einstein (any other Einstein users out there?) simply booted up with a prompt to code into! Many people have a similar iss...