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.
Choose Code-> Convert Java File to Kotlin File or you can also use keyboard shortcut Ctrl + ALT + Shift + K.
Now your .java files will convert to .kt files in src folder.
Sometimes, You will get a notification says “Kotlin not configured”. Just click on Configure
If you don’t get the configure option, then you need to go
Tools-> Kotlin -> Configure Kotlin in Project
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.
Go to this URL “https://try.kotlinlang.org/” and click on “Convert from Java”
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.