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

4

14

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?

Hexagon answered 8/7, 2017 at 18:31 Comment(1)
Are the marked as text in .gitattributes? See help.github.com/articles/dealing-with-line-endingsBoston
L
17

I would not set core.autocrlf to true (I have advised against it since 2010); leave it to false.

For 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.

Longford answered 9/7, 2017 at 6:53 Comment(0)
L
12

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

Landsturm answered 8/11, 2018 at 12:22 Comment(0)
E
11

For those who is like me searching how to set LF or CRLF for Android Studio files, you can change this setting in a right bottom corner of the IDE. Click "CRLF" and select another option. But I don't know how to do this for a whole project, not every file alone.

enter image description here

Effortless answered 1/7, 2018 at 12:30 Comment(3)
For all files, a git add --renormalize . can help: see my edited answer above.Longford
@VonC, thank you! I executed this command and got all the text files in a project changed (git offers to commit them all). :)Effortless
strange - this didn't show up for me. Only the git branch showed.Lyndell
A
1

i prefer to use this solution

git config core.filemode false

it solve similar problem like CRLF vs LF mode

Allynallys answered 27/10, 2021 at 3:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.