Android 性能优化(1)性能工具之「 lint 」 :Improving Your Code with lint:优化代码
Improving Your Code with lint
1.See Also
In addition to testing that your Android application 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.
2.Overview
The Android SDK 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.
Note: In Android Studio, additional IntelliJ code inspections run when your code is compiled in Android Studio to streamline code review.
Figure 1 shows how the lint tool processes the application source files.

Figure 1. Code scanning workflow with the lint tool
- 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.
- The
lint.xmlfile - A configuration file that you can use to specify any
lintchecks that you want to exclude and to customize problem severity levels. - The
linttool - A static code scanning tool that you can run on your Android project from the command-line or Android Studio. The
linttool 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 thatlintdetects before publishing your application. - Results of
lintchecking - You can view the results from
lintin the console or in the Event Log in Android Studio. Each issue is identified by the location in the source files where it occurred and a description of the issue.
The lint tool is automatically installed as part of the Android SDK Tools revision 16 or higher.
3.Running lint in Android Studio
In Android Studio, the configured lint and IDE inspections run automatically whenever you build your app. The IDE inspections are configured along with the lint checks to run IntelliJ code inspections to streamline code review.
Note: To view and modify inspection severity levels, use the File > Settings > Project Settings menu to open the Inspection Configuration page with a list of the supported inspections.
With Android Studio, you can also run lint inspections for a specific build variant, or for all build variants from the build.gradle file. Add the lintOptions property to the android settings in the build file. This code snippet from a Gradle build file shows how to set the quiet option to true and the abortOnError option to false.
在AS中如何运行Lint工具
android {
lintOptions {
// set to true to turn off analysis progress reporting by lint
quiet true
// if true, stop the gradle build if errors are found
abortOnError false
// if true, only report errors
ignoreWarnings true
}
...
}
To manually run inspections in Android Studio, from the application or right-click menu, choose Analyze > Inspect Code. The Specify Inspections Scope dialog appears so you can specify the desired inspection scope and profile.
4.Running lint from the Command-Line
To run lint against a list of files in a project directory:
lint [flags] <project directory>
For example, you can issue the following command to scan the files under the myproject directory and its subdirectories. The issue ID MissingPrefix tells lint to only scan for XML attributes that are missing the Android namespace prefix.
lint --check MissingPrefix myproject
To see the full list of flags and command-line arguments supported by the tool:
lint --help
Example lint output
The following example shows the console output when the lint command is run against a project called Earthquake.
$ lint Earthquake Scanning Earthquake: ...............................................................................................................................
Scanning Earthquake (Phase 2): .......
AndroidManifest.xml:23: Warning: <uses-sdk> tag appears after <application> tag [ManifestOrder]
<uses-sdk android:minSdkVersion="7" />
^
AndroidManifest.xml:23: Warning: <uses-sdk> tag should specify a target API level (the highest verified version; when running on later versions, compatibility behaviors may be enabled) with android:targetSdkVersion="?" [UsesMinSdkAttributes]
<uses-sdk android:minSdkVersion="7" />
^
res/layout/preferences.xml: Warning: The resource R.layout.preferences appears to be unused [UnusedResources]
res: Warning: Missing density variation folders in res: drawable-xhdpi [IconMissingDensityFolder]
0 errors, 4 warnings
The output above lists four warnings and no errors in this project. Three warnings (ManifestOrder,UsesMinSdkAttributes, and UnusedResources) were found in the project's AndroidManifest.xml file. The remaining warning (IconMissingDensityFolder) was found in the Preferences.xml layout file.
5.Configuring lint
By default, when you run a lint scan, the tool checks for all issues that are supported by lint. 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 configure lint to report non-critical issues at a lower severity level.
You can configure lint checking at different levels:
Lint 扫描选项配置等级:
- Globally, for the entire project
- Per project module
- Per production module
- Per test module
- Per open files
- Per class hierarchy
- Per Version Control System (VCS) scopes
5.1 Configuring lint in Android Studio (在AS中配置Lint扫描选项)
Android Studio allows you to enable or disable individual inspections and configure project-global, directory-specific, and file-specific settings for lint.
You can manage inspection profiles and configure inspection severity within Android Studio using the File > Settings > Project Settings menu to open the Inspections page with a list of the supported profiles and inspections.
配置页面在: File > Settings > Editor > Inspections > Android Lint

