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

In my Kotlin project I have some parts of Java code which I want to convert to Kotlin. The menu item Convert 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?

Answers


There is no tool to convert Kotlin code to Java. If you want to convert part of a file from Java to Kotlin, the easiest way is to copy the code from the Java file in the IDE and paste it into the Kotlin file.


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

To convert Java code in Android studio use CTRL+SHIFT+ALT+K

To convert from Website use Try Kotlin site.

To convert from Android app use Master in Android.


I found a workaround:

  1. Create a Java file and paste inside this file Java code, which you want covert.
  2. Now copy Java code from Java file.
  3. Go to Kotlin file and paste this code. Idea will suggest convert code from Java to Kotlin.

In Android Studio 3.0

Kotlin is fully supported from Android Studio 3.0 (i.e, NO Kotlin Plugin required). If you have Android Studio >= 3.0 then your following jobs are very easy,

  • You can create new Kotlin file.
  • You can add Kotlin code to existing Project.
  • You can convert entire JAVA code into Kotlin

Convert existing Java code to Kotlin code

In Android Studio 3.0, open a Java file and select Code > Convert Java File to Kotlin File.

Or, create a new Kotlin file (File > New > Kotlin File/Class), and then paste your Java code into that file—when prompted, click Yes to convert the code to Kotlin. You can check Don't show this dialog next time, which makes it easy to dump Java code snippets into your Kotlin files.

Dialog Window

Official Source :: Convert to Kotlin From JAVA

Note::When you create a new project

Tick checkbox Include Kotlin support to get kotlin support for the entire project.

The below is the screen shot of first screen of new Android Project Creation Window with an option to include Kotlin Support to the entire project.


When pasting Java code into Kotlin file, using IntelliJ derived IDE, the IDE will not convert the Java into Kotlin.

The problem seems to stem from IntelliJ not knowing the code is Java. Here is a simple way to ensure that code is converted into Kotlin.

  1. Copy the Java code from your web browser.
  2. Open Scratch File (File -> New -> Scratch File or Ctrl+Alt+Shift+Insert)
  3. In the Languages dialog select Java as the language.

  1. Paste the previously copied Java snippet.
  2. Select All (Ctrl-A) and Copy (Ctrl-C)
  3. Now Paste into your Kotlin code.

By first pasting the Java code into a Java scratch file, you have let IntelliJ know that the code snippet is Java code. It will then automatically convert the code to Kotlin when pasting into a Kotlin file.


Simple Ctrl+Alt+Shift+ K code will convert into kotlin


click here for image

In Android 3.0 Canary, it is very simple: Go to Code; the last option is 'Convert Java file into Kotlin.' And you are done!

There is also a shortcut: Ctrl+Alt+Shift+K


its a simple process.. go into your Android studio's tool bar and Do follow this Step

CODE -> Convert java file to kotlin file..

then it asks you for Confirmation and Restart the Android Studio IDE..when you confirm then it restart your IDE and it show you converted Kotlin code file...


If your Android Studio version is less than 3 then you have to install plugin of Kotlin. To install it follow steps from below link

Blog jetbrains :- Working with Kotlin in Android Studio

After installing plugin restart Android Studio and Find Action (Ctrl+Shift+A) and type Convert Java File to Kotlin.


From kotlinlang.org, click on "Try Online" at the top of the page. You'll see an option to "Convert from Java" in the toolbar.


To convert a complete file into Kotlin, use this combo of keys:

  • Shift + Alt + Cmd + k in Mac
  • Shift + Alt + Ctrl + k in Windows

Need Your Help

The "right" JSON date format

javascript json

I've seen so many different standards for the JSON date format:

How to add a new line of text to an existing file in Java?

java file-io text-processing

I would like to append a new line to an existing file without erasing the current information of that file. In short, here is the methodology that I am using the current time: