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

本文翻译自: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源代码?


#1楼

参考:https://stackoom.com/question/2Mg1W/如何将Kotlin源文件转换为Java源文件


#2楼

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)


#3楼

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 Decompile button 单击Decompile按钮
  3. Copy the java code 复制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


#4楼

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源代码时,我会这样做:

  • Generate apk: ./gradlew assembleDebug 生成apk: ./gradlew assembleDebug
  • Open apk using Jadx GUI: 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)进行查看。


#5楼

  1. open kotlin file in android studio 在android studio中打开kotlin文件
  2. go to tools -> kotlin ->kotlin bytecode 转到工具-> kotlin-> kotlin字节码
  3. in the new window that open beside your kotlin file , click the decompile button . 在您的kotlin文件旁边打开的新窗口中,单击反编译按钮。 it will create java equivalent of your kotlin file . 它将创建与您的kotlin文件等效的java。

#6楼

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);
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值