Debugging/Optimization Tools

Total Page:16

File Type:pdf, Size:1020Kb

Debugging/Optimization Tools

Android Debugging/Optimization Tools By Victor Gallego

This tutorial will introduce some of the tools available in your Android SDK.

If you are using Eclipse your tools folder is found in your Android SDK folder. So if your SDK is on the root of your C: drive for example, it can be found in: C: \android-sdk-windows\tools

For those using MOTODEV Studio, your tools folder may be found in your installation folder: C:\Program Files\Motorola\MOTODEV Studio for Android 1.0.2\android-sdk- windows\tools

1. Dev Tools –Show Screen Updates

Open the Dev tools application installed on your Android emulator.

Show Screen updates

Invalidate call while performing drag and drop on an icon

The following is shown whenever a view has invoked the invalidate() method. This tool may be useful for identifying whether your application is redrawing parts of the screen unnecessarily. For the example shown above, an icon on the home screen will invalidate itself while being dragged, here it makes sense to redraw just a small portion of the screen and not the entire screen.

For more information I recommend the following link: http://developer.android.com/guide/developing/debug-tasks.html

2. Hierarchy Viewer

Typically, for optimizing your android application’s UI your goal as a developer is to make the View Hierarchy as flat as possible. As seen below in hierarchy viewer, your views are represented with a tree structured where children views are nested within their respective parent views. The layout created in XML, for instance the beginning of our main.xml starts under the FrameLayout whose id is content. We can see that can see that this particular layout consists of a LinearLayout, one TextView and three Buttons. Your XML Layout begins here

This example is not very complex, but you can imagine for more interesting layouts the hierarchy can become long if you are not careful. Long hierarchies increase the amount of time it takes to draw your layout, and so produce a less responsive UI. Long hierarchies are generally attributed to adding views groups unnecessarily for “better” placement of your widgets on the screen.

The strategy for achieving flat hierarchies is to use a RelativeLayout. RelativeLayouts allow you to define rules which place your views on the screen relative to one another or relative to one particular view. You can build very complicated layouts with RelativeLayouts without having to nest views within each other and consequently add depth to your hierarchy.

Here is a list of articles I recommend reading for more information: http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html http://developer.android.com/guide/developing/tools/hierarchy-viewer.html 3. Traceview

Trace Threads timeline

Profile Panel

Usually we are only concerned with the main thread. This is the thread where the UI runs. Shown above, each method has its parents (the calling method) and children (called methods). The columns are described below:

 Inc % - percentage of the total time spent in the method plus any called methods.  Inclusive – amount of time spent in the method plus the time spent in any called methods.  Excl % - percentage of the total time spent in the method.  Exclusive – amount of time spent in the method  Calls + Recursive – the number of calls to this method plus any recursive calls  Time/Call – the average amount of time per call

Note: Method tracing has huge impact on performance, so before uploading your application to the market, it is best remove all tracing code from your application.

In order to start method tracing in Android here is what is required.

 Debug.startMethodTracing("name_of_tracefile"); o This method usually goes in your onCreate() method of your starting activity, but really it can go in any method. Any name can be entered for the trace file name.  Debug.stopMethodTracing(); o This method usually goes in either of the methods of below for the Activity you are tracing. . onPause() . onStop() . onDestroy()  o You must add this permission to your android manifest file for your application in order to write the trace file to your SD Card on the emulator/device.

To pull the trace file off the SD card you must use DDMS in Eclipse/Motodev, select the file you wish to pull from the device/emulator and hit the pull icon in the top corner of the file explorer. Once the trace file is on your system simply drag the trace file on top of your traceview.bat in your SDK tools folder.

A general tip for identifying methods with performance issues is to identify where there is a large dip or large change in percentages in either the inclusive % or exclusive % column. Otherwise try to identify methods which are taking too long to execute when they should be executing much faster.

Useful links: http://developer.android.com/guide/developing/tools/traceview.html http://newfoo.net/2009/04/18/performance-tuning-android-applications.html http://stackoverflow.com/questions/1957135/how-to-test-the-performance-of-an-android- application

Recommended publications