Android Studio 1.2 - Project encoding mismatches b

2019-01-11 15:46发布

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!

4条回答
Luminary・发光体
2楼-- · 2019-01-11 16:21

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.

查看更多
对你真心纯属浪费
3楼-- · 2019-01-11 16:24

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楼-- · 2019-01-11 16:39

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.

Screen capture of Android Studio settings window

查看更多
戒情不戒烟
5楼-- · 2019-01-11 16:41

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.

enter image description here

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.

IDE Encoding

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

查看更多
登录 后发表回答