Android 中性能优化之lint使用总结

Posted yuminfeng728

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 中性能优化之lint使用总结相关的知识,希望对你有一定的参考价值。

概述

lint是随着android SDK自带的静态代码分析工具,用来对Android工程的源文件进行检查,找出在正确性,安全,性能,可使用性,可访问性及国际化等方面可能的bugs和可优化提升的地方。其中包含一个重要的作用,就是检查项目中不用的资源文件。

lint工作流程

正对我们的项目,通过lint.xml配置lint分析的选项,执行检查后,会针对要分析的issue生成报告。

运行lint

在Android studio 中右边的菜单栏中可以找到Gradle的工具栏,展开verification,可以找到lint选项,并点击运行:

lint分析完成后,可以生成xml、html格式的报告文档。报告位置会在执行结束时,提示。文档具体指出了代码在不同方面所发现的问题,每个问题项都指出了在源文件的位置和问题描述。

设置分析的范围

lint工具提供了对代码的一系列不同方面的问题进行检查的功能。每一个问题(issue)都有它唯一的id。这些issue是分类的。
可以通过为工程配置lint.xml来指定要分析哪些方面的问题:
每个issue一个标签,可以指定它的严重性(severity)为ignore、error、warning等:
下面是一个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>

在xml和java文件中,也可以禁用lint检查:
● To disable lint checking for a specific Java class or method, use the @SuppressLint annotation.
● To disable lint checking for specific sections of your XML file, use the tools:ignore attribute.

以上是关于Android 中性能优化之lint使用总结的主要内容,如果未能解决你的问题,请参考以下文章

Android 性能优化:使用 Lint 优化代码去除多余资源

android -------- Lint优化工具

Android性能优化之TraceView和Lint使用详解

PHP中性能优化之生成器

PHP中性能优化之生成器

Cocos开发中性能优化工具介绍之使用Windows任务管理器