eclipse lint工具介绍


一、基本介绍

作为移动应用开发者,我们总希望发布的apk文件越小越好,不希望资源文件目录中没有用到的图片等资源也被打包进apk,不希望应用中使用了高于minSdk的api,也不希望AndroidManifest文件存在异常,lint就能解决我们的这些问题。

Android lint是在ADT 16提供的新工具,它是一个代码扫描工具,能够帮助我们识别代码结构存在的问题,主要包括:

  • Usability problems (like not specifying an input type on a text field) 可用性问题(如不指定输入类型的文本字段)
  • Layout performance problems (all the issues the old layoutopt tool used to find, and more) 布局性能问题
  • Unused resources未使用的冗余资源
  • Inconsistent array sizes (when arrays are defined in multiple configurations)在多个配置中的数组大小不一致文件
  • Accessibility and internationalization problems (hardcoded strings, missing contentDescription, etc) 可访问性和国际化的问题(硬编码字符串,缺少contentDescription等)
  • Icon problems (like missing densities, duplicate icons, wrong sizes, etc) 图标的问题
  • Missing translations (and unused translations)没有翻译的文本
  • Manifest errors 配置文件的错误

Android lint可以解决如上的问题,当然还有更多,具体的可以参考Android Lint Checks。

Android官方也总结了lint能解决的问题,如下图(对应lint窗口中的Category一栏的值)

  • correctness 正确性
  • usability 可用性
  • security 安全性
  • accessibility 可访问性
  • performance 表现,性能
  • I18n 国际化

 /

lint是命令工具,它已经完美的集成到了Eclipse中,我们可以方便的使用。

通过lint,我们可以检测出每个问题的说明和问题的严重性,根据检测报告可以对程序作出改进。


二、命令行工具

在Android SDK的tools目录下有个叫lint.bat的文件,它就是lint的命令行工具。

2.1、扫描项目

    命令行定位到目录【 sdk/tools】(若设置了环境变量则可直接运行lint命令),运行命令【 lint xxx】

    lint命令后可以带一个或多个参数,参数之间用空格隔开,参数表示的是需要使用lint进行扫描的Android项目的目录

    

2.2、检查规则

  • 在执行lint命令时可以通过【--disable】选项要指定关闭的检查规则项
  • 在执行lint命令时可以通过【--enable】选项开启默认是关闭的检查项
  • 在执行lint命令时可以通过【--check】选项后跟检查项id来指定只进行某些检查   UnusedResources
  • 可以通过lint的【--list】选项来得到检查项类别和检查项id
  • 可以通过lint的【--show】选项后跟检查项id来得到一个检查项的详细说明
  • 可以使用【--help】选项来得到lint命令的一些帮助信息
    这些选项后跟要关闭的检查规则项的id或检查规则项的类型(Category,详见第四大点)。
    关于lint检查项的id和类型等信息请参考《Android Lint 检查规则列表》
    如:


2.3、输出html、TXT形式的报告

    在lint中,我们可以通过【--html】选项后接文件路径的形式把代码扫描结果以【html或TXT】文件的形式进行输出。

后缀名改为TXT则可生成纯文本文件:


2.4、批量删除未使用的资源文件

本文重点!当你第一次运行时 发现需要数千资源文件需要删除的时候就会很伤脑筋 手工逐条删除并不符合程序猿三大优秀品质:懒惰、没有耐心、骄傲 尝试过使用 vim 删除  发现操作起来也相当麻烦 大家可以参考下面的代码 使用FIle 获取生成的.txt中的文件信息 调用 File.delete();  方法删除 

详见另一个笔记《lint删除无用图片最佳示例》


三、在Eclipse中使用lint

从ADT16开始,lint就集成到了ADT中。该它在lint命令行的基础上新增了以下功能:
  • Automatic fixes for many warnings    自动修正大量警告【自动修复了许多警告】
  • Lint gets run automatically on various editing operations    当编辑操作完成后,立即自动运行【Lint在执行各种编辑操作之后会自动运行】
  • Ability to suppress types of errors as well as specific instances of an error    可以忽略一种类型的error,也可以忽略特定的一个error【能够抑制各种类型的错误和指定错误的实例】
  • Ability to configure issue severities    能够配置问题的严重性
  • Jump directly to the problem source from the lint view    通过lint视图能直接跳转到其问题对应的源码处
