Android Lint简介(转载)

英文原文:http://tools.android.com/tips/lint  参照文章:http://blog.csdn.net/thl789/article/details/8037473

转载地址:http://blog.csdn.net/hudashi/article/details/8333349

一、简介

Android Lint是SDK
Tools 16 (ADT 16)之后才引入的工具,通过它对Android工程源代码进行扫描和检查,可发现潜在的问题,以便程序员及早修正这个问题。Android
Lint提供了命令行方式执行,还可与IDE(如Eclipse)集成,并提供了html形式的输出报告。

由于Android
Lint在最初设计时就考虑到了independent于IDE,所以它可以很方便的与项目中的其他自动系统(配置/ Build /
测试等)集成.

Android
Lint主要用于检查以下这些错误:

1、Missing translations (and unused translations)没有翻译的文本

2、Layout performance problems (all the issues the old layoutopt tool used
to find, and more)

3、Unused resources未使用的冗余资源

4、Inconsistent array sizes (when arrays are defined in multiple
configurations)在多个配置中的数组大小不一致文件

5、Accessibility and internationalization problems (hardcoded strings,
missing contentDescription, etc)

6、Icon problems (like missing densities, duplicate icons, wrong sizes,
etc)

7、Usability problems (like not specifying an input type on a text
field)

8、Manifest errors

当然Android
Lint远远不至检查以上的错误,更多的内容请参考《Android Lint 检查规则列表

在Eclipse中可以在菜单Window->Preference->“Lint Eerro checking”中设置规则的检查级别,如图1所示。

检查级别可以是:

Default

Fatal

Errro

Waring

Information

Ingore(即不检查)

图1

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

如果你想对lint的检查规则做些高级的定制,请参考官方文档 Writing New Lint Checks and Writing
Custom Lint Rules
.

二、命令行中使用Lint

2.1、基本使用

There is a command line tool in the SDK tools/ directory called lint.
If
you have the SDK tools/ directory
on your path, you can invoke it as “lint”.
Just point to a specific Android project directory. You can also point to a
random directory, which (if it is not an Android project) will be searched
recursively and all projects under that directory will be checked. (And you can
also specify multiple projects separated by spaces)

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

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

示例1

linux命令行

lint /src/astrid/

ScanningGreenDroid-GoogleAPIs:..

Scanning stream:...

Scanning api:...........................

ScanningGDCatalog:.......................

ScanningGreenDroid:...........................................................

Scanning tests:...

Scanning filters:....

Scanning tests:.....

Scanning astrid:....................................................................................................................................................

Scanning simple:.......

api/res/values-ca:Error:Locale ca is missing translations for: sync_SPr_bgwifi_key, sync_SPr_forget_key, sync_SPr_interval_values,
sync_SPr_logged_in_prefix...(2 more)[MissingTranslation]

astrid/res/values-ca:Error:Locale ca is missing translations for: DLG_cancel, DLG_dismiss, DLG_ok,EPr_deactivated...(117 more)[MissingTranslation]

api/res/values-cs:Error:Locale cs is missing translations for: sync_SPr_bgwifi_key, sync_SPr_forget_key, sync_SPr_interval_values,
sync_SPr_logged_in_prefix...(2 more)[MissingTranslation]

(many
lines omitted)

43
errors,466
warnings

示例2

Window命令行

C:\Documents andSettings\Administrator>lint D:\workspace\TestScanningTest:.........................................................................................................
........................................................................................................................
...................
ScanningTest(Phase2):......res\layout\internet_image_demo.xml:9:Warning:The id "button1"isnot referring to any views inthis layout [UnknownIdInLayout] android:layout_alignLeft="@+id/button1"^
res\layout\internet_image_demo.xml:10:Warning:The id "textView1"isnot referring to any views inthis layout [UnknownIdInLayout] android:layout_below="@+id/textView1"^
AndroidManifest.xml:52:Warning:Exported receiver does notrequire permission [ExportedReceiver]
<receiver android:name=".AlarmReceiver">
^res\menu\activity_main.xml:Warning:The resource R.menu.activity_main appears to be unused
[UnusedResources]
res\drawable-hdpi\ic_action_search.png:Warning:The resource R.drawable.ic_action_search appears to be unused
[UnusedResources]res\values\strings.xml:7:Warning:The resource R.string.hello appears to be unused [UnusedResources]
<string name="hello">你好!</string>^res\drawable-mdpi:Warning:Missing the following drawables
in
drawable-mdpi: icon.png, icon2.png [IconDensities]
res\drawable-xhdpi:Warning:Missing the following drawables
in
drawable-xhdpi: icon.png, icon2.png [IconDensities]
res\layout\internet_image_demo.xml:5:Warning:[Accessibility]Missing
contentDescription attribute on image [ContentDescription]<ImageView^res\layout\activity_main.xml:17:Warning:[I18N]Hardcodedstring"go
Hello", should use@string resource [HardcodedText]
android:text="go
Hello"^res\layout\activity_main.xml:23:Warning:[I18N]Hardcodedstring"打印所有任务栈信息", should use@string resource [HardcodedText] android:text="打印所有任务栈信息"^res\layout\activity_main.xml:29:Warning:[I18N]Hardcodedstring"打印所有服务信息", should use@string resource [HardcodedText] android:text="打印所有服务信息"^res\layout\activity_main.xml:35:Warning:[I18N]Hardcodedstring"打印进程信息", should use@string resource [HardcodedText] android:text="打印进程信息"^res\layout\hello.xml:23:Warning:[I18N]Hardcodedstring"please click me", should use@string resource [HardcodedText] android:text="please
click me"^0
errors,14
warnings

