本文主要介紹checkstyle在AndroidStuido中的gradle配置
1.build.gradle配置
1.在Project目錄下(App同級(jí))建立文件夾 config,并新建quality.gardle文件和quality文件夾
2.在quality文件夾下新建checkstyle文件夾泻红。里面新建 checkstyle.xml和suppressions.xml文件
3.checkstyle.xml和suppressions.xml中內(nèi)容可以參考阿里巴巴或者華為的checkstyle文件夭禽。這里主要講解下quality.gradle和項(xiàng)目中g(shù)ardle的配置。
quality.gradle文件如下:
apply plugin: 'checkstyle'
check.dependsOn 'checkstyle'
checkstyle {
toolVersion = '8.0'
}
task checkstyle(type: Checkstyle) {
configFile = file("$configDir/checkstyle/checkstyle.xml")
configProperties.checkstyleSuppressionsPath = file("$configDir/checkstyle/suppressions.xml").absolutePath
source 'src'
Iterator<String> iterator = project.getParent().getChildProjects().keySet().iterator()
while (iterator.hasNext()) {
source "${project.rootDir}/${iterator.next()}/src/main/java"
} //這里的主要操作是:將項(xiàng)目的app和子module中的java目錄下的代碼全部添加到findbugs需要查找源文件中承桥。
include '**/*.java'
exclude '**/gen/**'
exclude '**/db/**'
exclude '**/bean/**'
exclude '**/test/**'
exclude '**/androidTest/**'
exclude '**/R.java'
exclude '**/BuildConfig.java'
classpath = files()
}
然后在 app目錄下的build.gradle中添加
apply from: '../config/quality.gradle'
之后點(diǎn)擊右側(cè)的gradle-->Tasks-->other-->checkstyle即可驻粟。
在run命令上會(huì)展示出運(yùn)行信息
備注:
1.若是checkstyle的jar包下載不下來。可以在項(xiàng)目的build.gradle的allprojects下的repositories中配置阿里云的maven倉庫
maven { url "https://maven.aliyun.com/repository/jcenter/" }
maven { url "https://maven.aliyun.com/repository/gradle-plugin/" }
maven { url "https://maven.aliyun.com/repository/google/" }
2.若在運(yùn)行出錯(cuò)蜀撑,可以在Terminal輸入命令行:gradlew checkstyle --stacktrace挤巡。查看詳細(xì)打印信息。