简体   繁体   English

Android Studio - 基于 Git 的多平台项目的 CRLF 与 LF

[英]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.我正在处理一个涉及多个开发人员的 Android 项目,其中一些在 Windows 上,其他在 Linux/MacOS 上。 Since I'm on Windows, I've been instructed to configure Git as follows to avoid issues:由于我在 Windows 上,我被指示按如下方式配置 Git 以避免出现问题:

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.我在 Android Studio 中创建的任何 .java/XML/etc 文件都在 CRLF 中,当我将它们推送到 repo 时转换为 LF,然后当我将更改拉入我的本地副本时返回到 CRLF。 Problem is, some types of files, like vector drawable assets, get generated in LF instead, for some reason.问题是,某些类型的文件,例如矢量可绘制资源,由于某种原因在 LF 中生成。 So when I try to add them to Git, I get the "irreversible conversion" error:因此,当我尝试将它们添加到 Git 时,出现“不可逆转换”错误:

在此处输入图片说明 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.我知道我可以设置safecrlf = warn ,但据我所知,如果 Git 将二进制文件误认为是文本文件,这会带来损坏二进制文件的风险,所以我正在寻找一个更安全的解决方案。 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.现在,我在将矢量资产添加到 Git 之前手动将矢量资产编辑到 CRLF 中,这避免了上述错误消息,但必须为每个文件重复该过程变得乏味。 Any way to force Android Studio to generate all local files in CRLF?有什么方法可以强制 Android Studio 在 CRLF 中生成所有本地文件?

I would not set core.autocrlf to true (I advices against it since 2010 ): leave it to false. 不会core.autocrlf设置为true (我建议自2010年起反对它 ):将其保留为false。

Any file you want to be managed in a .gitattributes file , especially since Git 2.10 . 您希望在.gitattributes文件中管理的.gitattributes文件 ,尤其是从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. 使用.gitattributes ,您可以将eol转换限制为所需的精确文件集,而不是盲目地将其应用于所有 repo文件。
See more at " Checking-out and checking-in ". 有关详细信息,请参阅“ 结帐和登记 ”。

And with Git 2.16+ , git add --renormalize . 使用Git 2.16 +git add --renormalize . allows for converting all concerned files eol. 允许转换所有相关文件eol。

In Android Studio go to settings, menu - file/settings/editor/Code Style. 在Android Studio中转到设置,菜单 - 文件/设置/编辑器/代码样式。 Here you can choose default line separator. 您可以在此处选择默认行分隔符。

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. 对于那些像我一样搜索如何为Android Studio文件设置LF或CRLF的人,您可以在IDE的右下角更改此设置。 Click "CRLF" and select another option. 单击“CRLF”并选择其他选项。 But I don't know how to do this for a whole project, not every file alone. 但我不知道如何为整个项目做到这一点,而不仅仅是每个文件。

在此输入图像描述

i prefer to use this solution我更喜欢使用这个解决方案

git config core.filemode false

it solve similar problem like CRLF vs LF mode它解决了类似的问题,如 CRLF vs LF 模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多平台项目中的 Android 上下文 - Android context in multiplatform project Jar 生产的 Kotlin 多平台库项目在 Android Studio 中不可见 - Jar produced by Kotlin Multiplatform Library project not visible in Android Studio 将Android Studio项目导出到GIT - Exporting Android Studio Project to GIT Android Studio:如何在 Kotlin 多平台共享模块 build.gradle.kts 中访问项目级 compileSdkVersion 和 targetSdkVersion? - Android Studio: How to access project level compileSdkVersion and targetSdkVersion in Kotlin Multiplatform shared module build.gradle.kts? Android Studio Git .gitignore vs 项目 > 设置 > 版本控制 > 忽略的文件 - Android Studio Git .gitignore vs Project > Settings > Version Control > Ignored Files 在Android Studio中清理项目与重建项目与运行项目 - Clean Project vs Rebuild Project vs Run Project in Android Studio ANDROID STUDIO:此项目不是基于 gradle 的项目 - ANDROID STUDIO: this project is not a gradle-based project Android studio:这个项目不是基于 gradle 的项目 - Android studio: this project is not gradle based project 将Git项目从Eclipse迁移到Android Studio - Migrate Git project from Eclipse to Android Studio git如何与项目一起使用(Android Studio) - How does git work with project ( Android Studio )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM