From 961e06ec4864a89bf150abe38e48c3bf9c8a459c Mon Sep 17 00:00:00 2001
From: Cheryl Potter
-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.
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 {@code 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 {@code lint} from the command-line or from Android Studio.
+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 {@code 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 {@code lint} from the command-line or from Android Studio.
To run {@code 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 {@code myproject} directory and its subdirectories. The issue ID MissingPrefix tells {@code lint} to only scan for XML attributes that are missing the Android namespace prefix.
For example, you can issue the following command to scan the files under the {@code myproject}
+directory and its subdirectories. The issue ID MissingPrefix tells {@code 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
The following example shows the console output when the {@code lint} command is run against a project called Earthquake.
+The following example shows the console output when the {@code lint} command is run against a +project called Earthquake.
$ lint Earthquake @@ -124,10 +147,17 @@ res/layout/preferences.xml: Warning: The resource R.layout.preferences appears t 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 ({@code ManifestOrder}, {@code UsesMinSdkAttributes}, and {@code UsesMinSdkAttributes}) were found in the project's AndroidManifest.xml file. The remaining warning ({@code IconMissingDensityFolder}) was found in the Preferences.xml layout file.
The output above lists four warnings and no errors in this project. Three warnings
+({@code ManifestOrder}, {@code UsesMinSdkAttributes}, and {@code UnusedResources}) were found in
+the project's AndroidManifest.xml file. The remaining warning
+({@code IconMissingDensityFolder}) was found in the Preferences.xml layout file.
By default, when you run a {@code lint} scan, the tool checks for all issues that are supported by {@code lint}. You can also restrict the issues for {@code lint} to check and assign the severity level for those issues. For example, you can disable {@code lint} checking for specific issues that are not relevant to your project and configure {@code lint} to report non-critical issues at a lower severity level.
+By default, when you run a {@code lint} scan, the tool checks for all issues that are supported +by {@code lint}. You can also restrict the issues for {@code lint} to check and assign the severity +level for those issues. For example, you can disable {@code lint} checking for specific issues that +are not relevant to your project and configure {@code lint} to report non-critical issues at a lower +severity level.
You can configure {@code lint} checking at different levels:
You can specify your {@code 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 {@code 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 {@code lint}.
You can specify your {@code 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 {@code 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 {@code 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 {@code lint} checking for an issue or change the severity level for an issue.
Tip: To see the full list of issues supported by the {@code lint} tool and their corresponding issue IDs, run the lint --list command.
By setting the severity attribute value in the <issue> tag, you can disable
+{@code lint} checking for an issue or change the severity level for an issue.
Tip: To see the full list of issues supported by the {@code lint}
+tool and their corresponding issue IDs, run the lint --list command.
The following example shows the contents of a lint.xml file.
To disable {@code 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 {@code lint} checking for the {@code NewApi} issue in the onCreate method. The {@code lint} tool continues to check for the {@code NewApi} issue in other methods of this class.
To disable {@code 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 {@code lint} checking for the {@code NewApi}
+issue in the onCreate method. The {@code lint} tool continues to check for the
+{@code NewApi} issue in other methods of this class.
@SuppressLint("NewApi")
@Override
@@ -221,22 +264,30 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
-The following example shows how to turn off {@code lint} checking for the {@code ParserError} issue in the FeedProvider class:
The following example shows how to turn off {@code lint} checking for the {@code ParserError}
+issue in the FeedProvider class:
@SuppressLint("ParserError")
public class FeedProvider extends ContentProvider {
-To suppress checking for all {@code lint} issues in the Java file, use the {@code all} keyword, like this:
+To suppress checking for all {@code lint} issues in the Java file, use the {@code all} keyword, +like this:
@SuppressLint("all")
You can use the tools:ignore attribute to disable {@code lint} checking for specific sections of your XML files. In order for this attribute to be recognized by the {@code lint} tool, the following namespace value must be included in your XML file:
You can use the tools:ignore attribute to disable {@code lint} checking for specific
+sections of your XML files. In order for this attribute to be recognized by the {@code 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 {@code lint} checking for the {@code 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 {@code lint} check is also disabled for the child <TextView> element.
The following example shows how you can turn off {@code lint} checking for the
+{@code 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 {@code lint} check is also disabled for the
+child <TextView> element.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
@@ -247,11 +298,13 @@ namespace xmlns:tools="http://schemas.android.com/tools"
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:
+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 {@code lint} issues in the XML element, use the {@code all} keyword, like this:
+To suppress checking for all {@code lint} issues in the XML element, use the {@code all} keyword, +like this:
tools:ignore="all"