Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio - CRLF vs LF for a Git-based multiplatform project

I'm working on an Android project involving multiple developers, some of which are on Windows, others Linux/MacOS. Since I'm on Windows, I've been instructed to configure Git as follows to avoid issues:

autocrlf = true
safecrlf = true

This works mostly fine. Any .java/XML/etc files I create in Android Studio are in CRLF, get converted to LF when I push them into the repo, then back into CRLF when I pull changes into my local copy. Problem is, some types of files, like vector drawable assets, get generated in LF instead, for some reason. So when I try to add them to Git, I get the "irreversible conversion" error:

enter image description here I know I could set safecrlf = warn, but from what I understand, this carries a risk of corrupting binary files if Git mistakes them for text files, so I'm looking for a safer solution. For now I'm manually editing vector assets into CRLF before I add them to Git, which avoids the above error message, but gets tedious having to repeat the process for every file. Any way to force Android Studio to generate all local files in CRLF?

like image 839
PM4 Avatar asked Jul 08 '17 18:07

PM4


People also ask

Does git use CRLF or LF?

text eol=crlf Git will always convert line endings to CRLF on checkout. You should use this for files that must keep CRLF endings, even on OSX or Linux. text eol=lf Git will always convert line endings to LF on checkout. You should use this for files that must keep LF endings, even on Windows.

What is the meaning of LF and CRLF in git?

In Unix systems the end of a line is represented with a line feed (LF). In windows a line is represented with a carriage return (CR) and a line feed (LF) thus (CRLF). when you get code from git that was uploaded from a unix system they will only have an LF.

Does git change line endings?

While Git normally leaves file contents alone, it can be configured to normalize line endings to LF in the repository and, optionally, to convert them to CRLF when files are checked out. Here is an example that will make Git normalize . txt, . vcproj and .


2 Answers

I would not set core.autocrlf to true (I advices against it since 2010): leave it to false.

Any file you want to be managed in a .gitattributes file, especially since Git 2.10. See the release note

echo "*.java text=auto eol=crlf" >.gitattributes

The combination has been fixed to be equivalent to doing

$ git config core.autocrlf true

With .gitattributes, you can limit that eol transformation to the precise set of files you want, instead of applying it blindly on all the repo files.
See more at "Checking-out and checking-in".

And with Git 2.16+, git add --renormalize . allows for converting all concerned files eol.

like image 197
VonC Avatar answered Nov 02 '22 19:11

VonC


In Android Studio go to settings, menu - file/settings/editor/Code Style. Here you can choose default line separator.

like image 40
Dmitry Avatar answered Nov 02 '22 17:11

Dmitry