Android代码优化---Android Lint

英文原文:http://tools.android.com/tips/lint

原文:Android Lint is a new tool introduced in ADT 16 (and Tools 16) which scans Android project sources for potential bugs. It is available both as a command line tool, as well as integrated with Eclipse (described below), and IntelliJ (details). The architecture is deliberately IDE independent so it will hopefully be integrated with other IDEs, with other build tools and with continuous integration systems as well.


译文:Android Lint是在ADT16提供的工具。旨在扫描Android工程源码潜在bug的扫描。它支持命令行操作,同时与Eclipse和IntelliJ均完美整合. 它的结构特意设计成与IDE独立,这样它有希望跟其他IDE,其他编译工具,其他自动化系统。


原文:Here are some examples of the types of errors that it looks for:

  • Missing translations (and unused translations)
  • 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)
  • Icon problems (like missing densities, duplicate icons, wrong sizes, etc)
  • Usability problems (like not specifying an input type on a text field)
  • Manifest errors

译文:以下是Android Lint所扫描的错误类型:

  • 遗漏的翻译(没用上的翻译)---这里应该是指values那堆international的values文件夹
  • UI布局上的错误(可探测出所有layoutopt可以检测到的错误,而且会更多)
  • 未使用的资源
  • 不一致的array大小 (若arrays在多个config中被定义)
  • 可访问性和国际化问题 (硬编码字符串,缺少contentDescription,诸如此类)
  • 图标问题 (如缺少densities定义,重复icon,错误尺寸,诸如此类)
  • 使用类型问题 (如没在Text Field上定义inputType)
  • 清单错误

原文:and many more.

    §  See this document for a full list of the currentchecks performed by lint.

    §  For information on how to suppressspecific lint warnings, see the suppressing warnings document.

    §  If you're interestedin writing custom lint checks, see Writing New Lint Checks and Writing Custom Lint Rules.