缺点:需要手动删除  当冗余文件数量多的时候会很伤脑筋

3.1、Automatic Lint
    Lint将在以下情况下自动运行:
  • Export an APK. In this case it runs lint in a special mode which only looks for fatal errors (which is faster) and aborts the export if any fatal errors are found. You can turn off this in the Lint Options
    在导出APK文件的时候,lint会做快速的扫描,以寻找重大的[致命的]错误。如果发现有重大的错误,导出APK的操作将被迫终止。
    【导出APK.这样的话将在一个特殊的模式下运行lint。它只能找出致命的错误(节省时间),如果发现致命的错误就会终止导出APK.】
  • Edit and Save an XML file, such as a layout file or a manifest file. In this case, all the file-scope checks that apply to the given file are run and editor markers are added for any issues found
    编辑和保存XML文件,lint也会自动扫描这些文件。另外从ADT20开始,对于java源码文件在编辑和保存后,lint也会对他们进行扫描。
    【编辑和保存XML文件(例如layout文件或者manifest文件),这样的话, 所有与之关联的文件都会重新检查。】
  • Use the layout editor. After every UI operation, file-scope checks (such as the various layoutopt rules) are run on the layout file and the results are shown in a special lint window (which can be opened from the error marker which shows in the top right corner of the layout editor when errors are found).
    对于使用layout editor来操作布局文件时,在每个UI操作后,lint也会自动扫描该布局文件。
    【用layout编辑器. 对每一个布局操作, 所有与之关联的文件都会重新检查。】
3.2、Lint Window
在Eclipse中,你可以通过两种方式来手动进行lint的扫描:
一种方式是通过工具栏,点击工具栏lint 按钮    的下拉箭头,然后在下拉框中选择要进行lint扫描的工程
另一种方式是选中一个Android工程,单击右键,在下拉菜单中选择“Android tools”->"Run lint:check common erro"
运行lint之后,将显示一个 包含有各种错误的  Lint Warrings窗口

错误信息中会给出具体的描述、类别、位置等,选择一个错误将会在右边的区域显示与之关联的解释


lint窗口的工具栏上有一些按钮,如下图所示
它们的意义分别如下
  • Android Lint简介 - hubingforever - 民主与科学 Refresh, which re-runs the current analysis[分析] on the same projects
  • Android Lint简介 - hubingforever - 民主与科学  Fix, which automatically fixes the issue (this applies to issues where a quickfix is available)
  • Android Lint简介 - hubingforever - 民主与科学Suppress[忽略] this issue with an attribute or annotation[属性或注释]
  • Android Lint简介 - hubingforever - 民主与科学 Ignore in this file (saves suppress information in lint.xml)
  • Android Lint简介 - hubingforever - 民主与科学 Ignore in this project (ditto[同上])
  • Android Lint简介 - hubingforever - 民主与科学  Always ignore
  • Android Lint简介 - hubingforever - 民主与科学 Delete this lint marker
  • Android Lint简介 - hubingforever - 民主与科学  Delete all lint markers
  •     Expand All, 
  • Android Lint简介 - hubingforever - 民主与科学    Collapse All
  • Android Lint简介 - hubingforever - 民主与科学 Configure Columns[栏]     设置在lint Window中对于检查出的issue的哪些项显示哪些项不显示
  • Android Lint简介 - hubingforever - 民主与科学 Edit Options     定制默认/全局的检查规则把检查级别[Severity]设为”ignore“,其实就是忽略[suppress]该检查规则

 3.3、Quick Fixes

Many lint warnings have automatic fixes. For example, the various layoutopt fixes suggest replacements (e.g. replace wrap_content with 0dp). 
许多lint警告有自动修复功能。例如,不同的layoutopt修正建议替换(例如:更换wrap_content与0DP)
  • From the lint view, click the lightbulb Android Lint简介 - hubingforever - 民主与科学to invoke a fix.【从lint视图,点击灯泡修复
  • From the layout editor warning summary, click the Fix button to fix.【从布局编辑器警告摘要,单击“修复”按钮来修复
  • And from the XML source editor, invoke the Quick Fix and pick the quick fix associated with the warning.【从XML源代码编辑器中,调用快速修复(按Ctrl-1),并选择快速修复警告
