Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 638
activedecay Avatar asked Jan 22 '16 23:01

activedecay


People also ask

Can we convert Kotlin code to Java in Android Studio?

Android Studio does exactly the same to carry out the code conversion from Kotlin to Java. Developers may have many reasons to convert the Kotlin code into Java such as: To integrate features that are easy to implement in Java language. To resolve some performance issue that is difficult to locate in Kotlin.

Can I import Kotlin library in Java?

No, it just needs kotlin-stdlib as a dependency to use it from Java.

How can I convert a part of Java source file to Kotlin?

In Android Studio 3.0, open a Java file and select Code > Convert Java File to Kotlin File. Or, create a new Kotlin file (File > New > Kotlin File/Class), and then paste your Java code into that file—when prompted, click Yes to convert the code to Kotlin.


2 Answers

As @Vadzim said, in IntelliJ or Android Studio, you just have to do the following to get java code from kotlin:

  1. Menu > Tools > Kotlin > Show Kotlin Bytecode
  2. Click on the Decompile button
  3. 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.

like image 139
Louis CAD Avatar answered Sep 26 '22 01:09

Louis CAD


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)

like image 28
voddan Avatar answered Sep 27 '22 01:09

voddan