2.2、Disabling Checks(--disable

在执行lint命令时可以通过--disable选项要指定关闭的检查规则项。--disable选项后接要关闭的检查规则项的id(比如示例3中的MissingTranslation)或检查规则项的类别(比如示例3中的Usability:Icons和示例4的Internationalization)。

关于lint检查项的id和类别(Category)等信息请参考《Android Lint 检查规则列表

示例3

$ lint --disable MissingTranslation,UnusedIds,Usability:Icons/src/astrid/

示例4

C:\Documents andSettings\Administrator>lint --disable Internationalization D:\workspace\Test

ScanningTest:.........................................................................................................

........................................................................................................................

...................

ScanningTest(Phase2):......

res\layout\internet_image_demo.xml:9:Warning:The id "button1"isnot referring to any views inthis layout [UnknownIdI

nLayout]

android:layout_alignLeft="@+id/button1"

^

res\layout\internet_image_demo.xml:10:Warning:The id "textView1"isnot referring to any views inthis layout [Unknown

IdInLayout]

android:layout_below="@+id/textView1"

^

AndroidManifest.xml:52:Warning:Exported receiver does notrequire permission [ExportedReceiver]

<receiver android:name=".AlarmReceiver">

^

res\menu\activity_main.xml:Warning:The resource R.menu.activity_main appears to be unused
[UnusedResources]

res\drawable-hdpi\ic_action_search.png:Warning:The resource R.drawable.ic_action_search appears to be unused
[UnusedRe

sources]

res\values\strings.xml:7:Warning:The resource R.string.hello appears to be unused [UnusedResources]

<string name="hello">你好!</string>

^

res\drawable-mdpi:Warning:Missing the following drawables
in
drawable-mdpi: icon.png, icon2.png [IconDensities]

res\drawable-xhdpi:Warning:Missing the following drawables
in
drawable-xhdpi: icon.png, icon2.png [IconDensities]

res\layout\internet_image_demo.xml:5:Warning:[Accessibility]Missing
contentDescription attribute on image [ContentDes

cription]

<ImageView

^

0
errors,9
warnings

2.3、enabling Checks(--enable和--check)

lint的有些检查项默认是关闭的(disable),在执行lint命令时可以通过--enable选项开启它。-enable选项后接要开启的检查规则项的id(比如示例5中的MissingTranslation )或检查规则项的类别(示例5中的Usability:Icons)

示例5

$ lint --disable MissingTranslation,UnusedIds,Usability:Icons/src/astrid/

在执行lint命令时可以通过--check选项来指定只进行某些检查。-check选项后接要开启的检查规则项的id(比如示例6中的MissingPrefix)

示例6

$
lint --check MissingPrefix /src/astrid/

2.4、检查项类别和检查项id

可以通过lint的--list选项来得到检查项类别和检查项id.

比如:

lint --list

Valid
issue categories:


Correctness

Security

Performance

Usability

Usability:Icons

Accessibility

Internationalization



Valid
issue id‘s:


"ContentDescription":
Ensures that image widgets provide a contentDescription


"DuplicateIds":
Checks for duplicate ids within a single layout


"StateListReachable":
Looks for unreachable states in a <selector>


"InefficientWeight":
Looks for inefficient weight declarations in LinearLayouts


"ScrollViewSize":
Checks that ScrollViews use wrap_content in scrolling dimension


"MergeRootFrame":
Checks whether a root <FrameLayout> can be replaced with a <merge>
tag


...

可以通过lint的--show选项后跟检查项id来得到一个检查项的详细说明.

比如:

$
lint --show MissingPrefix


MissingPrefix

-------------

Summary:
Detect XML attributes not using the Android namespace




Priority: 8 /
10


Severity:
Warning


Category:
Correctness




Most
Android views have attributes in the Android namespace. When


referencing
these attributes you *must* include the namespace prefix,


or your
attribute will be interpreted by aapt as just a custom


attribute.

当然你也可以通过《Android Lint 检查规则列表》来查阅检查项的id等详细信息

2.4、html形式的report

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

示例6

C:\Documents andSettings\Administrator>lint --html D:\workspace\Test\report.htm

l D:\workspace\Test

ScanningTest:.................................................................

................................................................................

................................................................................

...................

ScanningTest(Phase2):......

Wrote
HTML report to D:\workspace\Test\report.html

html输出报告如图2所示

图2

By
default, links to source files will just use local file:// path resources. You
can remap the URLs to a different prefix with the --url option. For
example:

$ lint --html
/tmp/report.html --url
/src/MyProj=http://buildserver/src/MyProj

2.5、命令行帮助

在lint中,你可以使用--help选项来得到lint命令的一些帮助信息。

示例7

lint
--help

三、Eclispe中使用Lint

从ADT16开始,lint就集成到了ADT中。该它在lint命令行的基础上新增了以下功能

  • Automatic fixes for many warnings自动修正大量警告

  • Lint gets run automatically on various editing operations当编辑操作完成后,立即自动运行

  • Ability to suppress types of errors as well as specific instances of an
    error可以suppress(忽略)一种类型的erro,也可以suppress(忽略)特定的一个erro

  • Ability to configure issue severities能够配置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会做快速的扫描,以寻找fatal的错误。如果发现有fatal的错误,导出APK的操作将被迫终止

  • Edit and Save and 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也会对他们进行
    扫描。

  • 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也会自动扫描该布局文件。

3.2、Lint Window

在Eclipse中,你可以通过两种方式来手动进行lint的扫描:

一种方式是通过工具栏,双击图3-1中红色箭头指向的按钮,然后出现图3-2所示的下拉框,在该下拉选择要进行lint扫描的工程

图3-1

图3-2

一种方式是选中一个Android工程,单击右键,在下拉菜单中选择“Android
tools”->"Run lint:check common erro",如图3-3所示

图3-3

运行lint之后,你将看到如图3-4图3-5所示的lint 窗口

图3-4

图3-5

默认情况下,同一个类型的issue都是折叠成一块的,图3-4和图3-5是我手动展开的。

lint窗口的工具栏上有一些按钮,如图3-6所示

图3-6

它们的意义分别如下

  •  Refresh, which re-runs the current analysis on the
    same projects

  •  Fix, which automatically fixes the issue (this
    applies to issues where a quickfix is available)

  •  Suppress this issue with an attribute or
    annotation

  •  Ignore in this file (saves suppress information in
    lint.xml)

  •  Ignore in this project (ditto)

  •  Always ignore

  •  Delete this lint marker

  •  Delete all lint markers

  •  Expand All,

     Collapse All


  •   Configure Columns用于设置在lint
    Window中对于检查出的issue的哪些项显示哪些项不显示,如图3-7所示


  •  Edit Options Edit
    Options 点击该按钮会弹出Lint
    Preference dialog,如图1或图3-8在里面你可以定制默认/全局的Android
    Lint的基本检查规则,在其中可以设置所有项目默认的lint检查规则的检查级别,把检查级别(Severity)设为”ignore“,其实就是忽略(suppress)该检查规则

Configure
Columns lets you edit which columns are visible. There are several new columns
you can display, such as Category, Priority, etc, and you can click on column
headers to sort the display by the given column. There‘s also a new "Location"
column, shown by default, which incorporates several different pieces of
information: the file name, the line number, the parent folder name (useful when
looking at translation or configuration issues), and the project
name:

Configure
Columns用于设置在lint Window中对于检查出的issue的哪些项显示哪些项不显示,如图3-7所示

图3-7


 
  The Edit Options actions
brings up the Lint Preference dialog, which has also been improved.  You
can now search through the options by filter:

点击Edit Options按钮会弹出处理Lint Preference
dialog,在里面可以定制默认/全局的Android Lint的基本检查规则

图3-8


 
 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). 

  • From the lint view(如图3-6所示), click the lightbulb 
      to invoke a fix.

  • From the layout editor warning summary, click the Fix button to fix.

  • And from the XML source editor, invoke the Quick Fix (Ctrl-1 or Command-1) and
    pick the quick fix associated with the warning.

