Как в android studio поменять kotlin на java
Перейти к содержимому

Как в android studio поменять kotlin на java

  • автор:

How to Convert Java to Kotlin and Kotlin to Java

You can convert Kotlin to Java and Java to Kotlin using offline tools. Some of them are explained in this article. Do note that depending on the code being converted and the type of tool being used for the conversion purpose, the converted code may not be 100% accurate and you may have to make some manual edits. You should always review converted code before using it in an application.

Converting Kotlin to Java Using IntelliJ IDE Software and Android Studio

You can use Android Studio app development software suite to convert Kotlin code to Java. Android Studio can be downloaded from here. After downloading, launch Android Studio and create a new Kotlin application project according to your requirements. You can also import your existing Kotlin projects to Android Studio.

With a “.kt” file open in Android Studio editor, click on Tools > Kotlin > Show Kotlin Bytecode menu entry (as shown in the screenshot below).

How to Convert Java to Kotlin and Kotlin to Java

You will get Kotlin bytecode in a separate sub-window once the process finishes.

Convert Java to Kotlin and Kotlin to Java

Click on the “Decompile” button in the same window to convert the code to Java.

You will get a new file with the same name but with a “.java” extension instead of the original “.kt” extension. This file will contain the converted Java code and will have “decompiled” keyword in its file name.

You can also use IntelliJ IDE, an open source and commercial software suite for developing applications in Java and Kotlin, to convert Kotlin code to Java. The steps to convert Kotlin to Java are the same as above, as Android Studio itself is based on IntelliJ IDE software. You can download IntelliJ IDE software from its official website.

Converting Java to Kotlin Using IntelliJ IDE Software and Android Studio

You can use both Android Studio and IntelliJ IDE software to convert Java code to Kotlin. If you are using a Kotlin based project in Android Studio or IntelliJ IDE software and try to copy/paste Java code, you should get a prompt to automatically convert Java code to Kotlin (as shown in the screenshot below).

Another method is to create a “.java” file inside your project directory or add an existing “.java” file to your project and then convert code contained in the entire file into Kotlin. To do so, open a “.java” file located in your project folder in Android Studio editor and click on Code > Convert Java File to Kotlin File menu entry (as shown in the screenshot below).

Once the conversion process has finished, you will get a new “.kt” file with the same name and the original “.java” file will be removed from the project folder.

As stated earlier, these steps should work with IntelliJ IDE software as well.

Converting Kotlin to Java Using Fernflower Command Line Utility

Both Android Studio and IntelliJ IDE software use a Java library and command line utility underneath to convert Kotlin code to Java. This utility is called “Fernflower” and it can be compiled from its source code available here using the Gradle build tool. You will get a “fernflower.jar” file once the build process is completed successfully. This command line tool is also available in downloadable archives of Android Studio and IntelliJ IDE software under the “plugins/java-decompiler/lib/” folder and it is named as “java-decompiler.jar”.

To convert a “.kt” file using “fernflower.jar” or “java-decompiler.jar” files, you will first need to compile it to a “.class” file using Kotlin compiler. The official Kotlin command line compiler can be installed in Linux by following instructions available here. Once you have Kotlin command line compiler or “kotlinc” installed in your Linux system, run a command in the following format to convert a “test.kt” file to “TestKt.class” file.

Depending on the name of the “.kt” file, kotlinc will automatically assign a suitable name to the “.class” file. You can now convert “.class” file to “.java” file using one of the following two commands:

$ java — jar fernflower. jar TestKt. class .

$ java — cp java — decompiler. jar org. jetbrains . java . decompiler . main . decompiler . ConsoleDecompiler TestKt. class .

The decompiler may take some time to convert the code depending on the complexity and scale of your program. Once the decompilation process is finished, you should get a “.java” file (TestKt.java file in this case) containing converted code in Java.

Conclusion

Since Kotlin was made the preferred language for developing Android apps, its adoption has increased considerably. App developers may want to convert Java code to Kotlin to keep their application up-to-date. The tools built into Android Studio and IntelliJ IDE software allows them to convert code both ways and makes it easy to convert code without much manual input.

About the author

Nitesh Kumar

I am a freelancer software developer and content writer who loves Linux, open source software and the free software community.

Convert a Kotlin Source File to a Java File

Convert a Kotlin Source File to a Java File

Google declared Kotlin as the preferred language for Android app development. But that does not mean we would not require to use Java.

Kotlin’s success is because of interoperability with Java as both use Java Virtual Machine (JVM).

Hence, converting a Kotlin source code file to a Java one is essential.