3.4、Suppressing Errors( 抑制错误)
From the editor quick fix menu, you can also choose to
从编辑器的快速修复菜单,您也可以选择
  • Ignore the warning in this file only【仅仅在这个文件中忽略警告
  • Ignore the warning in this project【在这个项目中忽略警告
  • Ignore the warning, period.【忽略警告,周期
  • Ignore warnings using annotations or attributes.【使用注释或属性忽略警告
 
3.5、Configure Columns
设置在lint Window中对于检查出的issue的哪些项显示哪些项不显示,如下图 所示


3.6、Edit Options
选中一个问题,可以在右下角的下拉列表中选择 检查级别 。这使您可以设计特定的问题作为一个错误。
检查级别可以是:
  • Default
  • Fatal
  • Errro
  • Waring
  • Information
  • Ingore(即不检查)
需要完全的关闭一个问题,选择“忽略”。需要注意的是,这并不编辑每个项目的Lint设置。


          项目右键-->properties--> 打开项目设置对话框,点击Android Lint的属性页。在“选项”对话框手动编辑严重程度伴随抑制错误将被记录在lint.xml文件,并且你的团队的其他人将获得相同的诊断配置。

          These choices are stored in a file named  lint.xml  in the project, which is also read by the command line tool. Thus, you can ignore warnings from the UI, and check in the  lint.xml  file with your source projects, and others running lint will not see warnings you have ignored (presumably because they have been manually verified).    

如果你只是想对lint的检查规则做些简单的定制,请参考《Android Lint 检查规则的定制(基本篇)》或英文官方文档
如果你想对lint的检查规则做些高级的定制,请参考官方文档 Writing New Lint Checks and Writing Custom Lint Rules


四、常见错误类型[Category]

1, Correctness:Messeges

  • (1)字符串国际化不完全
  • (2)国际化的字符串,在默认位置(default locale),没有定义  

2,Correctness

  • (1)Xml中view的id重名
  • (2)代码中使用的某些API高于Manifest中的Min SDK
  • (3)字符串国际化中,同一名字的的String-Array对应的item值不相同 (4)Activity没有注册到Manifest
  • (5)使用已经废弃的api
  • (6)避免使用px,使用dp
  • (7)添加不需要的权限

3,Performance

  • (1) 避免在绘制或者解析布局(draw/layout)时,分配对象。eg,Ondraw()中实例化Paint().
  • (2)Layout中无用的参数。
  • (3)可优化的布局:如一个线性布局(一个Imageview和一个TextView),可被TextView和一个Compound Drawable代替。
  • (4)可优化的代码:如SparseArray可代替一个Interger2Object的Hashmap
  • (5)优化layout,比如如果子view都是wrap_content,则设置android:baselineAligned为false,则When set to false, prevents the layout from aligning its children's baselines.
  • (6)使用FloatMath代替Math,执行sin()和ceil(),以避免float的两次转换。
  • (7)Nested weight (内外均有weight)将拖累执行效果
  • (8)未被使用的资源
  • (9)Overdraw 即指定theme的activity会自己绘制背景,但是布局中会再一次设置背景
  • (10)View或view的父亲没有用

4,Security

  • (1)设置setJavascriptEnable将导致脚本攻击漏洞(XSS vulnerabilities)

5,Usability:Icons

  • (1) 图片尺寸在转换成不同dpi时,存在不能整除的问题,比如2*24px
  • (2)显示有些base 和browser的资源名不同,但图片内容完全相同。

6,Usability

  • (1)自定义view缺少默认的构造方法

7,Usability:Typography

  • (1)特殊字符需用编码代替,如“_”需要用“–”

8,Accessibility

  • (1)ImageView缺少src内容






转载于:https://www.cnblogs.com/baiqiantao/p/5334243.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值