赞
踩
本文翻译自: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. 我有一个Kotlin源文件,但我想将其转换为Java。
How can I convert Kotlin to Java source? 如何将Kotlin转换为Java源代码?
参考:https://stackoom.com/question/2Mg1W/如何将Kotlin源文件转换为Java源文件
You can compile Kotlin to bytecode, then use a Java disassembler. 您可以将Kotlin编译为字节码,然后使用Java反汇编程序。
The decompiling may be done inside IntelliJ Idea, or using FernFlower https://github.com/fesh0r/fernflower (thanks @Jire) 可以在IntelliJ Idea内完成反编译,也可以使用FernFlower https://github.com/fesh0r/fernflower (感谢@Jire)完成反编译
There was no automated tool as I checked a couple months ago (and no plans for one AFAIK) 几个月前我没有检查过自动化工具(也没有计划使用AFAIK)
As @Vadzim said, in IntelliJ or Android Studio, you just have to do the following to get java code from kotlin: 正如@Vadzim所说,在IntelliJ或Android Studio中,您只需执行以下操作即可从kotlin获取Java代码:
Menu > Tools > Kotlin > Show Kotlin Bytecode
Decompile
button 单击Decompile
按钮 Update: 更新:
With a recent version (1.2+) of the Kotlin plugin you also can directly do Menu > Tools > Kotlin -> Decompile Kotlin to Java
. 使用Kotlin插件的最新版本(1.2+),您还可以直接执行Menu > Tools > Kotlin -> Decompile Kotlin to Java
。
As @louis-cad mentioned "Kotlin source -> Java's byte code -> Java source" is the only solution so far. 就像@ louis-cad提到的那样,“ Kotlin源-> Java的字节码-> Java源”是迄今为止唯一的解决方案。
But I would like to mention the way, which I prefer: using Jadx decompiler for Android . 但我想提一提我喜欢的方式:使用Android的Jadx反编译器 。
It allows to see the generates code for closures and, as for me, resulting code is "cleaner" then one from IntelliJ IDEA decompiler. 它允许查看生成的闭包代码,对我而言,生成的代码“更干净”,然后是IntelliJ IDEA反编译器中的代码。
Normally when I need to see Java source code of any Kotlin class I do: 通常,当我需要查看任何Kotlin类的Java源代码时,我会这样做:
./gradlew assembleDebug
生成apk: ./gradlew assembleDebug
jadx-gui ./app/build/outputs/apk/debug/app-debug.apk
使用Jadx GUI打开apk: jadx-gui ./app/build/outputs/apk/debug/app-debug.apk
In this GUI basic IDE functionality works: class search, click to go declaration. 在此GUI中,基本的IDE功能起作用:类搜索,单击以进行声明。 etc. 等等
Also all the source code could be saved and then viewed using other tools like IntelliJ IDEA. 同样,所有源代码都可以保存,然后使用其他工具(如IntelliJ IDEA)进行查看。
I compile Kotlin to byte code and then de-compile that to Java. 我将Kotlin编译为字节码,然后将其反编译为Java。 I compile with the Kotlin compiler and de-compile with cfr . 我使用Kotlin编译器进行编译,并使用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);
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。