美文网首页Android 性能优化
(八)上篇 Android 性能优化 Lint 工具使用

(八)上篇 Android 性能优化 Lint 工具使用

作者: 科技猿人 | 来源:发表于2020-07-13 21:28 被阅读0次

小酌鸡汤

青春须早为,岂能长少年。

本文来源《Android 性能优化 全家桶》

为什么通过 lint 检查改进代码?

 除了通过构建测试来确保您的应用符合其功能要求之外,您务必还要通过 lint 运行您的代码来确保代码不存在结构问题。结构不合理的代码会影响 Android 应用的可靠性和效率,并使您的代码更难以维护,而 lint 工具有助于找到这些代码。
 例如,如果 XML 资源文件包含未使用的命名空间,则不仅占用空间,还会导致不必要的处理。其他结构问题(如使用目标 API 版本不支持的已弃用的元素或 API 调用)可能会导致代码无法正常运行。 lint 可帮助您解决这些问题。

lint 工具如何处理应用源文件?

Lint工作原理
  • 应用源文件:源文件包含组成 Android 项目的文件,包括 Java、Kotlin 和 XML 文件、图标以及 ProGuard 配置文件。
  • lint.xml 文件:一个配置文件,可用于指定要排除的任何 lint 检查以及自定义问题严重级别。
  • lint 工具:一个静态代码扫描工具,您可以从命令行或在 Android Studio 中对 Android 项目运行该工具
  • lint 检查结果:您可以在控制台或 Android Studio 的 Inspection Results 窗口中查看 lint 检查结果。

现在,就一起实操体验 Lint ~

(1)lint实操环境(可选项,用自己的环境和代码也一样)
  • SamplePop环境如下:
     Android Studio 4.0
     Gradle version 6.1.1
     Android API version 30
(2)从命令行运行 lint
//windows 
gradlew lint

//Linux 或 Mac
./gradlew lint

//如果您只想对某个特定的构建变体运行 lint 任务,则在大写变体名称并在其前面加上 lint 前缀
 gradlew lintDebug

//会有如下输出:
> Task :lint:lint
Ran lint on variant debug: 9 issues found
Ran lint on variant release: 9 issues found
Wrote HTML report to file:///F:/Git/Blog/SamplePop/lint/build/reports/lint-results.html
Wrote XML report to file:///F:/Git/Blog/SamplePop/lint/build/reports/lint-results.xml

 进入文件夹,找到lint-results.html,双击这个文件


Lint命令行生成分析结果文件 Lint命令行生成分析结果展示

常见问题可以分为如下几个大类:

(1)Accessibility 辅助选项,比如ImageView的contentDescription往往建议在属性中定义等。
(2)Compliance 合规性,违反了Google Play的要求,比如使用了过期的库版本,性能、安全性、API等级等没有遵循新系统的要求等。
(3)Correctness 不够完美的编码,比如硬编码、使用过时API等。
(4)Internationalization 国际化,直接使用汉字,没有使用资源引用等
(5)Interoperability 互操作性,比如和Kotln的交互等。
(6)Performanc 对性能有影响的编码,比如:静态引用,循环引用等
(7)Security 不安全的编码,比如在 WebView 中允许使用 JavaScriptInterface等
(8)Usability 可用的,有更好的替换的 比如排版、图标格式建议.png格式等

(3)使用独立工具运行 lint(不实用,忽略即可)
//工具路径:android_sdk/tools/

//要对项目目录中的文件列表运行 lint
lint [flags] <project directory>

//例如:
lint --check MissingPrefix myproject 

//查看帮助
lint --help
(4)Lint配置

您可以配置不同级别的 Lint 检查:

  • 全局(整个项目)
  • 项目模块
  • 生产模块
  • 测试模块
  • 打开的文件
  • 类层次结构
  • 版本控制系统 (VCS) 范围
(4.1)Android Studio 中配置 Lint(内置的 Lint)
  • 在AS代码编辑器中的弹出文本查看。lint 发现问题后,会用黄色突出显示有问题的代码,而对于更严重的问题,则会在代码下面添加红色下划线。
  • 依次点击 Analyze > Inspect Code 后,在 lint Inspection Results 窗口中查看。
