ChatGPT解决这个技术问题 Extra ChatGPT

How to convert a Kotlin source file to a Java source file

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

How can I convert Kotlin to Java source?

I'm pretty sure no automated tool has been built for this yet. You can build it first!
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?
Consider the j2objC translator. Would be great if you could do kotlin -> Java -> objC
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 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?

L
Louis CAD

As @Vadzim said, 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 Decompile button 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.


Is the Java output not ugly?
@Pacerier Just like most decompiling outputs, it is, of course
converted from kotlin to java. But its showing extra code. the code that i even never wrote or heard
that option is not enabled, it is coloured grey. How do i enable it.
@VikasPandey, Decompile Kotlin to Java is currently enabled only for compiled Kotlin classes.
G
Gastón Saillén

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)


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.
@voddan I think he's referring to reified generics, which aren't a possibility in pure Java.
@Jire reified generics are always inlined, so from Java they look like normal class references
This is what I'm talking about stackoverflow.com/questions/34762029/…
IntelliJ IDEA 2016.2: Menu / Tools / Kotlin / Show Kotlin Bytecode, then click Decompile button
R
Rasoul Miri

you can go to Tools > Kotlin > Show kotlin bytecode

https://i.stack.imgur.com/TE1W8.png

https://i.stack.imgur.com/Y4eiv.png

https://i.stack.imgur.com/Ebj05.png


Currently it is disabled. But you can use the answer of @ARGeo (stackoverflow.com/a/54116131/2914140).
A
Andy Jazz

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

Press Cmd-Shift-A on a Mac, or press Ctrl-Shift-A on a Windows machine. Type the action you're looking for: Kotlin Bytecode and choose Show Kotlin Bytecode from menu.

https://i.stack.imgur.com/A0W49.png

Press Decompile button on the top of Kotlin Bytecode panel.

https://i.stack.imgur.com/XozCm.png

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

https://i.stack.imgur.com/Czo9Z.png


Thank you, sir! This buggy AS again changed normal tools (Tools > Kotlin > ...).
T
Tomas Bjerre

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);
  }
}

A
Alex

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.


Good one, it's available through Homebrew: brew install jadx
佚名

open kotlin file in android studio go to tools -> kotlin ->kotlin bytecode in the new window that open beside your kotlin file , click the decompile button . it will create java equivalent of your kotlin file .


F
Fakhriddin Abdullaev

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:

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

https://i.stack.imgur.com/NStKu.png

You will get the bytecode of your Kotin file. Now click on the Decompile button to get your Java code from the bytecode