How to Convert Kotlin Code to Java Code?

Kotlin is a language that is well known for the interoperability with the java language. Kotlin is good for Mobile-cross platform, Native, Data-Science, Server-side, Web-development, Android. Interoperability is possible in both the languages because of the JVM (Java Virtual Machine) or in other words we can say that they are compiled to the same bytecode.

How to Convert Kotlin Code to Java Code

Image Source

If we want to convert java to kotlin we can easily do it with the help of Android Studio. But if we want to do the vice versa how we can do that. This we will learn it in the following tutorial. So let’s start.

When we want to learn that how can we convert Kotlin to Java. The first question that arises is why we need to convert Kotlin to Java.

  • So that we can remove kotlin from our project.
  • Integrate some functionalities that can easily be implemented in JAVA.
  • So that we can investigate or see the performance issues.

The conversion of Kotlin to Java basically consists of two tasks:

  • The first step consists of converting the code in Kotlin to the JVM Bytecode.
  • The second step is to decompile the produced code to the Java code so that we can see the result.

How to Convert Kotlin Code to Java Code?

First Method:

Suppose we have a file named as Demo.kt which has the following code snippet:

fun EvenNumbers() {
    for (i in 0..10 step 2) {
        println(i)
    }
}

When we compile the code with the command that will be:

Kotlininc Demo.kt

Now this file has been created. We need to convert this file to Java.

So for that we will be using the FernFlower. We can easily get the fernflower.jar by downloading the project and running the gradle once. Once when the jar file is with us, now we have to run a command that is:

Java -jar fernflower .jar Demokt.class

This will convert the kotlin code to the Java code. The code would look like:

import kotlin.Metadata;
import kotlin.ranges.IntProgression;
import kotlin.ranges.IntRange;
import kotlin.ranges.RangesKt;
 
@Metadata(
   mv = {1, 1, 15},
   bv = {1, 0, 3},
   k = 2,
   d1 = 
     {"\u0000\u0006\n\u0000\n\u0002\u0010\u0002\u001a\u0006\u0010\u0000\u001a\u00020\u0001"},
   d2 = {"EvenNumbers", ""}
)
public final class SampleKt {
   public static final void printEvenNumbers() {
      byte var3 = 0;
      IntProgression var10000 = RangesKt.step((IntProgression)(new IntRange(var3, 10)), 2);
      int i = var10000.getFirst();
      int var1 = var10000.getLast();
      int var2 = var10000.getStep();
      if (var2 >= 0) {
         if (i > var1) {
            return;
         }
      } else if (i < var1) {
         return;
      }
 
      while(true) {
         boolean var4 = false;
         System.out.println(i);
         if (i == var1) {
            return;
         }
 
         i += var2;
      }
   }
}

This is the one way of converting the Kotlin code to the Java.

Second Method:

Now we will be learning the alternate way to convert the kotlin code to java code. This we will be doing with the IntelliJ IDEA.

To compile the source code of kotlin to the bytecode follow the below steps:

  • First Code
  • Click Tools
  • Click Kotlin
  • Click Show Kotlin Bytecode from the context menu

Convert Kotlin Code to Java Code 1

After that we can see the readable form.

Note: The IntelliJ IDEA also uses the fernflower in the background. So the code will be similar to the above code.

// ================MainKt.class =================
// class version 50.0 (50)
// access flags 0x31
public final class MainKt {


  // access flags 0x19
  public final static main([Ljava/lang/String;)V
    // annotable parameter count: 1 (visible)
    // annotable parameter count: 1 (invisible)
    @Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 0
   L0
    ALOAD 0
    LDC "args"
    INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkNotNullParameter (Ljava/lang/Object;Ljava/lang/String;)V
   L1
    LINENUMBER 3 L1
    BIPUSH 120
    ISTORE 1
   L2
    LINENUMBER 4 L2
    BIPUSH 20
    ISTORE 2
   L3
    LINENUMBER 8 L3
    NOP
   L4
    LINENUMBER 10 L4
    LDC "a is greater"
    ASTORE 3
   L5
    ICONST_0
    ISTORE 4
   L6
    GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
    ALOAD 3
    INVOKEVIRTUAL java/io/PrintStream.print (Ljava/lang/Object;)V
   L7
   L8
    GOTO L9
   L10
    LINENUMBER 15 L10
   L9
    LINENUMBER 18 L9
    RETURN
   L11
    LOCALVARIABLE b I L3 L11 2
    LOCALVARIABLE a I L2 L11 1
    LOCALVARIABLE args [Ljava/lang/String; L0 L11 0
    MAXSTACK = 2
    MAXLOCALS = 5

  @Lkotlin/Metadata;(mv={1, 4, 0}, bv={1, 0, 3}, k=2, d1={"\u0000\u0014\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u0011\n\u0002\u0010\u000e\n\u0002\u0008\u0002\u001a\u0019\u0010\u0000\u001a\u00020\u00012\u000c\u0010\u0002\u001a\u0008\u0012\u0004\u0012\u00020\u00040\u0003\u00a2\u0006\u0002\u0010\u0005\u00a8\u0006\u0006"}, d2={"main", "", "args", "", "", "([Ljava/lang/String;)V", "Ifdemoapp"})
  // compiled from: main.kt
}


// ================META-INF/Ifdemoapp.kotlin_module =================

These are two ways by which we can convert the Kotlin code to Java. Comment down below if you have any queries.

Leave a Comment