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 -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.
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.