Programming
java kotlin code-conversion
Updated Mon, 04 Jul 2022 13:41:22 GMT

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?




Solution

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.





Comments (5)

  • +0 – Is the Java output not ugly? — Jun 17, 2017 at 07:31  
  • +0 – @Pacerier Just like most decompiling outputs, it is, of course — Jun 17, 2017 at 12:27  
  • +6 – converted from kotlin to java. But its showing extra code. the code that i even never wrote or heard — Jul 20, 2017 at 10:42  
  • +0 – that option is not enabled, it is coloured grey. How do i enable it. — Jul 12, 2018 at 06:07  
  • +0 – @VikasPandey, Decompile Kotlin to Java is currently enabled only for compiled Kotlin classes. — Sep 28, 2018 at 13:55