Why Convert Kotlin to Java

  • Integrate some Java features.
  • Compile and run in Java to detect bugs or other issues.
  • To use Java-specific features like operator overloading and static methods.
  • For better code readability.

Convert Kotlin Source File to Java Source File

Since Kotlin and Java use JVM, converting one language’s file to another is possible. There are two ways to convert Kotlin code to Java code; Android Studio/IntelliJ IDEA or FernFlower .

Convert Kotlin Code to Java Using Android Studio/IntelliJ IDEA

  • Compile Kotlin file to JVM bytecode.
  • Decompile JVM bytecode to Java file.
  • Open the Kotlin file that should be converted to Java.
  • Navigate to Tools > Kotlin > Show Kotlin Bytecode.
  • Open the Kotlin file’s bytecode.
  • Checkmark JVM 8 Target and click on the Decompile button to get the Java code.

The process to convert Kotlin to Java in IntelliJ IDEA is the same.

Convert Kotlin Code to Java Code Using FernFlower

The IntelliJ decompile option uses the FernFlower to generate the back-end code.

  • Compile the Kotlin source file using the kotlinc file_name.kt command to create the class file.
  • Use FernFlower to get the fernflower.jar file (To get the jar file, run the Gradle build after downloading the FernFlower project).
  • Use the command java -jar fernflower.jar file_name.class to get the converted Java source file.

Although we can use both methods to convert a Kotlin file to a Java file, it is recommended to use IntelliJ IDEA or Android Studio .

The reason is that directly using the FernFlower will lead to a complicated converted code, which will be hard to read and understand.

Kailash Vaviya is a freelance writer who started writing in 2019 and has never stopped since then as he fell in love with it. He has a soft corner for technology and likes to read, learn, and write about it. His content is focused on providing information to help build a brand presence and gain engagement.

Как в android studio поменять kotlin на java

Java programming language is the oldest and most preferred language for Android app development. However, during Google I/O 2017, Kotlin has been declared as an official language for Android development by the Google Android Team. Kotlin has gained popularity among developers very quickly because of its similarities as well as interoperable with the Java language. One can mix code of Java and Kotlin while designing an Android project. The syntax of Java and Kotlin differs in many aspects but their compilation process is almost the same. Code of both the languages gets compiled into bytecode that is executable on Java Virtual Machine(JVM). Thus if one can derive the bytecode of compiled Kotlin file, it can be decompiled in order to produce the equivalent Java code. 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.
  • To remove the Kotlin code from the project files.

Convert Kotlin Code to Java Code in Android Studio

Code Conversion

Step 1: Open Kotlin Class/File

Open the Kotlin Class/File which is to be converted into Java. Consider the code of the MainActivity file mentioned below for the conversion.

How to Convert Kotlin Code to Java Code?

Kotlin is a language that is well known for the interoperability with the java language. Kotlin is good for Mobile-cross platform, Native, Data-Science, Server-side, Web-development, Android. Interoperability is possible in both the languages because of the JVM (Java Virtual Machine) or in other words we can say that they are compiled to the same bytecode.

How to Convert Kotlin Code to Java Code

If we want to convert java to kotlin we can easily do it with the help of Android Studio. But if we want to do the vice versa how we can do that. This we will learn it in the following tutorial. So let’s start.

  • So that we can remove kotlin from our project.
  • Integrate some functionalities that can easily be implemented in JAVA.
  • So that we can investigate or see the performance issues.
  • The first step consists of converting the code in Kotlin to the JVM Bytecode.
  • The second step is to decompile the produced code to the Java code so that we can see the result.

How to Convert Kotlin Code to Java Code?

First Method:

Suppose we have a file named as Demo.kt which has the following code snippet:

When we compile the code with the command that will be:

Kotlininc Demo.kt

Now this file has been created. We need to convert this file to Java.

So for that we will be using the FernFlower. We can easily get the fernflower.jar by downloading the project and running the gradle once. Once when the jar file is with us, now we have to run a command that is:

Java -jar fernflower .jar Demokt.class

This will convert the kotlin code to the Java code. The code would look like:

This is the one way of converting the Kotlin code to the Java.

Second Method:

Now we will be learning the alternate way to convert the kotlin code to java code. This we will be doing with the IntelliJ IDEA.

  • First Code
  • Click Tools
  • Click Kotlin
  • Click Show Kotlin Bytecode from the context menu

Convert Kotlin Code to Java Code 1

After that we can see the readable form.

Note: The IntelliJ IDEA also uses the fernflower in the background. So the code will be similar to the above code.

These are two ways by which we can convert the Kotlin code to Java. Comment down below if you have any queries.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *