Converting file (tracked with Git) from Java to Kotlin in Android Studio

Viewed 4109

A conversion from Java to Kotlin in Android Studio 2.3.2 (in 3.0 the same behaviour) creates a new file and deletes previous. So Git doesn't know anything about this conversion. And the git history doesn't save. In Intellij Idea everything's fine. IDE just renames file and git saves the history. How can do the same in Android Studio.

5 Answers

In case this might help a future reader:

If you use the Git commit dialog integrated with IntelliJ (Commit via Ctrl+K), there is a checkbox on the right in recent versions: ☑ Extra commit for .java > .kt renames

Submitting the dialog this way will create two commits, the first one being just .java files renamed into .kt files with no content changes. This helps Git track the content.

Android Studio now has an option: Extra commit for java > kt renames which is enabled by default and fixes git blame. But this creates an invalid commit in the history.

If this change is rebased, then you keep git blame, but lose git bisect, and the opposite is true if you squash merge.

I've created a small (MIT) gradle script and instructions to get the best of both worlds (TL;DR: perform a rename, and in the same commit, flag to the compiler that the renamed .kt file should be run via javac):

https://github.com/ankidroid/Anki-Android/blob/bffd3c60c00649cdc75f35293a85f7e1ef064b53/docs/kotlin-migration.md https://github.com/ankidroid/Anki-Android/blob/1ee5f65b87a8fe544193d928d64dd8395513c9f0/AnkiDroid/kotlinMigration.gradle

The above could further be productionized in an IntelliJ plugin.

Related