Converting Kotlin to Java
Asked Answered
L

4

21

Can anyone help me about converting Kotlin code in Android Studio to Java?

I went through the below process:

Tools >> Kotlin>> Decompile Kotlin to Java

But the "Kotlin to Java" option is disabled. What should I do?

enter image description here

Lierne answered 17/6, 2018 at 10:12 Comment(1)
R
29

Currently Tools >> Kotlin>> Decompile Kotlin to Java is enabled only for compiled Kotlin classes (.class, not .kt extension) located at app/build/tmp/kotlin-classes/ after a successful build.

A note to the @Uriel Frankel's workaround:

If Tools >> Kotlin >> Show Kotlin Bytecode >> Decompile button is disabled (or not shown) the Java Bytecode Decompiler plugin should be installed: Preferences >> Plugins >> search forJava Bytecode Decompiler` >> check the plugin & press OK.

Rehearing answered 23/9, 2018 at 1:5 Comment(0)
S
8

Tools >> kotlin >> Show Kotlin bytecode >> Decompile

Supererogatory answered 17/6, 2018 at 11:55 Comment(1)
At least for me, there is no "Decompile" option in that view.Sgraffito
A
2

Seems like a bug.

Tools -> Kotlin -> Decompile Kotlin to Java

Was actually disabled for me too even after a successful build (In my side Android Studio v3.2) so, I went through this:

Click on the Kotlin (Compiled class):

Selected Tools -> Kotlin -> Show Kotlin Bytecode

Then kotlin bytecode window appeared:

enter image description here

Made JVM 8 Target as selected and then decompile gave me the java class.

Note that this happened when Java Bytecode Decompiler plugin was enabled on the Android Studio.

Update:

This however might have some issues even after converting to java and perhaps, we should wait for the newest releases.

Artful answered 25/9, 2018 at 11:19 Comment(0)
R
0

I followed Onik's answer above.

The Java Bytecode Decompiler was automatically bundled. I opened the .class file in Android Studio, and selected Tools >> Kotlin>> Decompile Kotlin to Java.

After waiting several minutes, Android Studio told me

The file is too large: 50.78 MB. Showing a read-only preview of the first 2.56 MB

and it opened a file MyKotlinClass.decompiled.java, most of which is unintelligible to me.

MyKotlinClass is approximately 300 lines long. At this point, manually re-writing the file as Java code is looking like the better option.

Rubalcava answered 9/7, 2019 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.