1、问题描述

最近在负责公司基础业务和移动基础设施的开发工作,正在负责Lint代码静态检查工作。因此编写了自定义的Lint规则,在调试过程中,编译的时候出现了如下所示的错误:

【我的Android进阶之旅】解决Android Studio 运行gradle命令时报错: 错误: 编码GBK的不可映射字符-LMLPHP

部分输出日志如下所示:

【我的Android进阶之旅】解决Android Studio 运行gradle命令时报错: 错误: 编码GBK的不可映射字符-LMLPHP

:XTCLintrRules:compileJava
D:\GitLab Source\XTCLint\XTCLintrRules\src\main\java\com\xtc\lint\rules\detectors\binaryResource\XTCImageFileSizeDetector.java:16: 错误: 编码GBK的不可映射字符
* 瀹氫箟浠g爜妫?鏌ヨ鍒?
^
D:\GitLab Source\XTCLint\XTCLintrRules\src\main\java\com\xtc\lint\rules\detectors\binaryResource\XTCImageFileSizeDetector.java:16: 错误: 编码GBK的不可映射字符
* 瀹氫箟浠g爜妫?鏌ヨ鍒?
^
D:\GitLab Source\XTCLint\XTCLintrRules\src\main\java\com\xtc\lint\rules\detectors\binaryResource\XTCImageFileSizeDetector.java:17: 错误: 编码GBK的不可映射字符
* 杩欎釜鏄拡瀵归」鐩腑鐨勫浘鐗囧ぇ灏忕殑涓?涓垽鏂紝瓒呰繃250kb鐨勫叏閮ㄦ彁閱掑嚭鏉ワ紝璁╀粬浠帇缂?
^
D:\GitLab Source\XTCLint\XTCLintrRules\src\main\java\com\xtc\lint\rules\detectors\binaryResource\XTCImageFileSizeDetector.java:17: 错误: 编码GBK的不可映射字符
* 杩欎釜鏄拡瀵归」鐩腑鐨勫浘鐗囧ぇ灏忕殑涓?涓垽鏂紝瓒呰繃250kb鐨勫叏閮ㄦ彁閱掑嚭鏉ワ紝璁╀粬浠帇缂?
^
D:\GitLab Source\XTCLint\XTCLintrRules\src\main\java\com\xtc\lint\rules\detectors\binaryResource\XTCImageFileSizeDetector.java:32: 错误: 编码GBK的不可映射字符
private static final String ISSUE_DESCRIPTION = "閿欒锛氬浘鐗囨枃浠惰繃澶?";
^
D:\GitLab Source\XTCLint\XTCLintrRules\src\main\java\com\xtc\lint\rules\detectors\binaryResource\XTCImageFileSizeDetector.java:33: 错误: 编码GBK的不可映射字符
private static final String ISSUE_EXPLANATION = "閿欒锛氬浘鐗囨枃浠惰繃澶э紝璇峰帇缂╀綘鐨勫浘鐗囨枃浠躲?傚浘鐗囧帇缂╂柟妗堝彲浠ュ弬鑰?: https://github.com/qjoy/TinyPNGNodeJSBatcher";
^
D:\GitLab Source\XTCLint\XTCLintrRules\src\main\java\com\xtc\lint\rules\detectors\binaryResource\XTCImageFileSizeDetector.java:33: 错误: 编码GBK的不可映射字符
private static final String ISSUE_EXPLANATION = "閿欒锛氬浘鐗囨枃浠惰繃澶э紝璇峰帇缂╀綘鐨勫浘鐗囨枃浠躲?傚浘鐗囧帇缂╂柟妗堝彲浠ュ弬鑰?: https://github.com/qjoy/TinyPNGNodeJSBatcher";
^
D:\GitLab Source\XTCLint\XTCLintrRules\src\main\java\com\xtc\lint\rules\detectors\binaryResource\XTCImageFileSizeDetector.java:50: 错误: 编码GBK的不可映射字符
String reportStr = "閿欒锛氬浘鐗囨枃浠惰繃澶?: %d" + "KB,瓒呰繃浜嗛」鐩檺鍒剁殑:" + CHECK_IMAGE_KB_SIZE + "KB,璇疯繘琛屽浘鐗囧帇缂╂垨鎵綰I宸ョ▼甯堥噸鏂拌璁″嚭鍥?.";

主要原因是编译Java工程的时候,编译代码的编码方式有问题。

2、解决问题

【我的Android进阶之旅】解决Android Studio 运行gradle命令时报错: 错误: 编码GBK的不可映射字符-LMLPHP

如上图所示,在Java工程的build.gradle文件中,添加如下所示的代码:

//指定编译的编码
tasks.withType(JavaCompile){
options.encoding = "UTF-8"
}

添加如上所示的代码之后,重新编译即可正常,不会再弹出警告窗口。


【我的Android进阶之旅】解决Android Studio 运行gradle命令时报错: 错误: 编码GBK的不可映射字符-LMLPHP

【我的Android进阶之旅】解决Android Studio 运行gradle命令时报错: 错误: 编码GBK的不可映射字符-LMLPHP

04-23 03:30