美文网首页
强制执行Lint规范代码

强制执行Lint规范代码

作者: yin_xin | 来源:发表于2022-06-02 16:18 被阅读0次

Lint

开发中使用静态代码检测工具对代码进行检查,达到规范代码减少bug的目的。常用的检测工具有FindBugs、PMD、Coverity,Checkstyle。
Android工程使用Lint是最方便。Lint作为官方配套工具,功能完善,自定义扩展性强,常规需求基本可以做到无需修改。
自定义Lint网上相关文章也很多,不在本篇讨论范围之中。

现实

虽然Lint很好,能够在代码中实时提示,但只要不影响到编译流程,改不改,全看开发者自觉。所以我们需要增加一些强制性,不修改就不能继续。

  1. 将Lint任务部署到CI中,Pull Request进行Lint任务,不通过则不允许提交
  2. hook到git commit,在git commit时执行Lint任务
  3. 本地编译时执行Lint任务,不通过中断编译

根据实际情况选择即可,下面对方案3讲解一下

插入到编译过程中

众所周知,apk生成或者aar生成会执行assemble任务,子module时在不输出aar并不会执行这个任务,经过
观察会执行bundleLibRuntimeToDirxxxx任务,所以可以把Lint任务插入到这两个任务之前执行。

common.gradle, 基于AGP7.0+

/**当设置为true时每次运行都会进行lint,设置为false时只会在发布版本时检查*/
def lintAlways = true

def isAppModule = plugins.hasPlugin('com.android.application')
def isLibraryModule = plugins.hasPlugin('com.android.library')

//lint检测
if (isAppModule) {
    android.applicationVariants.all { variant ->
        def lintTask = tasks["lint${variant.name.capitalize()}"]
        variant.assembleProvider.get().dependsOn lintTask
    }
} else if (isLibraryModule) {
    android.libraryVariants.all { variant ->
        def lintTask = tasks["lint${variant.name.capitalize()}"]
        if (lintAlways) {
            def runTask = tasks["bundleLibRuntimeToDir${variant.name.capitalize()}"]
            if (runTask != null) {
                //直接运行时也进行lint
                runTask.dependsOn lintTask
            }
        }
        //打包成aar
        variant.assembleProvider.get().dependsOn lintTask
    }
}

最后在每个模块引入这个gradle即可。

Lint配置

只有error或者fatal级别的issue,才会中断编译。如果我们想修改issue等级或者我们想忽略某些文件错误。可以在工程根目录添加lint.xml文件,

<?xml version="1.0" encoding="UTF-8"?>
<lint>
    <!-- Disable the given check in this project -->
    <issue id="IconMissingDensityFolder" severity="ignore" />

    <!-- Ignore the ObsoleteLayoutParam issue in the specified files -->
    <issue id="ObsoleteLayoutParam">
        <ignore path="res/layout/activation.xml" />
        <ignore path="res/layout-xlarge/activation.xml" />
    </issue>

    <!-- Ignore the UselessLeaf issue in the specified file -->
    <issue id="UselessLeaf">
        <ignore path="res/layout/main.xml" />
    </issue>

    <!-- Change the severity of hardcoded strings to "error" -->
    <issue id="HardcodedText" severity="error" />
</lint>

配置好后,当有error级别的issue未解决时就不编译不通过,督促开发人员进行修改。

相关文章

  • 强制执行Lint规范代码

    Lint 开发中使用静态代码检测工具对代码进行检查,达到规范代码减少bug的目的。常用的检测工具有FindBugs...

  • Lint代码规范记录

    背景 最近在用lint做一个代码规范的检测,虽然意义不是特别大,但是也有一定的作用,这里把自己遇到的一些给记录一下...

  • android 代码规范 Lint

    code clear(清除无用import package 未使用的资源文件 .xml .png 等) And...

  • 用 husky 和 lint-staged 构建超溜的代码检查工

    具备基本工程素养的同学都会注重编码规范,而代码风格检查(Code Linting,简称 Lint)是保障代码规范一...

  • 前端项目中使用husky做预检查

    具备基本工程素养的同学都会注重编码规范,而代码风格检查(Code Linting,简称 Lint)是保障代码规范一...

  • 这可能是最好的AndroidLint开源库

    Android Lint 这是一个通用Android Lint库,你可以用它来检查代码规范、bug、资源命名等✌️...

  • StyleLint 的配置与注意的点

    lint 用于确保代码风格,根据配置的代码规范,进行检查。stylelint 用于样式规范检查与修复,支持 .cs...

  • vue - ESlint

    vue - ESlint 代码规范 怎样取消 配置package.json 运行 npm run lint 自动...

  • ESLint简介

    Lint是什么?* Lint 是检验代码格式工具的一个统称* 这类工具主要做两件事情: -- 提供编码规范; ...

  • 代码规范

    代码规范 作为前端工程化的第一步,就是要统一代码规范。而前端的代码规范,用三个插件就能保证(husky lint-...

网友评论

      本文标题:强制执行Lint规范代码

      本文链接:https://www.haomeiwen.com/subject/ipeqmrtx.html