Android Studio writing BOM's to UTF-8 file

2019-01-26 00:43发布

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?

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

3条回答
Viruses.
2楼-- · 2019-01-26 00:55

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.

查看更多
男人必须洒脱
3楼-- · 2019-01-26 01:10

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

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. de-select "Autodetect UTF-encoded files"
  3. select "Transparent native-to-ascii conversion"

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

查看更多
来,给爷笑一个
4楼-- · 2019-01-26 01:16

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

I used Notepad++:

  1. remove "<?xml version="1.0" encoding="utf-8"?>" from file if it is. (I used Notepad for that)
  2. open file with Notepad++.
  3. ensure you see characters as they should be
  4. select Menu -> Encoding -> Convert to UTF-8
  5. add "<?xml version="1.0" encoding="utf-8"?>" at file start
查看更多
登录 后发表回答