6

I am working on a project where all the source files are encoded as UTF-8, to please the java compiler (otherwis, 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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Machinarius
  • 3,637
  • 3
  • 30
  • 53

6 Answers6

3

Remove BOM Didn't work for me.

It's the problem for the code I copied from a website.

I wrote it again and problem solved.

Anice Jahanjoo
  • 7,088
  • 3
  • 20
  • 29
2

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.

CJBS
  • 15,147
  • 6
  • 86
  • 135
free3dom
  • 18,729
  • 7
  • 52
  • 51
  • +1 for finding the bug ticket but I think it's still open, isn't it? There's no solution or work-around there. It doesn't look like it's possible to vote for bugs in YouTrack either. – Rup Jun 18 '13 at 08:45
  • Unfortunately, I does appear to still be an open bug :( However, there is one workaround listed, the link above should take you directly to it (it involves removing the default encoding for all files). Also, you CAN vote up if you are logged in; under the bug heading is an "up" arrow. – free3dom Jun 18 '13 at 09:16
  • Oops, yes - I should have searched the page source for 'vote' not just the page text. Done, voted - thanks. – Rup Jun 18 '13 at 10:23
2

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.

Sunbul M H
  • 21
  • 2
1

If you are using Android Studio 3.3.1

Just right-click on the file, from the menu you will find Remove BOM

niglng
  • 99
  • 1
  • 2
  • 12
  • But why did Android Studio add it in the first place, when the settings say "Create UTF-8 files: with NO BOM"? – Christian Davén Feb 27 '19 at 06:45
  • This solved the issue for me. It happened after migrating my project to use their new viewBinding feature. I wish I knew the answer to @ChristianDavén's question though... I couldn't find any info from Google about this. – hellaandrew Mar 08 '21 at 02:07
0

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
babay
  • 4,689
  • 1
  • 26
  • 40
0

for Android studio 3.4 Right Click on Android Project. List will pop up, below the list you will see No BOM, click on it and then run the project. encoding byte error issue will resolved