102

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?

3
  • 1
    Do you want to convert Java code to Kotlin or Kotlin code to Java?
    – mfulton26
    Jan 4, 2016 at 12:47
  • 24
    CMD+SHFT+ALT+K is the easiest way to convert from Java to Kotlin in Android Studio Dec 21, 2017 at 5:11
  • Used the following tool to convert some complex java code to kotlin - javainuse.com/java2kot Jan 24, 2023 at 9:18

15 Answers 15

144

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
4
  • But is this possible to for all java files convert to kt files? Dec 21, 2017 at 7:21
  • It is possible as I know. And even when you can copy java code and paste it to the .kt file. Android Studio will ask you to "Would you like to convert java code to kotlin code" option.
    – okarakose
    Dec 22, 2017 at 6:41
  • 2
    I do not have this as an option what do I do?
    – Lime
    Mar 3, 2018 at 6:03
  • @WilliamA, probably this is because it wants to convert a whole file, not a piece of code. You can use try.kotlinlang.org ("Convert from Java" button), as wrote Md Mohsin.
    – CoolMind
    May 29, 2019 at 7:30
51

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.

6
  • 1
    This is true within Intellij IDEA, the auto-convert Java to Kotlin. Does it also work with the Eclipse plugin for Kotlin? Jan 4, 2016 at 14:36
  • 2
    @yole, any chance to see "Paste and convert Java to Kotlin" action in IDEA/AS? I was discouraged that prompt "Convert to Kotlin?" is not predictable when I'm trying to paste Java code – I thought some code analysis is used to detect Java code in clipboard :) The answers here enlightened me. But an issue still here – I have to use .java file as intermediate if I'm copying from the browser.
    – std.denis
    Feb 4, 2016 at 21:44
  • 6
    Actually, there is such a tool. try.kotlinlang.org has a convert from Java dialog where you can paste your code in and get Kotlin code out.
    – 2xsaiko
    Nov 8, 2017 at 18:41
  • 1
    Shift + Alt + Ctrl + k in Windows worked for me in Android studio so please check once Feb 11, 2021 at 6:37
  • @2xsaiko I did not find that option though. Where is it? Apr 18, 2022 at 10:49
29

I found a workaround:

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

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.

2
  • 1
    Can't find this functionality on the Try Kotlin site. Do you have a direct link?
    – pasio
    Jun 18, 2021 at 3:29
  • 1
    @pasio They have changed the interface of the website and remove conversion now. Need to check properly that they are providing convertor or not!
    – Md Mohsin
    Jun 19, 2021 at 4:47
24

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

  • Shift + Alt + Cmd + k in Mac
  • Shift + Alt + Ctrl + k in Windows
3
  • how to do this in an exact opposition manner. Kotlin -> Java ?
    – Kamlesh
    Jan 27, 2020 at 14:09
  • @KamleshKarwande, it is impossible. I was interested in it, JetBrains won't write such a tool.
    – CoolMind
    Jan 28, 2020 at 16:43
  • 1
    Maybe for a whole file, I don't know, but a fragment is not converted anymore. AS 3.5.3, Windows.
    – CoolMind
    Jan 28, 2020 at 16:45
11

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.

New Scratch window Languages dialog.

  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.

1
  • On Intellij Ultimate 2017.02 on OS X, when I do the paste, I get the dialog, and when I select "yes" to do the conversion, the resulting file is blank. When I choose "no" it pastes it as Java code. So it seems buggy/broken. Feb 27, 2018 at 20:48
11

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

enter image description here

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.

Create new Android project with Kotlin support

10

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

4

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

4

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...

3

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.

Click Try Online

Click Convert from Java

3

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.

3

You don't need any specific tools. Just simply copy and paste from anywhere and paste to .kt file in Android Studio. Dialog Window will be open by asking "Convert Java to Kotlin". Press Yes, It will convert it and do some miner changes.

2

In the new Android Studio version of 4.1 and above, you don't have to do any of the above stuff. You simply copy and paste any Java code into any existing kt file in Android Studio and it will recognize it as Java code needed to be converted to Kotlin. Then it will ask you in a dialog box whether it's something you want to do and convert it right in the spot if you answer yes. You can also check the "do not ask to convert" and it will automatically convert any Java code you paste in the future for you.

0

The simplest way to do this -

  1. Copy the code from the .java file.
  2. Create a new file with .kt extension.
  3. Paste the code.
  4. A pop up would appear asking to convert the code you pasted to Kotlin. Allow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.