3.4、Suppressing Errors(检查规则的基本定制)

From
the editor quick fix menu(如图3-9所示),
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, as explained here.

图3-9

(If
you do not see the lint fix action in the quickfix list, see the Known Bugs
section)



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 thelint.xml file
with your source projects, and others running lint will not see warnings you
have ignored (presumably because they have been manually
verified).

你的选择在被存在Android工程目录下的 lint.xml 文件中

关于此的详细内容请参考《Android Lint 检查规则的定制(基本篇)

结束!

时间: 2024-11-04 06:29:59

Android Lint简介(转载)的相关文章

Android Lint简介(转)

转载自原文:http://blog.csdn.net/hudashi/article/details/8333349,感谢原作者. 英文原文:http://tools.android.com/tips/lint 参照文章:http://blog.csdn.net/thl789/article/details/8037473 一.简介 Android Lint是SDK Tools 16 (ADT 16)之后才引入的工具,通过它对Android工程源代码进行扫描和检查,可发现潜在的问题,以便程序员及

Android Lint简介

Android Lint是SDK Tools 16 (ADT 16)之后才引入的工具,通过它对Android工程源代码进行扫描和检查,可发现潜在的问题,以便程序员及早修正这个问题.Android Lint提供了命令行方式执行,还可与IDE(如Eclipse)集成,并提供了html形式的输出报告. 选中一个Android工程,单击右键,在下拉菜单中选择“Android tools”->"Run lint:check common errors" 然后我们就能得到错误报告了 这里我们

