[Work] Android Lint its profits article - tailor-made for Android static code review tool

Foreword

       When we conduct code optimization, often through experience developer to determine what code may be a potential problem, the use of which resources and other irregularities. In fact Android SDK provides a very powerful tool to help developers automatically detect the quality and safety code, this tool called Android Lint. Lint provides a command-line and integrated with the IDE are executed by Android developers now basically using Android Studio (hereinafter referred to as AS, the same below) to develop, powerful AS also integrates the tools, paper-based AS3.2 version to introduce Lint.

 

A, Android Lint Profile

       Android Lint is the SDK Tools 16 (ADT16, ADT currently I use is already 26, so now do not have to worry about the SDK they are using does not contain lint tool a) code scanning tool begins the introduction, the path is: Sdk \ tools \ bin \ lint.bat. Through its Android project source code scanning and inspection, you can identify potential problems early so that programmers fix this problem. By Lint, without actually executing the application, do not write test cases, you can check the Android project source file is stored potential errors, and whether correctness, security, performance, ease of use, accessibility and internationalization We need to optimize improvements.

 

Two, Lint works

       Lint will use, you can simply look at how it works, that works. Lint android working process is relatively simple, based on a process by the Lint Lint Tool (detection means), Source Files (program source) and lint.xml (profile) of three parts, read Lint Tool Source Files, The lint (Issue) configuration .xml output rules (see below).

 

Three, Lint use in the AS

       Since Lint is now directly integrated into the AS, it can be used directly, without having to re-install.

  1, Lint start

       Lint use the steps in Android Studio is very simple, there are two ways to start Lint:

    (1) Start from the main menu bar

       Click on the main menu> Analyze> Inspect Code selection dialog box will pop up to check range

    

The dialog box is used to check the range selection, selected according to actual needs. Wherein the check range can customize the selected "Custom scope",

After choosing the check range, click on "OK", Lint began to start the bottom of the screen will indicate AS is running, wait a minute you can see the test results.

    (2) Start menu by right

       In the editing interface or AS interface file, right-click on the pop-up menu you can also see the "Analyze> Inspect Code" option, and back on the same way as above, and will not repeat them here.

 2, view test results

       After the inspection is finished, the interface plate may see the following:

 

In this panel, "Lint test results zone" show is to check out the results by Lint tool. When you click on the problem entry of these results, you can see this kind of problem or details of specific issues on the right, and then automatically modify or manually modify the description of the problem.

  3. Results panel features

        These results panel from left to right can be divided into three large areas: the toolbar, the results area and details of the problem area.

     (1) Toolbar

       The toolbar provides a number of commonly used functions, it will give a prompt when the mouse over the icon, on the part of the button is described here:

       1) at ① in FIG, Group By Serverity: packet according to the check result extent of the problem.

       2) at ② in FIG, Group By Directory: checking result grouped by directories.

       3) at ③ in FIG, Filter resolved items: filtering out the questionnaire item has been resolved.

       4) Figure ④ Department, Export: export to HTML or XML documents.

       5) the drawing ⑤, Edit Settings: edit the settings can be set by Lint herein, will be detailed later.

       6)图中⑥处,Apply a quickfix:快速修复,点击这里可以直接对所选问题进行快速修复。

    (2)检查结果区

       由于这里检查代码不仅仅只有Lint在工作,还有很多其它插件也在执行,所以结果中有很多其它工具检查出来的问题,Android > Lint下面的目录就是Lint的结果区了。如果展开Lint的结果,会发现这些问题都是Android所特有的问题,比如布局文件、控件、AndroidManifest文件中的问题,可见Lint就是为Android而量身定做的。默认时这些结果是按照一定的分类来分组的,比如,Correctness、Performance、Security等,这问题类型是否需要检查,也是在设置中设置的,后面会详细介绍。当然通过这个结果区还可以看到Java、Kotlin、Spelling等的问题,也一样解决。

    (3)问题详情区

       如果鼠标在结果区选择的是某一类问题,这部分显示了所选问题的详情,解决建议,以及快速修复等功能。其中“Run inspection on”按钮,点击后会弹出如下对话框:

这里选择在哪个范围内检查同类问题。

       如果在结果区选择的是具体的问题项,那么右边显示的就是对问题所在的位置进行预览。如下图所示:

       通过上面的介绍,可以看到Lint的基本使用还是比较简单的。

 

四、对Lint进行设置

  1、进入设置界面的两种方式

       有两种途径可以进入到Lint的设置界面:

    (1)File > Settings > Editor > Inspections;

    (2)Lint控制面板工具栏的设置按钮,即第二节中的按钮⑤。

  2、设置面板预览

 3、设置项功能介绍

       设置界面中的各个区域和按钮的功能都在图中标注了,这里再对按钮①和按钮②进行说明。

    (1)筛选按钮   

       这是一个下拉框,通过该处可以筛选要显示的功能项。

    (2) 问题严重等级设置

  

下拉列表,会显示可以自定义的等级及对应颜色,可以通过此处编辑选择项的严重等级。点击“Edit severities”可以添加新的严重等级,以及编辑各等级德颜色和字体。当然,系统默认的问题严重性等级和这里略有区别,系统默认的严重等级由高到低依次为:Fatal、Error、Warning、Information、Ignore 5个等级。

 

五、Lint问题种类

       从设置面板中可以看到,问题可以分为如下几个大类:

    (1)Accessibility 辅助选项,比如ImageView的contentDescription往往建议在属性中定义等。

    (2)Compliance 合规性,违反了Google Play的要求,比如使用了过期的库版本,性能、安全性、API等级等没有遵循新系统的要求等。

    (3)Correctness 不够完美的编码,比如硬编码、使用过时API等。

    (4)Internationalization 国际化,直接使用汉字,没有使用资源引用等

    (5)Interoperability 互操作性,比如和Kotln的交互等。

    (6)Performanc 对性能有影响的编码,比如:静态引用,循环引用等

    (7)Security 不安全的编码,比如在 WebView 中允许使用 JavaScriptInterface等

    (8)Usability 可用的,有更好的替换的 比如排版、图标格式建议.png格式等

 

六、根据规则名称来检查

       有时候我们需要明确检查某一类问题,比如查找项目中没有使用的资源,并将它们删除,它们会耗性能。可以通过如下的方式来找到这些没有使用的资源:点击Analyze > Run Inspection By Name,会弹出对话框

输入该规则的名称,按“Enter”键,提示选择检查范围:

点击“OK”按钮,结果中只会显示“Unused resoures”的问题项目:

该功能能帮我们明确定位某一类问题。

 

七、自定义Lint

       对于多数情况,默认的Lint已经足够使用了,但总有一些需求是Lint自带规则不能满足的,这个时候就需要自定义Lint规则了。当然,限于笔者对Liint的使用经验有限,对自定义Lint还不太熟悉,这里推荐一篇文章,有兴趣的可以深入研究:

自定义Lint实践https://blog.csdn.net/ouyang_peng/article/details/80374867

 

参考:

       https://blog.csdn.net/luzhenyuxfcy/article/details/79398761

       http://tools.android.com/tips/lint

       https://developer.android.google.cn/studio/write/lint

       https://blog.csdn.net/hudashi/article/details/8333349

       https://www.jianshu.com/p/9eaf7c0eee67

 

结语

       本文只介绍了Android Lint常见的一些使用方法,一定还有很多很牛的功能,希望读者们多多探索,多多交流,共同进步,谢谢!

Guess you like

Origin www.cnblogs.com/andy-songwei/p/11832280.html