Android Studio设置编码格式

查了几种方式都没有成功,最后找到这种方式成了,当然你可以设置成你需要的编码格式

在Project的下面打开Gradle Scripts,然后打开你的Module的buile.gradle.在其中加入如下代码:

    android {  
        ......  
      
        compileOptions {  
            encoding "UTF-8"  
        }  
    }  

其实在官网就有解决的方法,之前使用Android Studio 一遇见问题,就百度谷歌,后来发现下边的提示其实就能解决问题。

下面内容来自 官网      

Mismatched Encoding

The "encoding" of a given source file tells the compiler and the IDE how to interpret the bytes in the file as characters.

The encoding used to  compile your project is specified in the build.gradle files. The default, which is used when no specific encoding is specified, is UTF-8. We strongly recommend you use UTF-8 encoding whenever possible. The Android development tools create UTF-8 encoded projects by default, the Android Gradle plugin uses UTF-8 by default, and Android Lint will warn whenever it encounters XML files that are not using UTF-8, for example.

However, it is possible to change the encoding used by the IDE, both at the IDE level (which becomes the default for all loaded projects), on a per project level, and even on a per file level. This is specified in the Settings dialog under Editor > File Encodings.

However, this does not affect how the build system (Gradle) treats these files. This means that you can view and edit your files in the IDE using one encoding, and then having them translated by the build system using a different encoding. This can lead to really surprising and serious bugs (such as issue 163078).

If you do change the encoding in the IDE, make sure you also update the Gradle files to reflect these encodings:
android {
    ...
    compileOptions {
        encoding "UTF-8"
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}
As of Android Studio 1.2, we detect a mismatch between the encoding specified in Gradle and the encoding used by the IDE:

When you encounter the above problem (which points to the this page), either change your IDE settings or build.gradle to UTF-8 such that the two matches, or (if necessary) change your encoding to whatever custom encoding you have specified such that the two are in agreement. 

(Note: If your source files contain more than plain ASCII characters, you can't "just" change the encoding to UTF-8. If your source files were written with a custom encoding, you'll need to convert them such that the actual characters are read in with the previous encoding and written out with the new encoding.)

In a future version of the IDE, we plan to force the encoding specified in the build.gradle files to be pushed into the IDE, in the same way that the Java language setting already is.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值