【工利其器】Android Lint篇——为Android量身定做的静态代码审查工具

前言 我们在进行代码优化的时候,往往是通过开发者的经验来判断哪些代码可能存在潜在问题,哪些资源的使用不合规范等.实际上Android SDK提供了一款功能非常强大的工具,来帮助开发者自动检测代码的质量及安全问题,这款工具就叫Android Lint.Lint提供了命令行方式以及与IDE集成的方式来执行,现在Android开发者基本上都使用Android Studio(后文简称AS,下同)来进行开发,强大的AS也集成了该工具,本文就基于AS3.2版本来介绍Lint. 一.Android Lint简

Android提供的LruCache类简介[转载]

转自:here 1 package android.util; 2 3 import java.util.LinkedHashMap; 4 import java.util.Map; 5 6 /** 7 * A cache that holds strong references to a limited number of values. Each time 8 * a value is accessed, it is moved to the head of a queue. When a

【Android应用开发】Android Studio 简介 (Android Studio Overview)

一. Intelij IDEA 环境简介 Android Studio 来源 : Android Studio 是 Intelij IDEA 的免费版本 + Android SDK 集成的; -- Intelij 文档参考 : https://www.jetbrains.com/idea/help/intellij-idea.html ; 1. Intelij 与 Eclipse 区别 (1) Intelij 与 Eclipse 对应关系 Android Studio 与 Eclipse 对应

android官方技术文档翻译——Android Lint

本文译自androd官方技术文档<Android Lint>,原文地址:http://tools.android.com/tips/lint. 本文地址:http://blog.csdn.net/maosidiaoxian/article/details/41592783.转载请注明出处.翻译如有错讹,敬请指正. Android Lint Android Lint是在ADT 16(和 Tools 16)引入的一个新工具,可以扫描Android 项目源码中潜在的bug .它可同时作为一个命令行工

Android插件简介

/** * @actor Stafen.D * @time 2015.02.06 * @blog http://www.cnblogs.com/stafen */ Android插件简介 Android下,默认的情况是,每个apk相互独立的,基本上每个应用都是一个dalvik虚拟机,都有一个uid,再配合上linux本身的权限机制,使得apk互通很难直接进行.但作为一个独立应用的集成,不管多少个apk,都可以并为一个单独的dalvik虚拟机,直观的反映给开发人员就是在shell下列出进程,那几个

Android lint 删除无用图片文件和配置文件

Android lint  删除无用.冗余的  配置文件和 图片资源 转载请注明  http://blog.csdn.net/aaawqqq?viewmode=contents Android项目经过长期的迭代开发  项目当中有大量无用的java类和冗余图片 如果不整理将会导致 apk 包比较大 审查 清理Java类  使用UCDetector  可以查看我的上篇 博文 http://blog.csdn.net/aaawqqq/article/details/46684441 Android l

Eclipse出现&quot;Running Android Lint has encountered a problem&quot;解决方案

最近打开Eclipse的时候,总是发生这样的一个错误:"Running Android Lint has encountered a problem".截图如下: . 但是Eclipse可以正常运行程序,不会造成其他影响.但是每次打开Eclipse时,总是看到这个警告,心情会很不爽,具体解决方案如下: (1)打开Preferences,在Windows下面应该在WIndow-->Preferences.在mac下在Eclipse-->偏好设置.选择Android-->