In our previous tutorial, we learned how to create a new Android application with Kotlin support. In this Android Tutorial, we shall learn to convert Java Files in Android Application to Kotlin Files or Classes.

Quick Information: Existing Java Classes could be converted to Kotlin Classes using

(Studio’s Menu Bar) Code -> Convert Java File to Kotlin File

Convert Java Files in Android Application to Kotlin Files or Classes

Now, we shall see a step by step process of how to convert Java Files in Android Application to Kotlin Files and Run the Application on an Android Phone.

  1. Open existing Android Studio project

    Start Android Studio 3.0 and click on “Open an existing Android Studio project”.

Open existing Android Studio project - Convert Java Files in Android Application to Kotlin Files or Classes - Kotlin Tutorial - www.tutorialkart.com
ADVERTISEMENT
Browse Existing Android Studio project - Convert Java Files in Android Application to Kotlin Files or Classes - Kotlin Tutorial - www.tutorialkart.com

In this already existing Android Studio project, we have two activities namely “MainActivity.java” and “SecondActivity.java”

Existing Android Studio project with Java files - Convert Java Files in Android Application to Kotlin Files or Classes - Kotlin Tutorial - www.tutorialkart.com
  • Convert Java File to Kotlin File

    Open MainActivity.java. Click on “Code” in Menu bar, and then on “Convert Java File to Kotlin File”. Repeat the process for all the Java Classes in your Android project that you want to be converted to Kotlin Classes.

  • Convert Java Files in Android Application to Kotlin Files or Classes - Kotlin Tutorial - www.tutorialkart.com
  • Configure Kotlin

    If you get a message “Kotlin not configured”, Click on “Configure” link provided in the message. Keep the defaults in the dialogue box “Configure Kotlin in Project” and click “OK”.

  • Configure Kotlin - Convert Java Files in Android Application to Kotlin Files or Classes - Kotlin Tutorial - www.tutorialkart.com

    If Gradle Sync is required, proceed with the sync.

    Once the Gradle sync is done, the project should be without any errors as shown below.

    All Java Files converted to Kotlin Files - Convert Java Files in Android Application to Kotlin Files or Classes - Kotlin Tutorial - www.tutorialkart.com
  • Run the application.

  • Android Application MainActivity - www.tutorialkart.com
    Android Application SecondActivity - www.tutorialkart.com

    Conclusion

    We have successfully learned how to Convert Java Files of Android Studio project to Kotlin Files, and we have confirmed the working of our old Android application with the new Kotlin files on an Android phone. In our next tutorial, we shall the differences between Kotlin and Java.