How to convert Java source files into Kotlin in an existing project using command line?


#1

I am trying to convert some of the java files into kotlin using command line. Is there any library available to convert the java file into kotlin?


#2

Java to Kotlin conversion cannot be implemented correctly outside of Intellij Idea.

You can use JavaToKotlinTranslatorKt.translateToKotlin function from kotlin-plugin.jar, but you will need to have a mock implementation of Intellij Idea Project (see example) and this conversion will be different from the result of ‘Convert Java to Kotlin’ Action in Intellij Idea.


#3

Thanks. I will try.


#4

Maybe it would be helpful, if IntelliJ would allow to convert more than one file at a time.


#5

It is posiible - just select them is Project View and invoke Convert Java to Kotlin Action


#6

Go to Android studio main_Activity.
Enter CTRL + ALT + SHIFT + K.
The Java code will be converted to Kotlin.