且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Android Studio中将文件(使用Git进行跟踪)从Java转换为Kotlin

更新时间:2023-10-06 11:23:22

Git会从添加/删除的文件对中重新命名,但只有当这些文件足够接近,即如果该文件被重命名为没有或少量更改。



当您应用java-to-kotlin转换时,通常文件的每一行都会更改,所以git无法找到这些旧文件和新文件之间的相互关系。



您可以使用以下两阶段方法:




  • 只需将 .java 文件的扩展名更改为 .kt 并将其提交;

  • 将其重命名,应用转换并提交已修改的 .kt 文件。 $ b

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.

Git guesses renames from added/removed file pairs, but only if these files are close enough, i.e. if the file was renamed with no or small amount of changes.

When you apply java-to-kotlin conversion usually every line of a file changes, so that git cannot find that these old and new files somehow relate to each other.

You can use the following two-stage approach instead:

  • just change the extension of .java file to .kt and commit it;
  • rename it back, apply the conversion and commit the modified .kt file.