13.5 Other debugging concepts in Android

Hey guys!! Hope you are doing well. Today we shall conclude debugging with this sub-section. We shall have a brief overview on some other debugging concepts about which you should be aware of. Let us start exploring them without wasting any more time.

13.5.1 Hierarchy Viewer

When our layout files slow down we make use of lint and Hierarchy Viewer. We are already aware of lint, so we shall continue with Hierarchy Viewer. Hierarchy Viewer application is responsible for optimizing and debugging the user interface. A visual representation is displayed which comprises information regarding the performance. It can be magnified so that an enlarged view can be displayed where we can have a close eye on pixels of our layout. The Hierarchy Viewer is reached using the tool hierarchyviewer. After running the application in an attached emulator or device, we can launch the hierarchyviewer from <sdk>/tools directory. This is going to displays a list of Android devices which are currently connected to the development machine. From that list, when a particular device is selected, a complete list of running applications is displayed. Hence we can easily select a device which we intend to debug or optimize user performance. We have four important aspects of this viewer and they are as follows:

  • Tree View: This displays a sort of hierarchy tree of views corresponding to the object of Activity. We can use this opportunity to examine individual View objects. We can see the relationship between View objects in user interface.
  • Tree Overview: It represents a smaller map representation of the entire Tree View. We can easily identify the section of the view tree which is being displayed in Tree View using Tree Overview.
  • Properties View: As the name signifies, it displays the list of properties for a selected View object. Properties are organized according to the category. By expanding the category name we can easily examine a particular property.
  • Layout View: This is a sort of block representation. We can navigate through this facility. When we click on a particular area the View object corresponding to that area is highlighted in Tree View.

Layout View or the LoadView Hierarchy can be selected to see the View Tree which is constructed by the Hierarchy Viewer when a particular file is selected. The View Hierarchy window is responsible for displaying the View objects which form the User Interface of Activity. This Activity is the one which is running on the connected device or emulator. There is a limitation on the View Tree that each application can generate. The depth of the tree cannot be deeper than 10 and the width of the tree cannot be wider than 50.

Pixel Perfect is a tool for examining the pixel properties of user interface. The screen which is displayed on the emulator or connected device can be magnified using this window. It in turn helps in examining the properties of individual pixel in screen. To view Pixel perfect window, we need to click on Inspect Screenshot after running Hierarchy Viewer. This has three important aspects and they are as follows:

  • View Object Pane: The View objects which are currently visible on device or emulator is listed as a hierarchical list. According to the View class, the objects are listed.
  • Pixel Perfect Loupe Pane: This magnified image is overlaid by a grid. Each square represents a pixel.
  • Pixel perfect Pane: This displays the screen which is currently visible on emulator.

13.5.2 Trace View

TraceView is a graphical viewer. TraceView is a tool to optimize performance. Debug class has to be implemented in application so that this tool can be leveraged. Log files are created which generates the trace information regarding the analysis. This creates two panels and they are:

  • Profile Panel: It provides a summary of all the time spent in the xyz method or what happened inside the method. This shows an exclusive and inclusive time. The exclusive time is the time which is spent in the method whereas inclusive time is the time which is is the time spent in the method along with the time spent in any called functions
  • Timeline panel: It provides the information which describes when the thread and method started and stopped. In other words, when tracing was started and when it ended.

We can generate trace files in two ways and they are:

  1. By including Debug class in code. This is a precise option as we can specify where to start and stop the log traces. In this case we should set permission for writing external storage (READ_EXTERNAL_STORAGE). It would look similar to following few lines of code:
    //start tracing
    //stop tracing

Figure format of using Debug class

  1. There is a method profiling feature of DDMS which we can use to generate trace files. This option is less precise. This is because we cannot alter the code as we are not facilitated to start and stop logging. For android 2.1 and earlier devices we must have an SD card and it should have permission to write to SD Card. As far as the flavors starting from Android 2.2 and later, they are free from requirement of having SD Card as trace log files are directly streamed to the machine used for development.

13.5.3 Systrace Tool

Even after building features, eliminating bugs or cleaning code we are not ready, we have to keep an eye on the performance of application. The smoothness along with speed of drawing pixels screen by our application is an important metric to judge the performance of application. As we know android applications believe in sharing of resources, in other words they operate in a shared resource environment, so performance of one application has some effect over the larger android system.

The Systrace tool allows us to collect and review code execution data for our application and Android system. This helps us to analyze the way in which execution of our application ios going to impact the larger Android system. This facility is available on devices which are 4.1 or higher in flavor. So in order to create a trace of our application we should have a device running on android 4.1 or higher. The device should be set for debugging. Then we would have to connect this to machine used for development and finally install the application. This tool is potential enough to generate lot of data from application and system sources. We can use the following options to limit the data collected by this tool. Some of the options are as follows:

  • -t, --t: This option is used to limit the time used by this tool. By default it is 5 seconds.
  • -b, --buf-size: This option is used to limit the size of data collected by tool.

As soon as trace is generated, location of output file is listed. This report can be viewed using web browser. Best thing about these reports is that they are interactive. We can easily zoom in and out of details of process execution.

13.5.4 RAM Usage

Dalvik Virtual Machine in android system runs an automatic garbage collection but we should be responsible enough for RAM usage by our application. Android applications are meant to run on mobile devices so the Random Access Memory usage of application should be taken care of. User experience is stable only when he or she can switch between apps smoothly. So we should be sure about the usage of memory. It should not unnecessarily consume memory when user is not interacting with the app.

There is a Heap view which shows some basic stats about our heap memory usage. This is usually updated after every garbage collection. In other words this a real time update about the usage of data.

We can use Allocation Tracker for a better understanding of allocated memory-hogging objects. It can be used to analyze critical code paths in an app like scrolling.

13.5.5 Dev Tools App

By default this application is installed on all systems with SDK so we can use it with emulator. We can enable a number of settings on our device that will make it easier to test and debug our applications. This application is dependent on a lot of permissions. This is not available for third party applications so we have to install the Dev Tools application on real system. We have to build a system image for that device and sign the Dev Tools application with the same key which is used for the system image.

13.5.6 GDB Debugging

GDB stands for GNU project DeBugger. This is a common way to debug programs running on Linux system. A gdb tool is available in android to debug the native libraries.

Congratulations ladies and gentlemenJ!! With this we end the debugging half of section but the better half is left. In the next sub-section we shall continue with testing of android applications. See you in the following section. Till then keep practicing. Happy App Developing!!!