Android Studio编写BOM的到UTF-8文件。

时间:2023-01-06 07:26:52

I am working on a project where all the source files are encoded as UTF-8 so as to please the java compiler (It will spit out errors on other encodings). Everytime i save a source file with Android Studio, it will write a Byte Order Mark on the file, preventing the compilation from succeeding. How can i fix this?

我正在开发一个项目,所有的源文件都被编码为UTF-8,以便让java编译器(它会在其他编码上输出错误)。每次我在Android Studio中保存一个源文件时,它都会在文件上写一个字节顺序标记,防止编译成功。我怎么解决这个问题?

As a workaround, i am manually deleting the BOM with a Hex Editor but it gets really annoying.

作为一个解决方案,我用一个Hex编辑器手动删除BOM,但它变得非常烦人。

3 个解决方案

#1


1  

It seems that this is a bug in IntellJ IDEA, on which Android Studio is based.

这似乎是一个关于智能的想法的bug, Android Studio是基于这个想法的。

Try this:

试试这个:

  1. In Settings | File Encodings Select the "Projekt"-Entry with the mouse in the second column and choose "" (Confrim removal of Encodings set in subdirectories or choose the directories where you don't wan't a default-encoding enforced.)
  2. 在设置中,|文件编码选择“Projekt”-在第二列中使用鼠标,并选择“”(在子目录中删除设置的编码,或者选择不需要执行默认编码的目录。)
  3. de-select "Autodetect UTF-encoded files"
  4. 取消“自动侦测UTF-encoded文件”
  5. select "Transparent native-to-ascii conversion"
  6. 选择“透明native-to-ascii转换”

(Reference for above)

(参考上图)

Another possible fix is to remove all BOMs outside Android studio (with hex editor or another text editor) and then choosing File -> Synchronize in AS.

另一个可能的解决方法是删除Android studio之外的所有bom(使用hex编辑器或另一个文本编辑器),然后选择文件->同步。

#2


0  

You can use some tool to convert your file to UTF-8 with BOM.

您可以使用一些工具将文件转换为UTF-8和BOM。

I used Notepad++:

我用记事本+ +:

  1. remove "<?xml version="1.0" encoding="utf-8"?>" from file if it is. (I used Notepad for that)
  2. 删除“< ?xml version = " 1.0 " encoding = " utf - 8 " ?如果是,则从文件中删除。(我用记事本)
  3. open file with Notepad++.
  4. 用记事本打开文件+ +。
  5. ensure you see characters as they should be
  6. 确保你看到了他们应该看到的角色。
  7. select Menu -> Encoding -> Convert to UTF-8
  8. 选择菜单->编码->转换为UTF-8。
  9. add "<?xml version="1.0" encoding="utf-8"?>" at file start
  10. 添加”< ?xml version = " 1.0 " encoding = " utf - 8 " ?>“在文件的开始

#3


0  

In Android Studio "Project" pane, right-click on the file with BOM problem(s) and select: Analyze > Inspect Code. Then click OK. You will get a list of all BOM errors in the file. You can right-click on each error and select to remove the BOM.

在Android Studio“Project”窗格中,右键单击带有BOM问题的文件并选择:分析>检查代码。然后单击OK。您将得到文件中所有BOM错误的列表。您可以右键单击每个错误并选择删除BOM。

#1


1  

It seems that this is a bug in IntellJ IDEA, on which Android Studio is based.

这似乎是一个关于智能的想法的bug, Android Studio是基于这个想法的。

Try this:

试试这个:

  1. In Settings | File Encodings Select the "Projekt"-Entry with the mouse in the second column and choose "" (Confrim removal of Encodings set in subdirectories or choose the directories where you don't wan't a default-encoding enforced.)
  2. 在设置中,|文件编码选择“Projekt”-在第二列中使用鼠标,并选择“”(在子目录中删除设置的编码,或者选择不需要执行默认编码的目录。)
  3. de-select "Autodetect UTF-encoded files"
  4. 取消“自动侦测UTF-encoded文件”
  5. select "Transparent native-to-ascii conversion"
  6. 选择“透明native-to-ascii转换”

(Reference for above)

(参考上图)

Another possible fix is to remove all BOMs outside Android studio (with hex editor or another text editor) and then choosing File -> Synchronize in AS.

另一个可能的解决方法是删除Android studio之外的所有bom(使用hex编辑器或另一个文本编辑器),然后选择文件->同步。

#2


0  

You can use some tool to convert your file to UTF-8 with BOM.

您可以使用一些工具将文件转换为UTF-8和BOM。

I used Notepad++:

我用记事本+ +:

  1. remove "<?xml version="1.0" encoding="utf-8"?>" from file if it is. (I used Notepad for that)
  2. 删除“< ?xml version = " 1.0 " encoding = " utf - 8 " ?如果是,则从文件中删除。(我用记事本)
  3. open file with Notepad++.
  4. 用记事本打开文件+ +。
  5. ensure you see characters as they should be
  6. 确保你看到了他们应该看到的角色。
  7. select Menu -> Encoding -> Convert to UTF-8
  8. 选择菜单->编码->转换为UTF-8。
  9. add "<?xml version="1.0" encoding="utf-8"?>" at file start
  10. 添加”< ?xml version = " 1.0 " encoding = " utf - 8 " ?>“在文件的开始

#3


0  

In Android Studio "Project" pane, right-click on the file with BOM problem(s) and select: Analyze > Inspect Code. Then click OK. You will get a list of all BOM errors in the file. You can right-click on each error and select to remove the BOM.

在Android Studio“Project”窗格中,右键单击带有BOM问题的文件并选择:分析>检查代码。然后单击OK。您将得到文件中所有BOM错误的列表。您可以右键单击每个错误并选择删除BOM。