如何将部分 Java 源文件转换为 Kotlin?

在我的 Kotlin 项目中,我有一部分 Java 代码想要转换成 Kotlin。将 Java 文件转换为 Kotlin 的菜单项被禁用,因为它不是我想要转换的整个文件。

如何将 Java 程式码转换为 Kotlin?

154097 次浏览

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.

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.

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

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.

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

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

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.

Simple Ctrl+Alt+Shift+ K code will convert into 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.

Click Try Online

Click Convert from Java

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.

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

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

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.

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.

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.