Decompiling Kotlin to Java
Sometimes, to debug Java/Kotlin interoperability issues, it helps to see the equivalent Java code for a particular Kotlin source file.
IntelliJ IDEA and Android Studio both offer this capability. From the Tools > Kotlin menu, choose “Show Kotlin Bytecode”. This will open up the “Kotlin Bytecode” view and will show you the JVM bytecode associated with the current Kotlin source file. In that view, the “Decompile” button will attempt to decompile that bytecode into Java source code, opening up the result in another editor tab.
This works but is not without its flaws:
- Decompiled bytecode can be more difficult to read, as local variables tend to use generated names (e.g.,
var2
) rather than human-readable names - The decompilation process can be very slow, depending on both the direct size of the Kotlin source file and what that source uses that needs to be converted
- On Android Studio in particular, it may take a bit of work to get the decompiler to run
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.