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 to 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.
If you are using below Android Studio 3.0, You must install Kotlin plugin. Then only below steps work. So go to this tutorial and install Kotlin plugin in Android Studio. Otherwise, Just read on.
- RecyclerView In Android Ultimate Tutorial
- 9 Android WebView Example tutorials that had gone way too far
- Beginner’s Guide: AsyncTask Android tutorial example
Open any project in Android Studio, Here I have opened the HelloWorld App.
- Choose Code-> Convert Java File to Kotlin File or you can also use keyboard shortcut Ctrl + ALT + Shift + K.
- Now plugin starts its job and your .java files will convert to .kt files in the src folder.
- 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.
After gradle configuration,
- ext.kotlin_version stores Kotlin version and uses in various spots.
- Added a dependency for Kotlin plugin
- Kotlin plugin added
- Kotlin standard library dependency added.
- you don’t have to convert each java file one by one for a big project, you can convert the whole package using the above method.
- The plugin also converts automatically if you paste java code into Kotlin file.
JetBrains team does not stop making java to Kotlin offline tool, they also provide an online tool.
Convert 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.
Kotlin has become one of the fastest growing languages after Google has selected it as an official language for Android Development. Many of the business giants are also started making big projects in Kotlin. If you are a beginner in Kotlin and know how to code in Java. Don’t worry you can convert it to Kotlin using the Kotlin plugin. Android Studio makes the process more simple, so you can accomplish migration in a few seconds. For the beginning days, It assists you well to understand the basic structure of Kotlin. I assume this post might have helped you and If you like this post, please share it. Any other ways to convert, feel free to comment here.