android – How can I convert a part of Java source file to Kotlin?-ThrowExceptions

Exception or error:

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?

How to solve:

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


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

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


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.


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


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.

Leave a Reply

Your email address will not be published. Required fields are marked *