0x01. 在Project根目录新建 build_check.gradle文件

task checkEncoding () {
    group = 'Build Check'
    description = 'Check Project File Encoding'
    def file = new File("${rootDir}/.idea/encodings.xml")
    if (file.exists()) {
        XmlParser xmlParser = new XmlParser();
        Node node = xmlParser.parse(file)
        println node
        assert node.name() == 'project'
        assert node.@version == '4'
        assert node.component[0].@name == 'Encoding'
        assert node.component[0].file[0].@charset == 'UTF-8'
    } else {
        throw new GradleException("没有找到编码配置文件!")
    }
}

0x02. 在 Projectbuild.gradle文件中添加引用

apply from : 'build_check.gradle'
buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'


        // NOTE: Do not place your application build_config here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

0x03. 执行 gradle checkEncoding任务查看结果

如果抛出异常,请检查 Project目录下 .idea目录下是否包含 encodings.xml文件