Figure 3. Inspection Configuration
也可以扫描项目后,然后针对某个选项进行配置, 在Inspection 页面 > 右键某个选项 > Eidt Settings 就可以弹出针对该选项的设置,如图:

Global preferences (扫描整个项目)
- To specify global project settings, select the project folder in the Project View and choose Analyze > Inspect Code.
- Specify your inspection scope and profile, and click OK.
The configured settings run the specified lint inspections. The lint inspections are also run whenever you build and run your Android project and modules.
Module and file-specific preferences (扫描某模块或文件)
- Run the
linttool on your module by right-clicking on your module folder or file in the Project View and selecting Analyze > Inspect Code. This displays thelintinspection results with a list of issues thatlintdetected in your module. - From the Lint Warnings view, use the toolbar options to configure
lintpreferences for individual modules and files, and set the issue display options.
5.2 Configuring the lint file (用lint.xml配置扫描选项)
You can specify your lint checking preferences in the lint.xml file. If you are creating this file manually, place it in the root directory of your Android project. If you are configuring lint preferences in Android Studio, the lint.xml file is automatically created and added to your Android project for you.
The lint.xml file consists of an enclosing <lint> parent tag that contains one or more children <issue> elements. Each <issue> is identified by a unique id attribute value, which is defined by lint.
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<!-- list of issues to configure -->
</lint>
By setting the severity attribute value in the <issue> tag, you can disable lint checking for an issue or change the severity level for an issue.
Tip: To see the full list of issues supported by the lint tool and their corresponding issue IDs, run the lint --list command.
可以通过 命令 $lint --list 查看配置选项
Sample lint.xml file (lint.xml示例)
The following example shows the contents of a lint.xml file.
<?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>
5.3 Configuring lint checking in Java and XML source files (如何忽略 .java 和 .xml 等源文件中的lint提示)
You can disable lint checking from your Java and XML source files.
下面是在AS中的配置方法:
Tip: If you are using Android Studio, you can use the File > Settings > Project Settings > Inspections feature to manage the lint checking to your Java or XML source files.
Configuring lint checking in Java (在.java文件中忽略lint提示的方法:用 @SuppressLint(XXX) )
To disable lint checking specifically for a Java class or method in your Android project, add the @SuppressLint annotation to that Java code.
The following example shows how you can turn off lint checking for the NewApi issue in the onCreate method. Thelint tool continues to check for the NewApi issue in other methods of this class.
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
The following example shows how to turn off lint checking for the ParserError issue in the FeedProvider class:
@SuppressLint("ParserError")
public class FeedProvider extends ContentProvider {
To suppress checking for all lint issues in the Java file, use the all keyword, like this:
@SuppressLint("all")
Configuring lint checking in XML (在.xml文件中忽略lint提示,用:tools:ignore="XXX,XXX")
You can use the tools:ignore attribute to disable lint checking for specific sections of your XML files. In order for this attribute to be recognized by the lint tool, the following namespace value must be included in your XML file:
namespace xmlns:tools="http://schemas.android.com/tools"
The following example shows how you can turn off lint checking for the UnusedResources issue for the <LinearLayout> element of an XML layout file. The ignore attribute is inherited by the children elements of the parent element in which the attribute is declared. In this example, the lint check is also disabled for the child <TextView> element.
子元素会 继承 父元素的 tools:ignore
<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>
To disable more than one issue, list the issues to disable in a comma-separated string. For example:
tools:ignore="NewApi,StringFormatInvalid"
To suppress checking for all lint issues in the XML element, use the all keyword, like this:
tools:ignore="all"
Android 性能优化(1)性能工具之「 lint 」 :Improving Your Code with lint:优化代码的更多相关文章
- Android 性能优化(25)*性能工具之「Systrace」Analyzing UI Performance with Systrace:用Systrace得到ui性能报告
Analyzing UI Performance with Systrace In this document Overview 简介 Generating a Trace 生成Systrace文件 ...
- Android内存管理(12)*「实例」用Monitor 生成.hprof文件 并分析内存泄漏
参考 http://blog.csdn.net/xiaanming/article/details/42396507 基本步骤: 1,准备一个有内存泄漏的代码 2,如何发现内存泄漏 3,生成.hpro ...
- LG5202 「USACO2019JAN」Redistricting 动态规划+堆/单调队列优化
问题描述 LG5202 题解 \[opt[i]=xx+(cnt[i]-cnt[yy]<=0)\] 发现\(cnt[i]-cnt[yy] <= 0\)只能有两种取值 于是直接堆优化即可 \( ...
- Android 性能优化(2)性能工具之「Hierarchy Viewer 」Optimizing Your UI:分析哪个view有性能问题,查看屏幕上某像素点的坐标,颜色等
Optimizing Your UI In this document Using Hierarchy Viewer Running Hierarchy Viewer and choosing a w ...
- 「2014-3-17」C pointer again …
记录一个比较基础的东东-- C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...
- 「题解」「HNOI2013」切糕
文章目录 「题解」「HNOI2013」切糕 题目描述 思路分析及代码 题目分析 题解及代码 「题解」「HNOI2013」切糕 题目描述 点这里 思路分析及代码 题目分析 这道题的题目可以说得上是史上最 ...
- Android开发学习之路--性能优化之常用工具
android性能优化相关的开发工具有很多很多种,这里对如下六个工具做个简单的使用介绍,主要有Android开发者选项,分析具体耗时的Trace view,布局复杂度工具Hierarchy Vie ...
- Android APP性能分析方法及工具
近期读到<Speed up your app>一文.这是一篇关于Android APP性能分析.优化的文章.在这篇文章中,作者介绍他的APP分析优化规则.使用的工具和方法.我觉得值得大家借 ...
- Android比较实用的性能优化
Android设备作为一种移动设备,无论是内存还是CPU的性能都受到了很大的限制,这导致Android程序的性能问题异常突出,随着产品的不断更新迭代,对于性能优化提出了更高的要求.本篇文章从稳定性.流 ...
随机推荐
- [NOIP2007] 提高组 洛谷P1005 矩阵取数游戏
题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次后取完矩阵所有元素: 2. ...
- ***CSS总结-原创
CSS规则 第一个是:选择符.和jquery的选择器是一个意思大括号是:申明块里面KV是:属性和属性值分号是结束符 类别选择符(器) p.red的意思,就是对p标签中类为red的进行CSS作用 ID选 ...
- [bzoj3489]A simple rmq problem_KD-Tree
A simple rmq problem 题目大意:给定一个长度为$n$的序列,给出$m$个询问:在$[l,r]$之间找到一个在这个区间里只出现过一次的最大的数. 注释:$1\le n\le 10^5 ...
- HTTPS 是如何保证安全的?
每当我们讨论到信息安全的时候,我们最长接触到的信息加密传输的方式莫过于 HTTPS 了,当我们浏览器地址栏闪现出绿色时,就代表着这个网站支持 HTTPS 的加密信息传输方式,并且你与它的连接确实被加密 ...
- SHARP 加粉
http://v.ku6.com/show/7ufwm7pqfF8D_f13IdCaag...html
- Android应用程序安装过程浅析
我们知道在android中.安装应用是由PackageManager来管理的,可是我们发现PackageManager是一个抽象类.他的installPackage方法也没有详细的实现. 那在安装过程 ...
- 【c语言】统计一个数字在排序数组中出现的次数
// 题目:统计一个数字在排序数组中出现的次数. // 比如:排序数组{1.2,3,3,3,3,4.5}和数字3,因为3出现了4次.因此输出4 有一种最简单的算法,遍历.可是有比它效率更高的 先看遍 ...
- 单一责任原则(SRP)
1.就一个类而言,应该仅有一个引起它变化的原因. 2.在SRP中定义职责为:“变化的原因”. 如果你想到多个动机去改变这个类,那这个类就有多个职责
- 阿里云CentOS7.3搭建多用户私有git服务器(从安装git开始)
起因 自己会有练手的不敢公开的项目,就自己搭建个服务器放自己的渣代码了. 在经历了连不上服务器.没有访问权限.没法提交以后,我打通了任督二脉. 我这个git服务器适合条件:1.就那么几个人小项目,不是 ...
- ZOJ问题(2010浙江大学研究生复试上机题目[找规律] hdoj 3788)
ZOJ问题 pid=3788">点击打开链接 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...