如何将 Kotlin 源文件转换为 Java 源文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34957430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:10:47  来源:igfitidea点击:

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

javakotlincode-conversion

提问by activedecay

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 源代码?

采纳答案by Louis CAD

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 代码:

  1. Menu > Tools > Kotlin > Show Kotlin Bytecode
  2. Click on the Decompilebutton
  3. Copy the java code
  1. Menu > Tools > Kotlin > Show Kotlin Bytecode
  2. 点击Decompile按钮
  3. 复制java代码

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.

回答by voddan

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 的计划)

回答by Alex

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.

但我想提一下我更喜欢的方式:使用Jadx decompiler for Android

It allows to see the generates code for closuresand, 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 源代码时,我会这样做:

  • Generate apk: ./gradlew assembleDebug
  • Open apk using Jadx GUI: jadx-gui ./app/build/outputs/apk/debug/app-debug.apk
  • 生成apk: ./gradlew assembleDebug
  • 使用 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. etc.

在此 GUI 中,基本 IDE 功能起作用:类搜索、单击以进行声明。等等。

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

还可以保存所有源代码,然后使用 IntelliJ IDEA 等其他工具查看。

回答by Alex

  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 .
  1. 在android studio中打开kotlin文件
  2. 转到工具 -> kotlin -> kotlin 字节码
  3. 在 kotlin 文件旁边打开的新窗口中,单击反编译按钮。它将创建相当于您的 kotlin 文件的 java。

回答by 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.

我将 Kotlin 编译为字节码,然后将其反编译为 Java。我用 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);
  }
}

回答by Rasoul Miri

you can go to Tools > Kotlin > Show kotlin bytecode

您可以转到工具 > Kotlin > 显示 kotlin 字节码

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by Andy

To convert a Kotlinsource file to a Javasource file you need to (when you in Android Studio):

要将Kotlin源文件转换为Java您需要的源文件(当您在 Android Studio 中时):

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

  2. Type the action you're looking for: Kotlin Bytecodeand choose Show Kotlin Bytecodefrom menu.

  1. 新闻Cmd- Shift-A在Mac上,或按Ctrl- Shift-A在Windows机器上。

  2. 输入您要查找的操作:Kotlin BytecodeShow Kotlin Bytecode从菜单中选择。

enter image description here

在此处输入图片说明

  1. Press Decompilebutton on the top of Kotlin Bytecodepanel.
  1. 按下面板Decompile顶部的按钮Kotlin Bytecode

enter image description here

在此处输入图片说明

  1. Now you get a Decompiled Java file along with Kotlin file in a adjacent tab:
  1. 现在,您将在相邻选项卡中获得一个反编译的 Java 文件和 Kotlin 文件:

enter image description here

在此处输入图片说明

回答by Faxriddin Abdullayev

Java and Kotlin runs on Java Virtual Machine (JVM).

Java 和 Kotlin 在 Java 虚拟机 (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.

将 Kotlin 文件转换为 Java 文件涉及两个步骤,即将 Kotlin 代码编译为 JVM 字节码,然后将字节码反编译为 Java 代码。

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

将 Kotlin 源文件转换为 Java 源文件的步骤:

  1. Open your Kotlin project in the Android Studio.
  2. Then navigate to Tools -> Kotlin -> Show Kotlin Bytecode.
  1. 在 Android Studio 中打开您的 Kotlin 项目。
  2. 然后导航到工具 -> Kotlin -> 显示 Kotlin 字节码。

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
  1. 您将获得 Kotin 文件的字节码。
  2. 现在单击 Decompile 按钮从字节码中获取您的 Java 代码