(4.2)配置 lint.xml 文件
//lint.xml文件建议放置在 Android 项目的根目录下,build.gradle配置如下:
android {
    lintOptions {
        lintConfig file("lint.xml")
    }
}

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

        <!-- 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>

(4.3)配置 Java、Kotlin 和 XML 源文件的 lint 检查
//1 配置 Java 或 Kotlin 的 lint 检查
//1.1 停用 lint 检查,请向该代码添加 @SuppressLint 注释
@SuppressLint("NewApi")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main)

//1.2 对 FeedProvider 类中的 ParserError 问题关闭 lint 检查
@SuppressLint("ParserError")
    class FeedProvider : ContentProvider() {

//1.3 禁止 lint 检查文件中的所有问题
 @SuppressLint("all")

//2 配置 XML 的 lint 检查
//2.1  tools:ignore 属性对 XML 文件的特定部分停用 lint 检查
//布局文件的 <LinearLayout> 元素中的 UnusedResources 问题关闭 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>

//2.2 禁止检查多个问题,请使用以逗号分隔的字符串列出要禁止检查的问题
tools:ignore="NewApi,StringFormatInvalid"

//2.3 要禁止 lint 检查 XML 元素中的所有问题,请使用 all 关键字
tools:ignore="all"

(4.4)通过 Gradle 配置 lint 选项
//build.gradle配置如下:
android {
    lintOptions {
        // Turns off checks for the issue IDs you specify.
        disable 'TypographyFractions','TypographyQuotes'
        // Turns on checks for the issue IDs you specify. These checks are in
        // addition to the default lint checks.
        enable 'RtlHardcoded','RtlCompat', 'RtlEnabled'
        // To enable checks for only a subset of issue IDs and ignore all others,
        // list the issue IDs with the 'check' property instead. This property overrides
        // any issue IDs you enable or disable using the properties above.
        check 'NewApi', 'InlinedApi'
        // If set to true, turns off analysis progress reporting by lint.
        quiet true
        // if set to true (default), stops the build if errors are found.
        abortOnError false
        // if true, only report errors.
        ignoreWarnings true
    }
}  
(5)创建警告基准

 您可以为项目的当前警告集创建快照,然后将该快照用作将来运行检查的基准,以便只报告新问题。 有了基准快照,您便可开始使用 lint 让构建失败,而不必先返回并解决所有现有问题。
 个人感觉尤其是对多人的项目协同时,此基准特别有用。

//要创建基准快照,请修改项目的 build.gradle 文件,如下所示:
 android {
      lintOptions {
        baseline file("lint-baseline.xml")
      }
    }

//自定义基准:如果要将某些问题类型(而不是全部)添加到基准
android {
      lintOptions {
        check 'NewApi', 'HandlerLeak'
        baseline file("lint-baseline.xml")
       }
    }

 首次添加此代码行时,系统会创建 lint-baseline.xml 文件以建立基准。此后,lint 工具仅读取该文件以确定基准。如果要创建新基准,请手动删除该文件并再次运行 lint 以重新创建它。

Lint生成警告基准

 实行基准时,您会收到一条信息性警告,告知您一个或多个问题已被过滤掉,因为它们已在基准中列出。之所以发出这条警告,是为了帮您记住您已配置基准,因为理想情况下,您希望在某一时刻解决所有问题。

Lint警告基准的已过滤警告提示
(6)手动运行检查

 您可以通过依次选择 Analyze > Inspect Code,手动运行配置的 lint 及其他 IDE 检查。检查结果将显示在 Inspection Results 窗口中。
 追求细节请查阅文章末尾的谷歌官网(最全最详细)。

Lint设置检查范围和配置文件 Lint手动运行检查结果展示
赶紧对自己的项目代码实操开始吧~ 会有惊喜~

小编的扩展链接

参考链接

姿容清丽厌奢华,淡淡平平不自夸

举手之劳,赞有余香! ❤ 比心 ❤

相关文章

网友评论

    本文标题:(八)上篇 Android 性能优化 Lint 工具使用

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