Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 362 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert a Kotlin source file to a Java source file

#1
I have a Kotlin source file, but I want to translate it to Java.

How can I convert Kotlin to Java source?
Reply

#2
As @louis-cad mentioned "Kotlin source -> Java's byte code -> Java source" is the only solution so far.

But I would like to mention the way, which I prefer: using [Jadx decompiler for Android][1].

It allows to see the generates code for _closures_ and, as for me, resulting code is "cleaner" then one from IntelliJ IDEA decompiler.

Normally when I need to see Java source code of any Kotlin class I do:

- Generate apk: `./gradlew assembleDebug`
- Open apk using Jadx GUI: `jadx-gui ./app/build/outputs/apk/debug/app-debug.apk`

In this GUI basic IDE functionality works: class search, click to go declaration. etc.

Also all the source code could be saved and then viewed using other tools like IntelliJ IDEA.

[1]:

[To see links please register here]

Reply

#3
1. open kotlin file in android studio
2. go to tools -> kotlin ->kotlin bytecode
3. in the new window that open beside your kotlin file , click the decompile button . it will create java equivalent of your kotlin file .
Reply

#4
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`.
Reply

#5
you can go to **Tools > Kotlin > Show kotlin bytecode**


[![enter image description here][1]][1]


[![enter image description here][2]][2]


[![enter image description here][3]][3]


[1]:

[2]:

[3]:
Reply

#6
You can compile Kotlin to bytecode, then use a Java disassembler.

The decompiling may be done inside IntelliJ Idea, or using FernFlower

[To see links please register here]

(thanks @Jire)

There was no automated tool as I checked a couple months ago (and no plans for one AFAIK)
Reply

#7
Java and Kotlin runs on Java Virtual Machine (JVM).

Converting a Kotlin file to Java file involves two steps i.e. compiling the Kotlin code to the JVM bytecode and then decompile the bytecode to the Java code.

Steps to convert your Kotlin source file to Java source file:

1. Open your Kotlin project in the Android Studio.
2. Then navigate to Tools -> Kotlin -> Show Kotlin Bytecode.

[![enter image description here][1]][1]

3. You will get the bytecode of your Kotin file.
4. Now click on the Decompile button to get your Java code from the bytecode


[1]:
Reply

#8
**In Android Studio, to convert a `Kotlin` source file to a `Java` source file you need to**:

1. Press <kbd>Cmd</kbd>-<kbd>Shift</kbd>-<kbd>A</kbd> on a Mac,
or press <kbd>Ctrl</kbd>-<kbd>Shift</kbd>-<kbd>A</kbd> on a Windows machine.

2. Type the action you're looking for: `Kotlin Bytecode` and choose `Show Kotlin Bytecode` from menu.

[![enter image description here][2]][2]

3. Press `Decompile` button on the top of `Kotlin Bytecode` panel.

[![enter image description here][3]][3]

4. Now you get a Decompiled Java file along with Kotlin file in a adjacent tab:

[![enter image description here][4]][4]


[1]:

[2]:

[3]:

[4]:
Reply

#9
I compile Kotlin to byte code and then de-compile that to Java. I compile with the Kotlin compiler and de-compile with [cfr][1].

My project is [here][2].

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);
}
}


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through