Lint学习使用

Lint google 简介

Android Studio provides a code scanning tool called Lint that can help you to easily identify and correct problems with the structural quality of your code, without having to execute the app or write any test cases. Each problem detected by the tool is reported with a description message and a severity level, so that you can quickly prioritize the critical improvements that need to be made. You can also configure a problem's severity level to ignore issues that are not relevant for your project, or raise the severity level. The tool has a command-line interface, so you can easily integrate it into your automated testing process.
The Lint tool checks your Android project source files for potential bugs and optimization improvements for correctness, security, performance, usability, accessibility, and internationalization. You can run Lint from the command-line or from Android Studio.

以上内容总结翻译就是:

总结起来就是 Lint是一个帮助识别代码结构质量的工具并可以描述信息和严重程度,当然可以对严重级别等指标配置。
最后一段很关键:Lint工具检查您的Andr​​oid项目源文件的正确性,安全性,性能,可用性,可访问性和国际化潜在的bug和优化改进。您可以通过命令行或Android Studio中运行lint。

enter description here

1468993982318.jpg

上面lint.xml file 这个就是带有设置检查规则的配置文件

PS:Lint是在SDK TOOLS 版本 16或者更高的才有的

下面就是使用了

命令行:

配置好环境变量或者以控制台在SDK tools文件夹下运行

命令行运行

>lint

enter description here

1468994899962.jpg

如果配置对了 会出现版本号 这个配置很简单网上随便一搜就好了 (配置环境变量)

首先查看都有什么功能 当然开发的都知道help这个指令 我们就用help指令看看该怎么用

enter description here

1468996215524.jpg

总结几个常需要的:

检查时用到的
1.--list List the available issue id's and exit.说白了就是检查的项
2.--show List available issues along with full explanations. 就是检查的项+完整的解释
3.--show Show full explanations for the given list of issue id's. 指定检查项看解释
4.--disable Disable the list of categories or specific issue id's. The list should be a comma-separated list of issue id's or categories 禁用某些检查项
5.--enable Enable the specific list of issues. This checks all the default issues plus the specifically enabled issues. The list should be a comma-separated list of issue id's or categories. 启用某些检查项
6.--check Only check the specific list of issues. This will disable everything and re-enable the given list of issues. The list should be a comma-separated list of issue id's or categories. 以指定检查项检查


结果如果用命令行窗口看能看死人这里lint支持把结果输出到HTML或者XML 等
7.--html Create an HTML report instead. If the filename is a directory (or a new filename without an extension), lint will create a separate report for each scanned project. 创建一个html的报表
8.--xml Create an XML report instead. 创建一个XML报表

当然还有像 --simplehtml 这种指令不过没啥大用 简单报表 也不差那点空间 还是详细的好
具体指令去看help 里面多的很 想了解的 自己去看下就能会 相信你肯定轻松学会 ,这里就一抛砖引玉

比如我使用html来生成报表 生成的是这样
enter description here
点进第一个
enter description here
这只是一部分 后面还有代码的(也就是可能错误的代码 )

这上面讲的 怎么使用代码进行Lint 在Android Studio编译器里是自带了lint的下面讲讲怎么使用Android Studio来Lint

 Analyze > Inspect Code.

检查完后会在下面的窗口展示出检查结果
enter description here
如果想编辑lint的规则

File > Other Settings > Default Settings>Editor > Inspections.

enter description here

1469968412314.jpg

这里就可以编辑lint规则了

最后说下lint规则的文件设置
lint的规则是卸载lint.xml中的

<?xml version="1.0" encoding="UTF-8"?>
<lint>
    <!-- Disable the given check in this project -->
    <issue id="IconMissingDensityFolder" severity="ignore" />

    <!-- Ignore the ObsoleteLayoutParam issue in the specified files -->
    <issue id="ObsoleteLayoutParam">
        <ignore path="res/layout/activation.xml" />
        <ignore path="res/layout-xlarge/activation.xml" />
    </issue>

    <!-- Ignore the UselessLeaf issue in the specified file -->
    <issue id="UselessLeaf">
        <ignore path="res/layout/main.xml" />
    </issue>

    <!-- Change the severity of hardcoded strings to "error" -->
    <issue id="HardcodedText" severity="error" />
</lint>

用代码检查java或者Xml源文件


@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


@SuppressLint("ParserError")
public class FeedProvider extends ContentProvider {



@SuppressLint("all")

namespace xmlns:tools="http://schemas.android.com/tools"

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="UnusedResources" >

    <TextView
        android:text="@string/auto_update_prompt" />
</LinearLayout>


tools:ignore="NewApi,StringFormatInvalid"

tools:ignore="all"

当然还有很多代码lint 这里就不多说了 要想知道怎么用点下面(得翻墙)
https://developer.android.com/studio/write/annotations.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值