美文网首页
lint的使用

lint的使用

作者: feifei_fly | 来源:发表于2020-04-09 13:56 被阅读0次

1、lint介绍

Lint是Android Studio提供的一个代码扫描工具,通过对代码进行静态分析,可以帮助开发者发现代码的质量问题和提出一下改进建议。
image

应用源文件:源文件包含组成 Android 项目的文件,包括 Java 和 XML 文件、图标和 ProGuard 配置文件等

lint.xml文件:此配置文件可用于指定您希望排除的任何 Lint 检查以及自定义问题严重级别。

Lint工具:可以从命令行或在 Android Studio 中对 Android 项目运行此静态代码扫描工具。

Lint检查结果:可以在控制台或 Android Studio 的 Inspection Results 窗口中查看 Lint 检查结果。

  • Correctness:正确性

  • Security:安全性

  • Performance:性能

  • Usability:易用性

  • Accessibility:便利性

  • I18n:国际化

2、通过gradlew 使用lint

./gradlew lint

每个moudle都会生成两份lint报告,
保存在 模块/build/reports/中,可以参照报告查看存在的警告和错误。

> Task :busi:busi_user:lint
Ran lint on variant debug: 1 issues found
Ran lint on variant release: 2 issues found
Wrote HTML report to file:///Users/feifei/Desktop/TM/workspace/w3/busi/busi_user/build/reports/lint-results.html
Wrote XML report to file:///Users/feifei/Desktop/TM/workspace/w3/busi/busi_user/build/reports/lint-results.xml


3、通过gradle 来手动运行lint检查

Android studio中选择,Analayze->Inspect code

image

3.1、 Inspection scope 选择需要检查的代码范围:

  • Whole project 整个项目进行 lint检查
  • Moudle 针对当前选中模块进行lint检查,个人独立负责某个模块时,建议先对单独模块进行lint检查,保证模块内没有警告和错误
  • Directory 对某个指定的目录进行lint检查
  • Custom scope:可以自定义Lint检查的范围,可以实现整个项目中排除某个目录或某个文件,进行Lint检查。

3.2、 Inspection profile 可以配置lint进行哪些检查项目。

选择 Project Default ,点击右侧的"..."

image

在Inspections 页面,搜索关注的检查项,打钩的项lint会进行检查,去掉钩的项lint会忽略(不检查)

image

3.3、点击OK 按钮会开始lint检查,检查完成可以再Android Studio的Inspection Result中查看和修复问题。

如下图所示:

image

3.4、如何解除警告:

3.4.1、代码确实不规范,需要对代码进行修正,保证符合规范

3.4.2、check代码后,结论是不需修复,可以针对单个问题 进行忽略,关闭lint检查

  • 对kotlin代码 关闭lint检查
@SuppressLint("NewApi")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main)
    

要禁止 lint 检查文件中的所有问题,请使用 all 关键字

 @SuppressLint("all")
  • 对XML 关闭lint 检查
  <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        tools:ignore="UnusedResources" >

        <TextView
            android:text="@string/auto_update_prompt" />
    </LinearLayout>
    

要禁止检查多个问题,请使用以逗号分隔的字符串列出要禁止检查的问题

 tools:ignore="NewApi,StringFormatInvalid"

要禁止 lint 检查 XML 元素中的所有问题,请使用 all 关键字

 tools:ignore="all"

3.4.3、配置Inspection profile 禁止检查某一类的问题

Analayze->Inspect code ->Inspection profile ->点击"..."

image

如: 搜索框内 输入unused symbol,联想出的选项 右面的钩去掉,点击OK,lint就会禁止unused symbol的检查。

4、独立使用lint工具

lint 工具位于 android_sdk/tools/目录中。

我的mac电脑路径为/Users/feifei/Library/Android/sdk/tools/bin

  • 要查看该工具支持的标志和命令行参数的完整列表,请使用以下命令:
lint --help
  • 要对项目目录中的文件列表运行 lint,请使用以下命令:
lint [flags] <project directory>

例如 对一个名为 Earthquake 的项目运行 lint 命令,如下:

lint Earthquake
  • 查看lint所支持的所有的lint问题的 id
lint --list
image

5、w3 项目需要进行的配置项

5.1 配置custom Scope

要解决的问题:静态扫描 排除w3中的md文件

Analayze->Inspect code ->Custom scope ->点击右侧"..."

image
  • 点击加号->选择local->name 填写名字(如whole)
  • 依次将所有的module 以Include Recursively 方式加入进来。再将README.md文档和doc目录 Exclude排除在扫描路径

或者直接将下面的正则表达式 copy到pattern处

(file[app]:*/||file[arch]:*/||file[busi]:*/||file[busi_phonerecord]:*/||file[busi_user]:*/||file[mod]:*/||file[mod-mod_fit]:*/||file[mod-mod_http]:*/||file[mod-mod_probe]:*/||file[mod-mod_store]:*/||file[mod-mod_sweepback]:*/||file[w3]:*/)&&!file:README.md&&!file[w3]:doc//*

5.2 修改Inspection profile

目的:

  • 去掉unused symbol
  • private data class constructor is exposed via the "copy" method

如下图所示,将这两项右侧的对勾去掉,点击OK即可。

image

6、参考文章

相关文章

网友评论

      本文标题:lint的使用

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