How to Convert a Kotlin Source File to a Java Source File in Android? - GeeksforGeeks

17 Dec, 2021
We use Android Studio to translate our Java code into Kotlin while transitioning from Java to Kotlin. But what if we need to convert a Kotlin file to its Java equivalent? We’ll examine how to convert a Kotlin source file to a Java source file in this blog. Let’s get this party started. Because of its interoperability with Java, Kotlin grew at an exponential rate when it first appeared. This is because the Java Virtual Machine executes both Java and Kotlin (JVM).
Image #1: Understanding the conversion structure.
Compiling Kotlin code to JVM bytecode and subsequently decompiling the bytecode to Java code are the two processes involved in converting a Kotlin file to a Java file. As a result, converting Java code to Kotlin and vice versa is simple. Some of the benefits or motivations for translating Kotlin code to Java code are as follows:
To incorporate functionality that is simple to build in Java.To look into a performance issue.To remove Kotlin from your project, follow these steps.
To incorporate functionality that is simple to build in Java.
To look into a performance issue.
To remove Kotlin from your project, follow these steps.
Open your Kotlin project in your preferred IDE, here we are using IntelliJ
Then go to Kotlin > Show Kotlin Bytecode in Tools > Kotlin.Your Kotlin file’s bytecode will be returned to you.To extract your Java code from the bytecode, click the Decompile button.
Then go to Kotlin > Show Kotlin Bytecode in Tools > Kotlin.
Your Kotlin file’s bytecode will be returned to you.
To extract your Java code from the bytecode, click the Decompile button.
The Fernflower is used by IntelliJ IDEA behind the scenes. 
So, instead of utilizing IntelliJ IDEA, we may use Fernflower directly. The only demerit of using this method is that you will be getting the fern flower file which you will have to convert later on to pure java class. 
Do the compile process by using the command: kotlinc filename.ktNow we must decompile the class file created in the previous step.The fernflower.jar file can be downloaded here.After you’ve downloaded the jar file, use the command following to extract the Java file from your .class file:
Do the compile process by using the command: kotlinc filename.kt
Now we must decompile the class file created in the previous step.
The fernflower.jar file can be downloaded here.
After you’ve downloaded the jar file, use the command following to extract the Java file from your .class file:
java -jar fernflower.jar filename.class
Although the above procedure will generate a Java file, the code will be difficult to comprehend. In addition, reading will be poor. As a result, using IntelliJ IDEA to convert Kotlin code to Java code is suggested.
nnr223442
Picked
Android
Java
Kotlin
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Resource Raw Folder in Android Studio
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
How to Post Data to API using Retrofit in Android?
Retrofit with Kotlin Coroutine in Android
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
Arrays.sort() in Java with examples