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.



Open any project in Android Studio, Here I have opened the HelloWorld App.

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 plugin starts its job and your .java files will convert to .kt files in the src folder.
  • convert java class to kotlin android studio

  • 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.
  • how to convert java class to kotlin in android studio 2

    After gradle configuration,

    1. ext.kotlin_version stores Kotlin version and uses in various spots.
    2. Added a dependency for Kotlin plugin
    3. How to convert Java class to Kotlin in Android Studio

    4. Kotlin plugin added
    5. 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.

    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.

    Conclusion

    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.

Please disable your adblocker or whitelist this site!