- Convert a Java File Code to Kotlin
- Why Convert Java Source File to Kotlin
- Convert Entire Java File to Kotlin
- Convert Java to Kotlin Using Android Studio 3.0
- Convert Java to Kotlin in Earlier Android Studio Versions Before 3.0
- Convert a Few Java Code Lines to Kotlin
- Related Article — Kotlin Java
- Java Files to Kotlin Classes conversion in Android Application
- Convert Java File to Kotlin File
- Configure Kotlin
- Run the application.
- Conclusion
- How to Convert Java to Kotlin and Kotlin to Java
- Converting Kotlin to Java Using IntelliJ IDE Software and Android Studio
- Converting Java to Kotlin Using IntelliJ IDE Software and Android Studio
- Converting Kotlin to Java Using Fernflower Command Line Utility
- Conclusion
- About the author
- Nitesh Kumar
Convert a Java File Code to Kotlin
- Why Convert Java Source File to Kotlin
- Convert Entire Java File to Kotlin
- Convert a Few Java Code Lines to Kotlin
Kotlin is now the official Android development language, as declared by Google in 2017. Hence, it is evident that developers might wish to convert their Java code to Kotlin.
If you wonder how to do that, this article will introduce some basic steps to convert your Java file to Kotlin.
There are two scenarios where you might look out to convert Java to Kotlin: converting the entire file or converting a piece of code.
But before getting into converting Java source files to Kotlin, let’s understand why someone would want to do so.
Why Convert Java Source File to Kotlin
There are many reasons a developer would like to convert Java to Kotlin. The primary reason is that Kotlin is now the official Android development language.
- Kotlin is easier to read and understand than Java.
- Both use the same JVM compiler.
- Kotlin can handle significant exceptions like the null pointer exception.
- Kotlin code is cleaner.
These reasons are good enough for a developer to choose Kotlin over Java and convert the source code.
Convert Entire Java File to Kotlin
Since Kotlin is now the official Android development language, converting Java files to Kotlin has become easy.
Now, there are again two scenarios here. You will either be using Android Studio 3.0 or the earlier versions.
We will go through both scenarios in this article.
Convert Java to Kotlin Using Android Studio 3.0
You first need to open the Java file you want to change in the Android Studio.
Once the file is open, navigate to Android Studio Menu -> Code -> Convert Java File to Kotlin File . Alternatively, you can also use the shortcut keys Ctrl + Alt + Shift + K .
A dialogue box will open to ask permission for configuring Kotlin in the project and convert the code.
Click on OK to give permission and let Android Studio change the build.gradle file and convert the Java file to Kotlin.
Convert Java to Kotlin in Earlier Android Studio Versions Before 3.0
For the versions before Android Studio 3.0, we need to install the Kotlin plugin to complete the conversion.
To install the plugin, navigate to Android Studio Menu -> File -> Settings -> Plugins -> Browse Repositories -> Kotlin .
Once the plugin is installed, open the Java file you wish to convert and follow the four steps mentioned above to convert Java to Kotlin in Android Studio 3.0.
Convert a Few Java Code Lines to Kotlin
- Open the Kotlin file with the .kt extension where you wish to convert the code.
- Open the Java file and copy the code you desire to convert to Kotlin.
- Paste the Java code in the Kotlin file.
- Android Studio will recognize the Java code and ask whether you wish to convert it to Kotlin.
- Allow the Android Studio to make the conversion, and it will convert your Java code to Kotlin.
Kailash Vaviya is a freelance writer who started writing in 2019 and has never stopped since then as he fell in love with it. He has a soft corner for technology and likes to read, learn, and write about it. His content is focused on providing information to help build a brand presence and gain engagement.
Related Article — Kotlin Java
Java Files to Kotlin Classes conversion in Android Application
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
In this already existing Android Studio project, we have two activities namely “MainActivity.java” and “SecondActivity.java”
Convert Java File to Kotlin File
Configure Kotlin
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.
Run the application.
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.
How to Convert Java to Kotlin and Kotlin to Java
This article will cover a guide on converting code written in the Kotlin programming language to Java programming language and vice versa. Kotlin is a relatively new programming language being developed by JetBrains and it is fully interoperable with Java programming language. It offers some benefits over Java programming language like a more concise syntax, more built-in helper functions, stricter null type checking, data classes, and so on. Full list of differences between these two languages is available here. Kotlin is now the preferred language for developing Android apps and it has been fully integrated into Android Studio app development software suite.
You can convert Kotlin to Java and Java to Kotlin using offline tools. Some of them are explained in this article. Do note that depending on the code being converted and the type of tool being used for the conversion purpose, the converted code may not be 100% accurate and you may have to make some manual edits. You should always review converted code before using it in an application.
Converting Kotlin to Java Using IntelliJ IDE Software and Android Studio
You can use Android Studio app development software suite to convert Kotlin code to Java. Android Studio can be downloaded from here. After downloading, launch Android Studio and create a new Kotlin application project according to your requirements. You can also import your existing Kotlin projects to Android Studio.
With a “.kt” file open in Android Studio editor, click on Tools > Kotlin > Show Kotlin Bytecode menu entry (as shown in the screenshot below).
You will get Kotlin bytecode in a separate sub-window once the process finishes.
Click on the “Decompile” button in the same window to convert the code to Java.
You will get a new file with the same name but with a “.java” extension instead of the original “.kt” extension. This file will contain the converted Java code and will have “decompiled” keyword in its file name.
You can also use IntelliJ IDE, an open source and commercial software suite for developing applications in Java and Kotlin, to convert Kotlin code to Java. The steps to convert Kotlin to Java are the same as above, as Android Studio itself is based on IntelliJ IDE software. You can download IntelliJ IDE software from its official website.
Converting Java to Kotlin Using IntelliJ IDE Software and Android Studio
You can use both Android Studio and IntelliJ IDE software to convert Java code to Kotlin. If you are using a Kotlin based project in Android Studio or IntelliJ IDE software and try to copy/paste Java code, you should get a prompt to automatically convert Java code to Kotlin (as shown in the screenshot below).
Another method is to create a “.java” file inside your project directory or add an existing “.java” file to your project and then convert code contained in the entire file into Kotlin. To do so, open a “.java” file located in your project folder in Android Studio editor and click on Code > Convert Java File to Kotlin File menu entry (as shown in the screenshot below).
Once the conversion process has finished, you will get a new “.kt” file with the same name and the original “.java” file will be removed from the project folder.
As stated earlier, these steps should work with IntelliJ IDE software as well.
Converting Kotlin to Java Using Fernflower Command Line Utility
Both Android Studio and IntelliJ IDE software use a Java library and command line utility underneath to convert Kotlin code to Java. This utility is called “Fernflower” and it can be compiled from its source code available here using the Gradle build tool. You will get a “fernflower.jar” file once the build process is completed successfully. This command line tool is also available in downloadable archives of Android Studio and IntelliJ IDE software under the “plugins/java-decompiler/lib/” folder and it is named as “java-decompiler.jar”.
To convert a “.kt” file using “fernflower.jar” or “java-decompiler.jar” files, you will first need to compile it to a “.class” file using Kotlin compiler. The official Kotlin command line compiler can be installed in Linux by following instructions available here. Once you have Kotlin command line compiler or “kotlinc” installed in your Linux system, run a command in the following format to convert a “test.kt” file to “TestKt.class” file.
Depending on the name of the “.kt” file, kotlinc will automatically assign a suitable name to the “.class” file. You can now convert “.class” file to “.java” file using one of the following two commands:
$ java — jar fernflower. jar TestKt. class .
$ java — cp java — decompiler. jar org. jetbrains . java . decompiler . main . decompiler . ConsoleDecompiler TestKt. class .
The decompiler may take some time to convert the code depending on the complexity and scale of your program. Once the decompilation process is finished, you should get a “.java” file (TestKt.java file in this case) containing converted code in Java.
Conclusion
Since Kotlin was made the preferred language for developing Android apps, its adoption has increased considerably. App developers may want to convert Java code to Kotlin to keep their application up-to-date. The tools built into Android Studio and IntelliJ IDE software allows them to convert code both ways and makes it easy to convert code without much manual input.
About the author
Nitesh Kumar
I am a freelancer software developer and content writer who loves Linux, open source software and the free software community.