英文原文:http://tools.android.com/tips/lint 参照文章:http://blog.csdn.net/thl789/article/details/8037473
转载地址:http://blog.csdn.net/hudashi/article/details/8333349
New Lint Checks and Writing
Custom Lint Rules.
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)
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
^
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 [Har
dcodedText]
android:text="打印所有任务栈信息"
^
res\layout\activity_main.xml:29:Warning:[I18N]Hardcodedstring"打印所有服务信息", should use@string resource [Hardc
odedText]
android:text="打印所有服务信息"
^
res\layout\activity_main.xml:35:Warning:[I18N]Hardcodedstring"打印进程信息", should use@string resource [Hardcoded
Text]
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
$ lint --disable MissingTranslation,UnusedIds,Usability:Icons/src/astrid/
$ lint --disable MissingTranslation,UnusedIds,Usability:Icons/src/astrid/
$
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 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.
Lint 检查规则列表》来查阅检查项的id等详细信息
$ lint --html /tmp/report.html --url /src/MyProj=http://buildserver/src/MyProj
- 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视图能直接跳转到其问题对应的源码处
- 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也会自动扫描该布局文件。
-
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)该检查规则
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:
The Edit Options actions brings up the Lint Preference dialog, which
has also been improved. You can now search through the options by
filter:
3.3、Quick Fixes
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.
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.
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).
lint.xml
文件中