36

Before asking the question, I searched for a similar question on StackOverflow:

file was loaded in the wrong encoding: 'UTF-8' in android studio

But it did not give an explicit answer there.


My issue is that the project is downloaded from the network, and it is an eclipse project, I open it with IntelliJ IDEA, and get this issue:

enter image description here

GiovanyMoreno
  • 389
  • 3
  • 13
qg_java_17137
  • 3,310
  • 10
  • 41
  • 84
  • 3
    See the hyperlink "reload in another encoding"? Click it! – Tarik Apr 01 '17 at 06:48
  • 5
    Tarik...sure, but... What encoding was it loaded in? What encoding should it have instead been loaded in? If the IDE is aware it was loaded in the wrong one, why not load it in the right one? Something is happening here, and the reason is not clear. It would be good to know what is going on. Can anyone provide a little detail? – RCM May 05 '17 at 19:40
  • One of the benefits of UTF-8 is that it is possible to detect errors. There are byte combinations that are invalid in UTF-8 that may be valid in other encodings. My guess is that your file contained some text (in the comments) that is not valid UTF-8. – Mike Harris Sep 16 '17 at 23:12
  • 1
    Does this answer your question? [file was loaded in the wrong encoding: 'UTF-8' in android studio](https://stackoverflow.com/questions/39932158/file-was-loaded-in-the-wrong-encoding-utf-8-in-android-studio) – Benny Apr 30 '20 at 09:22
  • Simply just delete the IDE's Settings Directory, See the answer here: [file was loaded in the wrong encoding: 'UTF-8' in the Android Studio](https://stackoverflow.com/a/61488150/3621912) – Benny Apr 28 '20 at 19:19

9 Answers9

15

As Tarik point out, click the Reload in another encoding, and if you want UTF-8 encoding, then click the more -> UTF-8.

aircraft
  • 25,146
  • 28
  • 91
  • 166
  • I think it's the case that the IDE assumes UTF-8 by default and only throws this error if it detects that the file is NOT encoded in UTF-8. But while the IDE can detect "not UTF-8", it isn't smart enough to guess what the correct format should be. So it's not really a matter of choosing UTF-8. Rather, if you get this error, you want to choose something else from the list (whichever format your file actually is in). Of course that assumes you know the format your file is in, which you might not... – RCM May 05 '17 at 21:43
  • 1
    My suggestion: make a copy of the file and try to convert to a format. Then compare the files to find where the difference is. Right clicking on the content of the file you will find the "File Encoding" menu option where you can change the encoding even if the IDE is not complaining about it. When I tried to convert it back to UTF-8 it showed an error message and gave a guess of what would be the correct format. At the end I found the problem was in the "jalapeño" word, so I fixed it manually and saved it as UTF-8 at the end. – Constantino Cronemberger Jun 13 '17 at 17:29
  • when you choose UTF-8 again, you need to click on the button "convert" when pop appear – Yehouda Jan 18 '22 at 11:06
3

in my case , when i uploaded the first .ttf file in assert folder , android studio asked me to choose one file type , and when I selected text type . this problem start to appear . the right file type is : 'Files Opened In Associated Applications'. to fix your problem , you have to open preferences ->Editor ->File Types and search for .ttf extension then remove it and add it again under 'Files Opened In Associated Applications' enter image description here

enter image description here

  • Exactly my case, but now the gradle complains like "Error: The file name must end with .xml", any clues? – Hack06 Apr 01 '18 at 10:50
3

I was having the same problem.. and i solved it by changing the CR to CRLF AND then refresh the project like done in following image

CHOOSE CRLF (image)

Machavity
  • 30,841
  • 27
  • 92
  • 100
Dinesh Sarma
  • 461
  • 3
  • 14
2

You can try opening your file with an other text editor, like Notepad++. Sometimes it can detect the correct encoding of your file, using a different pattern than IntelliJ. Then you will be able to use the "Reload..." of IntelliJ.

Rodjf
  • 177
  • 1
  • 12
1

Click on setting symbol just right side of error and select suppress for class. That works for me and resolve my issue.

Umesh Sonawane
  • 517
  • 6
  • 17
1

Though the encoding setting was present in the pom.xml, IntelliJ was trying to load all test files using UTF-8 instead of ISO-8859-1, so I found this in the IntelliJ docs: Encoding in IntelliJ Which, it basically was...

  1. Open preferences (in macOS hit, command + ,)
  2. Go to Editor / File Encodings
  3. Select the project correct encoding if it defers from your pom.xml - project.build.sourceEncoding print from File Encodings IntelliJ

By doing so, it did stop asking me to reload the file with the correct encoding.

s_bighead
  • 1,014
  • 13
  • 24
0

My colleague works on Windows while I work on Ubuntu 16.04

Click the Reload in another encoding, and click the more -> GBK.

Then we can see the Chinese character.

Francis Bacon
  • 4,080
  • 1
  • 37
  • 48
0

I tried all possibilities posted here but any success with them, what worked was just to re import the project

Jimmy Alvarez
  • 587
  • 1
  • 5
  • 13
0

If you have previously loaded this file without problems, and this is a new issue for you, try invalidating the caches in your IDE.

An easy way to open this option is by opening the super-search functionality (hit shift twice) and search for 'cache' in the 'actions' tab.

enter image description here

When prompted, choose the following option:

enter image description here

ninjaPixel
  • 6,122
  • 3
  • 36
  • 47