As mobile developers, we often rewrite the same logic in another language, maintaining two similar codebases for Android and iOS. Wouldn’t it be nice to write Kotlin once and compile it everywhere? Enter Kotlin Multiplatform, which enables us to write the same code for the JVM as well as LLVM!
In IntelliJ or Android Studio, you just have to do the following to get java code from kotlin:
Menu > Tools > Kotlin > Show Kotlin Bytecode
- Click on the
- Copy the java code
With a recent version (1.2+) of the Kotlin plugin you also can directly do
Menu > Tools > Kotlin -> Decompile Kotlin to Java.
While migrating from Java to Kotlin, we convert our Java code into Kotlin with the help of Android Studio. But what if we want to convert a Kotlin file to it’s equivalent Java file? So, in this blog, we will see how to convert a Kotlin source file to a Java source file. Let’s get started.
When Kotlin came into existence, it started growing exponentially because of its interoperability with Java. This is because both Java and Kotlin runs on Java Virtual Machine (JVM).
Converting a Kotlin file to Java file involves two steps i.e. compiling the Kotlin code to the JVM bytecode and then decompile the bytecode to the Java code. So, it becomes easy to convert the Java code into Kotlin and vice-versa.
Following are some of the advantages or reasons for converting Kotlin code to Java code:
- To integrate some feature that can be easily implemented in Java.
- To investigate some performance issue.
- To remove Kotlin from your project.