Android lint优化 Improving Your Code with lint

源地址:http://developer.android.com/tools/debugging/improving-w-lint.html

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.
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.xml file
A configuration file that you can use to specify any lint checks that you want to exclude and to customize problem severity levels.
The lint tool
A static code scanning tool that you can run on your Android project from the command-line or Android Studio. 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.
Results of lint checking
You can view the results from lint in 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.
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.

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.
Running lint from the Command-Line

To run lint against a list of files in a project directory:

lint [flags]

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: tag appears after tag [ManifestOrder]

^
AndroidManifest.xml:23: Warning: 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]

^
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 UsesMinSdkAttributes) were found in the project’s AndroidManifest.xml file. The remaining warning (IconMissingDensityFolder) was found in the Preferences.xml layout file.
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:

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

Configuring lint in Android Studio

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.

Figure 3. Inspection Configuration
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 lint tool on your module by right-clicking on your module folder or file in the Project View and selecting Analyze > Inspect Code. This displays the lint inspection results with a list of issues that lint detected in your module.
From the Lint Warnings view, use the toolbar options to configure lint preferences for individual modules and files, and set the issue display options.

Configuring the lint file

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 parent tag that contains one or more children elements. Each is identified by a unique id attribute value, which is defined by lint.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值