457

I have a Kotlin source file, but I want to translate it to Java.

How can I convert Kotlin to Java source?

Andy Fedoroff
  • 26,838
  • 8
  • 85
  • 144
activedecay
  • 8,238
  • 3
  • 40
  • 54
  • 12
    I'm pretty sure no automated tool has been built for this yet. You can build it first! – Eric Cochran Jan 22 '16 at 23:37
  • 8
    It's very likely that you will end up with an ugly and unmaintainable Java class which won't run if you don't have the Kotlin standard library in the classpath. What's the point? – yole Jan 23 '16 at 08:08
  • 3
    Consider the j2objC translator. Would be great if you could do kotlin -> Java -> objC – Patrick Apr 01 '16 at 18:44
  • 1
    @Patrick Kotlin/Native now supports interop with Objective-C and multi platform projects, so you can share code now ;) – Louis CAD Nov 23 '17 at 14:41
  • 3
    The goal of the "Decompile" button is to help people understand how the Kotlin compilation works. The Java code it generates is not intended for use as actual production code (and is quite poorly suited for that - to begin with, it does not always compile...) – yole May 15 '18 at 19:12
  • `Decompile Kotlin to Java` is currently enabled only for [compiled Kotlin classes](https://stackoverflow.com/a/52462123/3290339). – Onik Sep 28 '18 at 09:22
  • 2
    @yole how about for somebody who doesn't know Kotlin or have time to learn it and has got some sample code in that language which has some functionality they need to understand to get their Java code to work? – Michael Jan 18 '19 at 18:51
  • 1
    @Michael As you can imagine, the goal of the Kotlin team is not to help people avoid learning Kotlin. Learning enough to understand what a code sample does will not take you long. – yole Jan 21 '19 at 08:36
  • We have to convert Kotlin to Java in order to read crash-log, see https://stackoverflow.com/questions/52969958/kotlin-code-stack-trace-shows-java-line-numbers. – CoolMind Jun 27 '19 at 09:31

9 Answers9

478

As @Vadzim said, in IntelliJ or Android Studio, you just have to do the following to get java code from kotlin:

  1. Menu > Tools > Kotlin > Show Kotlin Bytecode
  2. Click on the Decompile button
  3. Copy the java code

Update:

With a recent version (1.2+) of the Kotlin plugin you also can directly do Menu > Tools > Kotlin -> Decompile Kotlin to Java.

Louis CAD
  • 9,299
  • 2
  • 31
  • 50
  • 70
    Is the Java output not ugly? – Pacerier Jun 17 '17 at 07:31
  • 25
    @Pacerier Just like most decompiling outputs, it is, of course – Louis CAD Jun 17 '17 at 12:27
  • 6
    converted from kotlin to java. But its showing extra code. the code that i even never wrote or heard – Nauman Ash Jul 20 '17 at 10:42
  • 4
    @nauman Because you wrote Kotlin code, not Java. Kotlin features rely on bytecode, and doesn't necessarily translates to Java like you would expect. There's also extra checks, for nullability primarily, and synthetic methods and fields. – Louis CAD Jul 20 '17 at 14:45
  • 29
    that option is not enabled, it is coloured grey. How do i enable it. – Vikas Pandey Jul 12 '18 at 06:07
  • 13
    @VikasPandey, `Decompile Kotlin to Java` is currently enabled only for [compiled Kotlin classes](https://stackoverflow.com/a/52462123/3290339). – Onik Sep 28 '18 at 13:55
41

You can compile Kotlin to bytecode, then use a Java disassembler.

The decompiling may be done inside IntelliJ Idea, or using FernFlower https://github.com/fesh0r/fernflower (thanks @Jire)

There was no automated tool as I checked a couple months ago (and no plans for one AFAIK)

Gastón Saillén
  • 9,076
  • 4
  • 39
  • 58
voddan
  • 26,078
  • 7
  • 69
  • 77
  • 2
    As a side note, be aware that generics in generated code can cause compiler warnings, since Kotlin allows some things that the java compiler doesn't. It still works in byte code, though. – Jacob Zimmerman Jan 23 '16 at 17:03
  • @voddan I think he's referring to reified generics, which aren't a possibility in pure Java. – Jire Jan 24 '16 at 09:36
  • 1
    @Jire reified generics are always inlined, so from Java they look like normal class references – voddan Jan 24 '16 at 09:45
  • 3
    This is what I'm talking about http://stackoverflow.com/questions/34762029/getting-incompatible-types-error-when-using-code-generated-from-a-kotlin-data – Jacob Zimmerman Jan 25 '16 at 02:30
  • 4
    IntelliJ IDEA 2016.2: Menu / Tools / Kotlin / Show Kotlin Bytecode, then click Decompile button – Vadzim Jul 23 '16 at 11:36
26

you can go to Tools > Kotlin > Show kotlin bytecode

enter image description here

enter image description here

enter image description here

Rasoul Miri
  • 5,433
  • 43
  • 52
  • Currently it is disabled. But you can use the answer of @ARGeo (https://stackoverflow.com/a/54116131/2914140). – CoolMind Jun 27 '19 at 15:09
12

To convert a Kotlin source file to a Java source file you need to (when you in Android Studio):

  1. Press Cmd-Shift-A on a Mac, or press Ctrl-Shift-A on a Windows machine.

  2. Type the action you're looking for: Kotlin Bytecode and choose Show Kotlin Bytecode from menu.

enter image description here

  1. Press Decompile button on the top of Kotlin Bytecode panel.

enter image description here

  1. Now you get a Decompiled Java file along with Kotlin file in a adjacent tab:

enter image description here

Andy Fedoroff
  • 26,838
  • 8
  • 85
  • 144
9

I compile Kotlin to byte code and then de-compile that to Java. I compile with the Kotlin compiler and de-compile with cfr.

My project is here.

This allows me to compile this:

package functionsiiiandiiilambdas.functions.p01tailiiirecursive

tailrec fun findFixPoint(x: Double = 1.0): Double =
        if (x == Math.cos(x)) x else findFixPoint(Math.cos(x))

To this:

package functionsiiiandiiilambdas.functions.p01tailiiirecursive;

public final class ExampleKt {
  public static final double findFixPoint(double x) {
    while (x != Math.cos(x)) {
      x = Math.cos(x);
    }
    return x;
  }

  public static /* bridge */ /* synthetic */ double findFixPoint$default(
      double d, int n, Object object) {
    if ((n & 1) != 0) {
      d = 1.0;
    }
    return ExampleKt.findFixPoint(d);
  }
}
Tomas Bjerre
  • 2,594
  • 18
  • 20
5

As @louis-cad mentioned "Kotlin source -> Java's byte code -> Java source" is the only solution so far.

But I would like to mention the way, which I prefer: using Jadx decompiler for Android.

It allows to see the generates code for closures and, as for me, resulting code is "cleaner" then one from IntelliJ IDEA decompiler.

Normally when I need to see Java source code of any Kotlin class I do:

  • Generate apk: ./gradlew assembleDebug
  • Open apk using Jadx GUI: jadx-gui ./app/build/outputs/apk/debug/app-debug.apk

In this GUI basic IDE functionality works: class search, click to go declaration. etc.

Also all the source code could be saved and then viewed using other tools like IntelliJ IDEA.

Alex
  • 452
  • 7
  • 6
3
  1. open kotlin file in android studio
  2. go to tools -> kotlin ->kotlin bytecode
  3. in the new window that open beside your kotlin file , click the decompile button . it will create java equivalent of your kotlin file .
3

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.

Steps to convert your Kotlin source file to Java source file:

  1. Open your Kotlin project in the Android Studio.
  2. Then navigate to Tools -> Kotlin -> Show Kotlin Bytecode.

enter image description here

  1. You will get the bytecode of your Kotin file.
  2. Now click on the Decompile button to get your Java code from the bytecode
Faxriddin Abdullayev
  • 2,664
  • 1
  • 25
  • 27
-1

You can follow this steps to convert Kotlin to Java source

1.Open your Kotlin project in the IntelliJ IDEA / Android Studio.

2.Then navigate to Tools > Kotlin > Show Kotlin Bytecode.

3.You will get the bytecode of your Kotin file.

4.Now click on the Decompile button to get your Java code from the bytecode.

If you want to know more. I did a Google search for you. I found several really tutorials out there. below link looks pretty darn on point. https://hkrtrainings.com/kotlin-tutorial

  • 1
    This is a five year old question, and you gave literally the same answer five other people have already given. – stewSquared Apr 15 '21 at 14:58