13.4 Android Debugging with lint

Hope you learned lot of useful information regarding debugging. Today we are going to continue sessions with debugging as it is very important to understand the framework of debugging. We use them unknowingly so let us know them a little more so that we do not run into issues when circumstances are not in under control. Let us continue the session with this sub-section.

13.4.1 Introduction

Lint is a code scanning tool. It is provided by Android SDK. Now, there is a need for such a scanning tool as structure or code is very important in certain scenarios. Reliability and efficiency of android app is influenced by the structure of code. It could be very difficult at a certain point of time when structure of code has loopholes. Using lint can maintain the quality of code as it is easy to identify and rectify problems. The best part is you need not execute or write any test case in order to accomplish this task. This tool identifies the problem and publishes a problem description message along with the threat on security level. This in turn helps us to prioritize primary and secondary requirements of code. It can be integrated with the automatic testing process as it has a command-line interface. Lint can be run from eclipse or from command-line interface. lint tool is automatically installed when we talk about Android SDK Tools revision 16 or higher. When we talk about Eclipse we have to install Android Development Tools (ADT) Plugin for Eclipse revision 16 or higher.

13.4.2 Workflow

Let us have an overview on the workflow of this tool.

Figure workflow of lint

Let us have a brief idea regarding these jargons:

  • App source files: These are the source files which our project consists of. They include java files, xml files, icons, etc.
  • Lint.xml file: This is a configuration file. We can use it to specify lint checks which you might want to remove or configuring and customizing the security level.
  • Lint tool: This is the code scanning tool which we can run on our android project no matter from where you run it (Eclipse or command-line interface) The structural code errors are detected by this tool so that quality and security level of code is maintained. Structural code examples could be using API calls that are not supported by the target API versions or the XML resource file that contain unused namespaces which in turn consumes space or unnecessary code processing, etc.

Lint tool’s results can be viewed in console. It can be viewed in Eclipse as Lint Warnings. The concern is identified by the location of problem inside source files accompanied by description of concern in form of an error message. 

The lint tool runs automatically when we run it from Eclipse when any one of the following task is accomplished and they are as follows:

  1. When xml file is edited and saved in project like manifest or resource file
  2. Apk file is exported. lint automatically runs to keep a check on fatal errors and export is terminated if fatal errors are discovered. It can be turned off using Lint error checking page. 
  3. Layout editor is used in Eclipse to make changes

Following path is used to view lint warnings on EclipseWindow> Show View > Other > Lint Warnings.

FigureWindow > Show View > Other

Figure Lint Warnings

Figure Example of Lint Warnings

When we talk about command line interface, lint tool can be run against a list of files in project directory. The command usage is as follows:

lint [flags] <project_directory>

We canissue the following command to scan the files under the my_project directory and its subdirectories and the issue ID MissingPrefix asks lint to scan the xml attributes which are missing in Android namespace prefix. The command looks similar to following syntax:

lint --check MissingPrefixmy_project.

We can see the full listing of flags and command line arguments which are supported by lint. The command is as follows:

lint–help

Figure snapshot of lint tool (command line)

We can customize the restriction on issues for lint to check and assign the severity level for those issues. Lint checking can be configured at different levels of security which include globally (all projects), particular project, particular file and particular java class or method. We can specify our lint checking preferences in the lint.xml file. This fil should be placed in the root directory of our android project. When we use eclipse, configuring the lint preferences creates an lint.xml and is added to corresponding Android project automatically.

<?xml version="1.0" encoding="UTF-8"?>
    <lint>
        <!—configuration list of issues -->
</lint>

Figure lint.xml

Congratulations ladies and gentlemen!! We are done with this sub-section. Looking forward to share another useful sub-section in terms of debugging, we hope you learned something from this section. See you in the next section. Till then keep practicing and wish you Happy App Developing!!!