问题
I have a Kotlin source file, but I want to translate it to Java.
How can I convert Kotlin to Java source?
回答1:
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
.
回答2:
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)
回答3:
you can go to Tools > Kotlin > Show kotlin bytecode
回答4:
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);
}
}
回答5:
To convert a
Kotlin
source file to aJava
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 chooseShow Kotlin Bytecode
from menu.
- Press
Decompile
button on the top ofKotlin Bytecode
panel.
- Now you get a Decompiled Java file along with Kotlin file in a adjacent tab:
Hope this helps.
回答6:
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.
回答7:
- 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 .
回答8:
I can't believe nobody has mentioned Local History yet: by default, IDEA will keep a history of changes to a file.
To get your files back exactly the way they were before you converted them to Kotlin:
For each file you want to revert back:
- Open Find Action (Command-Shift-A on Mac)
- Search for "Local History"
- Press Enter twice (once to accept "Local History" in the search results, and then once more to select "Show History" in the Local History menu)
- Find "Convert files from Java to Kotlin" in the list on the left hand side
- Right click on it, and select "Revert"
This might not work if you did it months ago (because Local History is not a VCS), and it does not, for instance, re-create any methods you added using Kotlin, but it worked for me.
来源:https://stackoverflow.com/questions/34957430/how-to-convert-a-kotlin-source-file-to-a-java-source-file