Android Studio 1.2 - Project encoding mismatches b

2019-01-11 16:05发布

问题:

I'm a bit of an Android rookie, and I just upgraded to Android Studio 1.2. When I make a new project (API 16: Android 4.1 (Jelly Bean)), I immediately get the following warning:

Warning:The project encoding (windows-1252) does not match the encoding specified in the Gradle build files (UTF-8). This can lead to serious bugs.
More Info...
Open File Encoding Settings

This is without me having made any changes or written any code whatsoever. I read up on more info on tools.android.com (http://tools.android.com/knownissues/encoding) but it is incredibly vague and assumes knowledge on what's going on (this is where "More info..." takes you). Since I updated to Android Studio 1.2, the design and text tabs in my XML-files won't even sync up; the design tab cannot be edited while the textfile of XML-files can be. I assume that's a related issue.

Why does it mismatch this encoding by default, what can I do about it and what should I use as default encoding?

Thank you!

回答1:

Click File -> Settings (or click Ctrl + Alt + S) and find File Encodings in your Android Studio. See the image below as a reference.

Now you have three options

  • Change project encoding to match UTF-8 since both Gradle and Android Lint use this (recommended),
  • or change IDE encoding to match other encoding,
  • or just simply ignore the warning. If it worked out for you so far, it will continue to work anyway

http://shrani.si/f/3V/p7/5xt1uxV/encoding.png

Once you see the warning, you can directly click on "Open File Encoding Settings" as shown in the image below. This will take you to the same place as described above.

You can also see the current encoding of your file at the bottom right corner of Android studio. You can also change it there. See the image below.

Also you can specify charset in your build.gradle script like this

android {
    ...
    compileOptions {
        encoding "UTF-8"
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

According to Ashl7's comment, you need to do a gradle sync for this to work



回答2:

Instead of changing UTF-8 to match system default, I would recommend to set your Project Encoding to UTF-8 (File -> Settings -> Editor -> File Encodings). UTF-8 is one of the most common file encodings, and it uses space efficiently by encoding characters from using 1 to 4 bytes as needed.

Google recommends using UTF-8, and both Gradle and Android Lint use UTF -8: File Encoding for Android Studio

I had the exact same issue after upgrading to Android Studio 1.2.



回答3:

I am or was having the same problem. So I changed it so that the IDE and project encodings and the default encoding for properties files were all UTF-8. But I kept seeing the same warning in the gradle sync tab of the messages. Then eventually I simply closed that tab, and have not seen the warning message since. Presumably the IDE would reraise that tab if the warning condition again applied. Now when I make the project, neither the gradle sync tab nor the warning message appear. So maybe the warning was written appropriately during one make, but should have been cleared or somehow removed at the start of a new make, but wasn't, meaning that it is a bug in the IDE.

But every time I create a new project, the problem comes up again. There ought to be a setting to cause all the encodings to match every time a new project is created.



回答4:

You can change the default encoding settings for new projects by clicking on File -> Other Settings -> Default Settings.

Under "Editor" and "File Encodings" you can set IDE Encoding and Project Encoding to the same value to avoid getting the error message with future projects.

Setting it under File -> Settings will change it just for the currently open project.