Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a part of Java source file to Kotlin?

In my Kotlin project, I have some parts of Java code that I want to convert to Kotlin. The menu item that converts the Java file to Kotlin is disabled because it's not a whole file I want to convert.

How can I convert Java code to Kotlin?

like image 309
Alan Coromano Avatar asked Jan 04 '16 09:01

Alan Coromano


People also ask

Can Java Code be converted to Kotlin?

Convert existing Java code to Kotlin codeTo convert Java code to Kotlin, open the Java file in Android Studio, and select Code > Convert Java File to Kotlin File. Alternatively, create a new Kotlin file (File > New > Kotlin File/Class), and then paste your Java code into that file.

Can you mix Java and Kotlin files?

If your question is can you use kotlin files in java files and vice versa then the answer is yes.

How do you convert a file in Java?

The javac command is used to convert the Java source file into the class file. The java command is used to run a Java program stored in a . class file.


1 Answers

With Android Studio 3.0

You don't need to install any plugin to convert java code to kotlin code. Now, Google is officially supporting Kotlin language.

  • Android Studio Menu -> Code -> Convert Java File to Kotlin File

Before Android Studio 3.0

If you are using older Android Studio ( which is below version 3.0 ) there is a Kotlin tool under the menu.

Installing Kotlin plugin :

  • Android Studio Menu -> File -> Settings -> Plugins -> Browse repositories -> Kotlin

Converting Java Code to Kotlin Code :

  • Select your java class file from project structure.
  • Android Studio Menu -> Code -> Convert Java File to Kotlin File
like image 170
okarakose Avatar answered Oct 14 '22 22:10

okarakose