Android Studio lint

Improve Your Code with Lint 用lint 工具增强你的代码

In addition to testing your Android application to ensure that it meets its functional requirements, it’s important to ensure that your code has no structural problems. Poorly structured code can impact the reliability and efficiency of your Android apps and make your code harder to maintain. For example, if your XML resource files contain unused namespaces, this takes up space and incurs unnecessary processing. Other structural issues, such as use of deprecated elements or API calls that are not supported by the target API versions, might lead to code failing to run correctly.
除了要测试你的安卓应用能够保证基本的功能,更重要的是要确保你的代码没有结构性的问题,不好的架构代码能影响安卓应用的可靠性和性能,而且很难修复你的代码,举个例子,如果你的xml 资源包含了用不到的命名空间,他会占用空间和侵占不必要的进程资源,其他的架构问题,例如用过时的元素或者是调用的该版本不在支持的API 会导致运行失败。
Overview(概述)
android Studio provides a code scanning tool called lint that can help you to identify and correct problems with the structural quality of your code without your having to execute the app or write 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. Also, you can lower the severity level of a problem to ignore issues that are not relevant to your project, or raise the severity level to highlight specific problems.

Android studio 提供了代码扫描工具叫lint,他可以帮助你来确定和修正框架代码没有执行的代码和写的测试用例的问题,工具发现的每一个问题会用消息详情和一个严重级别报告出来,所以你可以快速的优先考虑需要改造的地方,也可以降低问题的级别来忽略跟项目不太相关的问题,或者提高指定问题的严重级别。

The lint tool checks your Android project source files for potential bugs and optimization improvements for correctness, security, performance, usability, accessibility, and internationalization. When using Android Studio, configured lint and IDE inspections run whenever you build your app. However, you can manually run inspections or run lint from the command line.

Lint工具能够检测android 工程的资源文件潜在的bug,并且优化提高正确性、安全性、性能,可用性,易用性和国际化。当你用android studio,配置lint工具每当构建app时,IDE 就会运行检查,但是,你可以手动的运行检查或者用命令行来运行lint
Note: When your code is compiled in Android Studio, additional IntelliJ code inspections run to streamline code review.

这里写图片描述

Code scanning workflow with the lint tool
上图 lint工具代码扫描流程

Application source files
The source files consist of files that make up your Android project, including Java and XML files, icons, and ProGuard configuration files.
资源文件包含构成android工程的文件。包含java和xml文件,icons文件,ProGuard 配置文件

The lint.xml file lint.xml文件
A configuration file that you can use to specify any lint checks that you want to exclude and to customize problem severity levels.
lint.xml配置文件是用来指定你想排除不想检测的和自定义严重级别的一个文件。

The lint tool Lint 工具
A static code scanning tool that you can run on your Android project either from the command line or in Android Studio (see Manually Run Inspections). The lint tool checks for structural code problems that could affect the quality and performance of your Android application. It is strongly recommended that you correct any errors that lint detects before publishing your application.

一个静态代码扫面工具你可以运行在你的安卓工程,可以用命令行也可以用Android studio,这个工具能够检测到影响你安卓应用的质量和性能的代码结构问题,强烈建议你在发布应用前修复lint检测出的问题。

命令行
Lint –help 查看lint所有支持的命令行 在android studio 左下角terminal 里面运行,但是你要配置 lint 的环境变量,更多的可以百度一下lint命令行

The issue ID MissingPrefix tells lint to only scan for XML attributes that are missing the Android namespace prefix.
运行命令 lint –check MissingPrefix app

Configure lint
By default when you run a lint scan, the tool checks for all issues that lint supports. You can also restrict the issues for lint to check and assign the severity level for those issues. For example, you can disable lint checking for specific issues that are not relevant to your project and, you can configure lint to report non-critical issues at a lower severity level.
默认情况下当你运行lint’扫描工具,这个工具会检查所有lint 支持的问题,你也可以控制管理问题的严重级别,举个例子,你可以让lint不检查你指定的问题和你可以配置无关紧要的问题设置成一个比较低的严重级别。
You can configure lint checking for different levels:
Globally (entire project)
Project module
Production module
Test module
Open files
Class hierarchy
Version Control System (VCS) scopes

Configure lint in Android Studio 在AndroidStudio 中配置lint
The built-in lint tool checks your code while you’re using Android Studio. You can view warnings and errors in two ways:
(1)As pop-up text in the Code Editor. When lint finds a problem, it highlights the problematic code in yellow, or for more serious issues, it underlines the code in red.
(2)In the lint Inspection Results window after you click Analyze > Inspect Code. See Manually Run Inspections.

两种方式查看lint 检查结果 第一种会在编辑器上面弹出来,当lint 发现问题的时候,他用黄色高亮代码,或者是在代码下面标识红线
Configuring lint checking in Java
在代码中忽略lint 检查在

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

在xml文件中忽略lint检查
声明命名空间

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>

The Android plugin for Gradle allows you to configure certain lint options, such as which checks to run or ignore, using the lintOptions {} block in your module-level build.gradle file. The following code snippet shows you some of the properties you can configure:
还可以在module级别的build.gradle中配置lint

android {
  ...
  lintOptions {
    // Turns off checks for the issue IDs you specify.
    disable 'TypographyFractions','TypographyQuotes'
    // Turns on checks for the issue IDs you specify. These checks are in
    // addition to the default lint checks.
    enable 'RtlHardcoded','RtlCompat', 'RtlEnabled'
    // To enable checks for only a subset of issue IDs and ignore all others,
    // list the issue IDs with the 'check' property instead. This property overrides
    // any issue IDs you enable or disable using the properties above.
    check 'NewApi', 'InlinedApi'
    // If set to true, turns off analysis progress reporting by lint.
    quiet true
    // if set to true (default), stops the build if errors are found.
    abortOnError false
    // if true, only report errors.
    ignoreWarnings true
  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值