Category

Beginner

Category
how to convert java class to kotlin in android studio
how to convert java class to kotlin in android studio

In this post, you will learn how to convert Java class to Kotlin in Android Studio. Google announced Kotlin as an official language for Android Development. When comparing Java with Kotlin, It has lots of advantages. But I am not going talk about its advantages here.

If you are from Java and want to step into Kotlin world, But don’t know how to write your code in Kotlin. Don’t worry, Kotlin plugin helps to convert Java code to Kotlin in Android Studio. Just type your code in Java then use below steps

This is not 100% perfect, the Best way is to learn Kotlin on your own. But for beginners, This will help a lot to move forward with Kotlin.

For example, I have created a simple HelloWorld App and opened MainActivity.Java file.
Before going further, read on if you don’t install Kotlin plugin in Android Studio.

How to install Kotlin plugin in Android Studio.

9 Android WebView Example tutorials that had gone way too far

Beginner’s Guide: AsyncTask Android tutorial example

convert java class to kotlin android studio

Choose Code-> Convert Java File to Kotlin File or you can also use keyboard shortcut Ctrl + ALT + Shift + K.

convert java class to kotlin android studio

Now your .java files will convert to .kt files in src folder.

convert java class to kotlin android studio
Sometimes, You will get a notification says “Kotlin not configured”. Just click on Configure

configure kotlin plugin in android studio project

If you don’t get the configure option, then you need to go

Tools-> Kotlin -> Configure Kotlin in Project

convert java class to kotlin andorid studio

Select the latest Kotlin compiler and runtime version, and click OK.

Now Android Studio will configure Kotlin by adding dependencies and other stuff in your Gradle file.

You can also convert your java code to Kotlin – online.
convert java code to kotlin online

Go to this URL “https://try.kotlinlang.org/” and click on “Convert from Java”

how to convert java class to kotlin online
how to convert java class to kotlin online

Just paste your java code in the left section and click on “convert to Kotlin” shown on the bottom of the box.

Now Kotlin code will appear on the right section.

That’s all for now.

If you find anything useful please share it.