Android Development Introduction Using Kotlin

Android Development Introduction Using Kotlin

Hi guys, its been a while since I wrote the last tutorial. Now, a lot has changed in Android Development. First, Google has announced a new programming Language for Android, know as Kotlin. Kotlin is a statically-typed programming language that runs on the JVM. It can also be compiled to JavaScript source code.

Ever since Apple released the Swift programming language for iOS, lots of Android developers have wanted a similar language for Android development. If you are one of those developers, you are going to love Kotlin, a JVM language that is remarkably similar to Swift.

Android Development Introduction Using Kotlin covers all the needed basics to start developing using Kotlin in your first simple app. Kotlin has some amazingly cool features! This is what we will be learning and using in this blog.

Kotlin is a statically-typed language, developed by JetBrains, with syntax more expressive and concise than that of Java. With features like higher-order functions, lambda expressions, operator overloading, string templates, and more, Kotlin has a lot more to offer than Java. Because Java and Kotlin are highly interoperable, they can be used together in the same project.

When creating native apps for Android, you will normally use the Java programming language for your logic. Whilst Java is a battle hardened language, it isn’t without its issues.
JetBrains, known for IntelliJ IDEA (Android Studio is based on IntelliJ), has introduced the Kotlin language.

Android Development Introduction Using Kotlin

Since Android took the world by storm, developers have had no alternatives to Java app development. Although its usage is widespread, Java comes with a lot of historical baggage.

Java 8 solved some language issues and corrected even more with Java 10. In order to get much of a benefit from the corrections made in these two releases, you have to set the minimum SDK to Android 24 just to use Java 8, which isn’t an option for many developers. For almost everybody, Java 10 isn’t even on the radar.

Kotlin aims to fill that gap of a missing modern language for the Android platform. There are a few core tenets that Kotlin lives by; it strives to be:

  1. Concise to reduce the amount of boilerplate code you need to write.
  2. Expressive to make your code more readable and understandable.
  3. Safe to avoid entire classes of errors such as null pointer exceptions.
  4. Versatile for building server-side applications, Android apps or frontend code running in the browser.
  5. Interoperable to leverage existing frameworks and libraries of the JVM with 100 percent Java interoperability.

Above all, it’s a new language! What could be more exciting? iOS developers can’t have all the fun. :]

Using Kotlin in Android

Using Kotlin into your Android application is quite easy — just follow these steps!

1. Installing Kotlin Plugin for Android Studio

In your Android Studio go to File -> Settings -> Plugins and click Browse Repositories and then search for Kotlin.

Click the “Install” button. Once the Kotlin plugin is installed, restart Android Studio

2. Creating the Project

Click File -> New Project

Enter the detail about the project, then select Phone and Tablet and click “Next”, select empty layout and click “Next”kotlin for android

Finally, Click Finish.

3. Java to Kotlin Conversion

By default, Android Studio creates the following Activity Template:

In order to convert this Java Template to Kotlin, Press CTRL + ALT + SHIFT + K (or) click Code -> Convert Java File to Kotlin. Doing this will convert Java files to Kotlin and the code becomes:

4. Configure Kotlin for your Android Project

Press shift twice in Android Studio Editor. You should be able to see finder, then you should type convert Kotlin

Then select Configure Kotlin in Project

Click the OK Button and Sync the project. Your main build.gradle becomes:

And your project build.gradle becomes:

Using the Kotlin Android Extension

The Android Kotlin extension gives an easy way to inject layout into your Kotlin activity:

Add the line above in your app’s build.gradle file and sync your project. And in the activity_main.xml

I created a TextView with id _helloText_. In the activity class, you can inject the TextView using their ID.

Run The Project

Hope this tutorial gave a glimpse of how useful Kotlin is for Android Development. You should consider it for your next project! Remember to leave a comment or a question below and I will get back as soon as possible. Have a nice one guys!

Happy Coding!

More From Android Study:

Leave a Reply

Your email address will not be published. Required fields are marked *