译文:更多的:

      参见详细的当前Lint所有检查项说明文档(http://tools.android.com/tips/lint-checks)

      参见屏蔽警告文档(http://tools.android.com/tips/lint/suppressing-lint-warnings)以屏蔽部分特殊的lint警告

      若要设置自定义的lint检测,见文档编写新的警告检测(http://tools.android.com/tips/lint/writing-a-lint-check)和编写自定义Lint规则(http://tools.android.com/tips/lint-custom-rules)


Command Line Usage

原文:There is a command line tool in the SDKtools/ directory called lint.

If you have theSDK tools/ directory on yourpath, you can invoke it as “lint”. Just point to a specific Androidproject directory. You can also point to a random directory, which (if it isnot an Android project) will be searched recursively and all projects underthat directory will be checked. (And you can also specify multiple projectsseparated by spaces).

 

译文:SDK中的tools目录下有个lint的命令行工具。如果你有这个文件夹,那你可以调用lint。只要指向一个指令android工程文件夹就行。也可以直接指向任意文件夹,它会递归检测所有在那个目录下的工程。你也可以用空格分隔多个工程。

$ lint /src/astrid/
Scanning GreenDroid-GoogleAPIs: ..
Scanning stream: ...
Scanning api: ...........................
Scanning GDCatalog: .......................
Scanning GreenDroid: ...........................................................
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


Disabling Checks

文:The “id” for each type of error is shown in bracket after the error message, such as“MissingTranslation” above. 
译文:每个错误类型的id会展示在每个错误信息之后的括弧中。如上面lint出来的错误信息 “MissingTranslation”。---见上面的信息
原文:You can disable a specific check, or a list of checks, byadding the --disable argument, e.g.

译文:你可以通过添加—disable参数禁止一些特定的检测。

$ lint --check MissingPrefix /src/astrid/


原文:To find out which id’s andcategories areavailable, run 

译文:查询哪些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
...


原文:And to get the explanationfor a specific issue usethe --show command along with a list of id’s or categories (or noarguments at all to see everything):

译文:通过—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.


HTML Reports

原文:The command line tool can also generate HTML reports. This has someadvantages over the plain lint output:

  • It includes the longer explanations associated with each issue, and hyperlinks toMore Info for issues which provide a more info attribute.
  • It includes the actual source code line with the error (and a window of 3 lines around it).
  • It can contain links to the associated source files
  • For icon errors, the icons themselves are shown in the report for comparison purposes

译文:lint这个命令行工具也可以生成HTML报告。这相对单一的lint输出有一些优势:

  • 包含了相关错误的更详细的解释,和一个可以提供更详细解释的链接地址.
  • 包含了出错的代码(有3行在里头).
  • 提供了直接进入出错代码的相关文件的连接
  • 对于icon错误,icons会直接被展示在报告中

原文:To generate an HTML report, just add --html filename as an argument:

译文:增加—html文件名生成一个HTML报告:

$ lint --html /tmp/report.html

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

译文:可以通过通过添加 --url HTML 报告默认链接到源工程文件。
$ lint --html /tmp/report.html --url /src/MyProj=http://buildserver/src/MyProj

Other Command Line Options

原文:Run lint --help to get information on the available arguments.

译文:运行lint –help获取更多的参数信息

Eclipse Usage

原文:NOTE:This document was written when lintwas first released. The Eclipseintegration has since been improvedsignificantly. For more up to date details, see New Eclipse Lint UI,

Layout EditorLint Feedback and Lint On Save.

译文:注意:这个文档是lint刚发布的时候编写的。现在eclipse已经有了显著的升级。查看更多信息请查看New Eclipse Lint UI,

Layout Editor Lint Feedback and Lint On Save.

 

原文:Lint is integrated with ADT 16 and later. The integration offers a fewfeatures above the command line version of 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
  • Ability to configure issue severities
  • Jump directly to the problem source from the lint view

译文:lint已经跟ADT16甚至更新的版本整合。整合提供了命令行模式下的lint更多的功能

  • 添加多种警告的自动修复
  • Lint自动运行在多种编辑模式
  • 能够屏蔽指定错误类型的检测
  • 能够严谨地配置错误
  • 可以通过lint视图直接跳转到问题源码
AutomaticLint

原文:Lint will be run automaticallywhen you:

  • 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.
  • 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.
  • Usethe layout editor. After every UI operation, file-scope checks (such as the variouslayoutopt rules) are run on the layout file and the results are shown in aspecial lint window (which can be opened from the error marker which shows inthe top right corner of the layout editor when errors are found).

译文:当你进行以下操作的时候会自动运行lint

  • 导出apk。在这种情况下,Lint会自动运行在一个特殊的模式:只会寻找fatal错误(这更快)和若找到fatal错误就取消导出apk。你可以在Lint配置中关闭这个功能.
  • 编辑和保存XML文件时,比如layout文件和manifest文件. 在这种情况下,所有的文件审查将会运行同时标记所有找到的错误.
  • 使用layout编辑器. 在任何UI操作之后,文件检查器会检测(比如多种layoutopt规则)所修改的layout文件,所有的结果会在展示在制定的窗口中(当错误被找到时可以通过右上角的layout编辑器打开).

Lint Window

原文:To run Lint on a project,select the project in the package explorer and click on the Lint toolbar action(see image on the right).

译文:要运行lint,在package管理器窗内选中project,点击Lint按钮(如图)

原文:Alternatively you can right click on the project and in the Android Tools sub menu, there is a “Run Lint” action. This will open a Lint Window which contains the various errors. Selecting an error will show the associated explanation in the text area on the right.
译文:要不然你可以右键点击project,在android tools的子菜单中,有一个Run Lint的按钮。这会打开一个包括了多种错误信息的Lint窗口。选中一个错误后会在右侧打开一个关联的管理器窗口。


原文:You can double click on errors in the lint view to jump to the associated source location, if any. The icon of each warning will indicate its severity, and for warnings which have fixes (see next section) there is a small light bulb overlay.

译文:你双击在Lint窗口中的错误会跳转到相关联的源地址。点图标的警告展示图标本身。对那些可以修复的警告会有一个小亮灯在警告上面。


原文:The window also has an action bar (in the upper right hand side corner), which lets you

  • Run the lint checks again to refresh the results (which turns into a Stop button during the refresh if you want to abort it)
  • Run the fix associated with this error, if any
  • Ignore this fix
  • Remove the warning marker
  • Remove all warning markers

译文:Lint窗口也有一个在操作栏(在窗口上部右角),这允许你做这样几件事

  • 再次运行Lint检测并刷新结果 (在刷新期间按钮会变成Stop按钮,你也可以停止)
  • 运行Error修复
  • 忽略这个修复提示
  • 移除警告标记
  • 移除所有的警告标记
QuickFixes

原文:Many lint warnings haveautomatic fixes. For example, the various layoutopt fixes suggest replacements(e.g. replace wrap_content with 0dp). 

  • From the lint view, click the lightbulb to invoke a fix.
  • From the layout editor warning summary, click the Fix button to fix.
  • Andfrom the XML source editor, invoke the Quick Fix (Ctrl-1 or Command-1) and pickthe quick fix associated with the warning.
译文:许多的Lint警告信息都有自动修复。比如,多种layoutopt修复建议(如0dp替换wrap_content
  • 在Lint界面上点击小亮灯修复.
  • 从Layout编辑器的警告信息上,点击Fix按钮去修复.
  • 在XML编辑器中,用快捷键(Ctrl-1或者Command-1)选择一个跟这个警告相关的快速修复.
SuppressingErrors
原文: From the editor quick fixmenu, 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.

译文:在快速修复菜单中,你还可以选择

  • 忽略在当前文件中的警告
  • 忽略当前project中的警告
  • 始终忽略这个警告.
  • 用annotations或attributes忽略警告, as explained here.
原文:(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).

译文:(如果你没有在快速修复列表中,看到lint fix action,查看 Known Bugs 部分)
这里的选择都保存在工程目录下 lint.xml 文件中,这也可以在命令行工具下读取。而且你可以忽略这些UI上警告,并检测在源工程中的 lint.xml,同时其他的Lint工具也不会检测到你所忽略的警告。

Options

原文:To edit the global lint options, which are the fallback options used for all projects, invoke the normal Eclipse options dialog and choose Android > Lint Options:
译文:编辑全局Lint选项

原文:If you select an issue, you can edit its Severity in the dropdown on the bottom right. This lets you designate certain issues as Errors for example (which will abort Export APK if the option near the top of the dialog is enabled).

To turn off an issue check completely, choose “Ignore” as the severity.

Note that this does not edit the per-project lint settings. Open the Project settings dialog (right click on the project), where you’ll find a property page for Android Lint. Manual severity edits in the options dialog are also recorded in the lint.xml file along with suppressed errors, so others in your team will get the same severity designations you edit for the project.

译文:若你选择了一个问题,你可以在右下下拉菜单中编辑它。这允许你编译 明确的问题(比如中止导出apk如果在弹窗顶部的option被勾选)
如果要完全关闭问题检测,可以选择Ignore
注意---这不会对在此之前的工程的lint设置。打开project 设置框(右击这个project), 你会找到一个恰当的Lint页面。在设置窗中,手动编辑的设置同样会保存在 lint.xml中,这包含了你忽略的错误信息,这样其他你队伍中的同事也会得到你给工程编辑的设置信息


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值