Android代码静态检查(lint、Checkstyle、ktlint、Detekt) 您所在的位置:网站首页 汽车静态检查工具 Android代码静态检查(lint、Checkstyle、ktlint、Detekt)

Android代码静态检查(lint、Checkstyle、ktlint、Detekt)

2023-12-15 04:54| 来源: 网络整理| 查看: 265

Android代码静态检查(lint、Checkstyle、ktlint、Detekt)

在这里插入图片描述

在Android项目开发过程中,开发团队往往要花费大量的时间和精力发现并修改代码缺陷。

静态代码分析工具能够在代码构建过程中帮助开发人员快速、有效的定位代码缺陷并及时纠正这些问题,从而极大地提高软件可靠性

节省软件开发和测试成本。

Android目前主要使用的语言为kotlin、java,所以我们需要尽可能支持这两种语言。

Lint

Android Studio 提供的代码扫描工具。通过进行 lint 检查来改进代码

能检测什么?是否包含潜在错误,以及在正确性、安全性、性能、易用性、便利性和国际化方面是否需要优化改进,帮助我们发现代码结/质量问题,同时提供一些解决方案。每个问题都有信息描述和等级。

支持【300+】检测规则,支持Manifest文件、XML、Java、Kotlin、Java字节码、Gradle文件、Proguard文件、Propetty文件和图片资源;

基于抽象语法树分析,经历了LOMBOK-AST、PSI、UAST三种语法分析器;

主要包括以下几个方面

Correctness:不够完美的编码,比如硬编码、使用过时 API 等;Performance:对性能有影响的编码,比如:静态引用,循环引用等;Internationalization:国际化,直接使用汉字,没有使用资源引用等;Security:不安全的编码,比如在 WebView 中允许使用 JavaScriptInterface 等

在module下的build.gradle中添加以下代码:

android { lintOptions { // true--关闭lint报告的分析进度 quiet true // true--错误发生后停止gradle构建 abortOnError false // true--只报告error ignoreWarnings true // true--忽略有错误的文件的全/绝对路径(默认是true) //absolutePaths true // true--检查所有问题点,包含其他默认关闭项 checkAllWarnings true // true--所有warning当做error warningsAsErrors true // 关闭指定问题检查 disable 'TypographyFractions','TypographyQuotes' // 打开指定问题检查 enable 'RtlHardcoded','RtlCompat', 'RtlEnabled' // 仅检查指定问题 check 'NewApi', 'InlinedApi' // true--error输出文件不包含源码行号 noLines true // true--显示错误的所有发生位置,不截取 showAll true // 回退lint设置(默认规则) lintConfig file("default-lint.xml") // true--生成txt格式报告(默认false) textReport true // 重定向输出;可以是文件或'stdout' textOutput 'stdout' // true--生成XML格式报告 xmlReport false // 指定xml报告文档(默认lint-results.xml) //xmlOutput file("lint-report.xml") // true--生成HTML报告(带问题解释,源码位置,等) htmlReport true // html报告可选路径(构建器默认是lint-results.html ) //htmlOutput file("lint-report.html") // true--所有正式版构建执行规则生成崩溃的lint检查,如果有崩溃问题将停止构建 checkReleaseBuilds true // 在发布版本编译时检查(即使不包含lint目标),指定问题的规则生成崩溃 fatal 'NewApi', 'InlineApi' // 指定问题的规则生成错误 error 'Wakelock', 'TextViewEdits' // 指定问题的规则生成警告 warning 'ResourceAsColor' // 忽略指定问题的规则(同关闭检查) ignore 'TypographyQuotes' } }

运行./gradlew lint,检测结果在build/reports/lint/lint.html可查看详情。

在这里插入图片描述

CheckStyle

Java静态代码检测工具,主要用于代码的编码规范检测 。

CheckStyle是Gralde自带的Plugin,The Checkstyle Plugin

通过分析源码,与已知的编码约定进行对比,以html或者xml的形式将结果展示出来。

其原理是使用Antlr库对源码文件做词语发分析生成抽象语法树,遍历整个语法树匹配检测规则。

目前不支持用户自定义检测规则,已有的【100+】规则中,有一部分规则是有属性的支持设置自定义参数。

在module下的build.gradle中添加以下代码:

/** * The Checkstyle Plugin * * Gradle plugin that performs quality checks on your project's Java source files using Checkstyle * and generates reports from these checks. * * Tasks: * Run Checkstyle against {rootDir}/src/main/java: ./gradlew checkstyleMain * Run Checkstyle against {rootDir}/src/test/java: ./gradlew checkstyleTest * * Reports: * Checkstyle reports can be found in {project.buildDir}/build/reports/checkstyle * * Configuration: * Checkstyle is very configurable. The configuration file is located at {rootDir}/config/checkstyle/checkstyle.xml * * Additional Documentation: * https://docs.gradle.org/current/userguide/checkstyle_plugin.html */ apply plugin: 'checkstyle' checkstyle { //configFile = rootProject.file('checkstyle.xml') configProperties.checkstyleSuppressionsPath = rootProject.file("suppressions.xml").absolutePath // The source sets to be analyzed as part of the check and build tasks. // Use 'sourceSets = []' to remove Checkstyle from the check and build tasks. //sourceSets = [project.sourceSets.main, project.sourceSets.test] // The version of the code quality tool to be used. // The most recent version of Checkstyle can be found at https://github.com/checkstyle/checkstyle/releases //toolVersion = "8.22" // Whether or not to allow the build to continue if there are warnings. ignoreFailures = true // Whether or not rule violations are to be displayed on the console. showViolations = true } task projectCheckStyle(type: Checkstyle) { group 'verification' classpath = files() source 'src' //include '**/*.java' //exclude '**/gen/**' reports { html { enabled = true destination file("${project.buildDir}/reports/checkstyle/checkstyle.html") } xml { enabled = true destination file("${project.buildDir}/reports/checkstyle/checkstyle.xml") } } } tasks.withType(Checkstyle).each { checkstyleTask -> checkstyleTask.doLast { reports.all { report -> // 检查生成报告中是否有错误 def outputFile = report.destination if (outputFile.exists() && outputFile.text.contains("


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有