Introduction to Android Application Development, Fourth Edition (2014)

Part VI. Appendixes

A. Mastering the Android Development Tools

Android developers are fortunate to have many tools at their disposal to help facilitate the design and development of quality applications. Some of the Android development tools are integrated by default into the Android IDE, which is included in the ADT Bundle, or integrated with Eclipse after installing the ADT plugin, whereas other Android development tools must be used from the command line. In this appendix, we walk through a number of the most important tools available for use with Android. Knowledge of ADT will help you develop Android applications faster and with fewer roadblocks.


Image Note

This appendix covers the existing tools available at the time of this writing, and not the new Android Studio, as that product is still very new and not feature complete. To see exactly which tools were used during the writing of this book, check out the book’s Introduction and read the section “Development Environment Used in This Book.”


When this appendix discusses an IDE, the focus is on the Android IDE. Whenever we refer to the Android IDE, the same instructions apply to Eclipse with the ADT plugin. Because the Android IDE and Eclipse with the ADT plugin installed are equivalent, from here on, we will mention only the Android IDE.


Image Note

The Android development tools are updated frequently. We have made every attempt to provide the latest steps for the latest tools. However, these steps and the user interfaces described in this appendix may change at any time. Please review the Android Development website (http://d.android.com/tools/help/index.html) and our book website (http://introductiontoandroid.blogspot.com) for the latest information.


Using the Android Documentation

Although it is not a tool per se, the Android documentation is a key resource for Android developers. An HTML version of the Android documentation is provided in the /docs subfolder of the Android SDK, and the documentation should always be your first stop when you encounter a problem. You can also access the latest help documentation online at the Android Developer website, http://developer.android.com/index.html (or http://d.android.com for short). The Android documentation is organized and searchable and is divided into three main categories with several sections per category, as shown in Figure A.1:

Image Design: This tab provides information about designing Android applications.

Image Get Started: When designing your application, you must start somewhere. This section is a great resource for learning how to get started designing your Android application.

Image Style: This links to quite a few sections that are useful for understanding how to visually style your applications. Topics such as themes, color, iconography, and typography are explained here.

Image Patterns: This link provides many sections describing common design patterns found throughout Android applications. Providing a pleasant and consistent experience to our users is important, and many of the problems associated with design issues are described here.

Image Building Blocks: This link comprises many sections explaining the many common types of user interface elements. Each section further describes how to use a specific element.

Image Downloads: This link provides many downloadable resources that are useful for designing Android applications. Provided for download are controls and icons following the typography and color guidelines of Android. You are able to download all files as one .zip, or you may choose to download only the files that are relevant to your design goals.

Image Videos: This section provides many videos from design sessions presented during the Google I/O conference. These videos are useful for learning tips and tricks directly from the Android team.

Image Develop: This tab provides information for developing Android applications.

Image Training: The training section includes tutorials for using specific classes and provides code samples that you are free to use within your applications. The tutorials are listed in order of how one should proceed through learning about Android development, and many important topics are discussed in depth. These trainings are an invaluable resource to Android developers.

Image API Guides: This tab provides in-depth explanations of many Android topics, classes, or packages. Although related to the Training section, the API Guides provide a much deeper explanation of particular features of the Android API.

Image Reference: This tab includes a searchable package and class index of all Android APIs provided as part of the Android SDK in a Javadoc-style format. You will spend most of your time on this tab, looking up Java class documentation, checking method parameters, and performing other similar tasks.

Image Tools: The Tools tab is the definitive resource for learning about the Android Developer Tools plugin that comes bundled with the Android IDE. Many of these tools are covered throughout this book and are usable from either your IDE or the command line. One particular section of the Tools tab, the Tools Help section, is particularly useful for learning how to use both the SDK Tools and the Platform Tools.

Image Get the SDK: This section provides downloads for the Android SDK. Included for download are the Android SDK Bundle and the SDK Tools, and each is available for Windows, Mac, and Linux.

Image Google Services: This tab provides tutorials, code samples, and API guides for integrating Google services into your Android application.

Image Distribute: This tab provides information about distributing Android applications.

Image Google Play: This tab provides an introduction to Google Play. Having a good understanding of Google Play is important before launching your application. This section covers how to gain visibility, monetize your application, and control distribution.

Image Publishing: The Publishing section teaches developers what they need to know to publish their applications with Google Play. Explanations of the Developer Console are provided in addition to a Publishing Checklist that developers should follow to ensure they have a successful launch.

Image Promoting: This section covers promoting your applications outside Google Play. Google provides many tools for making this an easy process, such as the Device Art Generator, which can be used for adding screenshots of your application to real device art for use within your advertising copy. This section also provides instructions for branding guidelines for incorporating the Android and Google Play brands into the advertising copy of your applications.

Image App Quality: This section provides resources for maximizing the quality of your applications. There is a list of criteria for evaluating application quality, and tips are provided for how to improve quality.

Image Policies: This section consists of guidelines developers must follow. Failure to follow these guidelines may result in removal of your application from Google Play. Understanding these policies will help you comply with them.

Image Spotlight: This section highlights some applications that have been successfully distributed within Google Play. Learning how others have achieved success is always helpful for understanding how those principles may be applied to your application.

Image Open Distribution: This section discusses alternative distribution methods to Google Play. Although Google Play is a great source for achieving success, there are other options developers should know about, and these are briefly discussed here.

Image

Figure A.1 The Android Developer website.

Figure A.1 shows a screenshot of the Android SDK Reference tab of the website.

Now is a good time to learn your way around the Android SDK documentation. First, check out the online documentation, and then try the local documentation.


Image Tip

Different features of the Android SDK are applicable to different versions of the platform. New APIs, classes, interfaces, and methods have been introduced over time. Therefore, each item in the documentation is tagged with the API level when it was first introduced. To see whether an item is available in a specific platform version, check its API level, usually listed along the right side of the documentation. You can also filter the documentation to a specific API level, so that it displays only the SDK features available for that platform version (see Figure A.1, left center).


Keep in mind that this book is designed to be a companion guide in your journey to mastering Android development. It covers Android fundamentals and tries to distill a lot of information into an easily digestible format to get you up and running quickly. It then provides you with a thorough understanding of what is available and feasible on the Android platform. It is not an exhaustive SDK reference, but a guide to best practices. You’ll need to become intimately familiar with the Android SDK Java class documentation in order to be successful at designing and developing Android applications in the long run.

Leveraging the Android Emulator

Although we introduced the Android emulator as a core tool in Chapter 2, “Setting Up Your Android Development Environment,” it’s worth mentioning again. The Android emulator is probably the most powerful tool at a developer’s disposal, along with the Android SDK and Android Virtual Device Managers (which we also talked about fairly extensively in other chapters of this book). It is important for developers to learn to use the emulator and understand its limitations. The Android emulator is integrated with the Android IDE. For more information about the emulator, please check out Appendix B, “Quick-Start Guide: The Android Emulator.” We suggest that you review this detailed appendix after you have looked over the other materials covered in this appendix.

Figure A.2 shows a sample application included with the Android SDK called App Navigation and what it looks like when running in the Android emulator.

Image

Figure A.2 The Android emulator in action running the App Navigation sample application.

You can also find exhaustive information about the emulator on the Android Developer website: http://d.android.com/tools/help/emulator.html.

Viewing Application Log Data with LogCat

You learned how to log application information in Chapter 3, “Writing Your First Android Application,” using the android.util.Log class. The log output appears in the LogCat pane of the Android IDE (available in the Debug and DDMS perspectives of the Android IDE). You can also interact with logcat directly.

Even when you have a great debugger, incorporating logging support into your applications is very useful. You can then monitor your application’s log output, generated on either the emulator or an attached device. Log information can be invaluable for tracking down difficult bugs and reporting application state during the development phase of a project.

Log data is categorized by severity. When you create a new class in your project, we recommend defining a unique debug tag string for that class so that you can easily track down where a log message originated. You can use this tag to filter the logging data and find only the messages you are interested in. You can use the LogCat utility from within the Android IDE to filter your log messages to the debug tag string you supplied for your application. To learn how to do this, check out the “Creating Custom Log Filters” section in Appendix D, “Android IDE and Eclipse Tips and Tricks.”

Finally, there are some performance trade-offs to consider when it comes to logging. Excessive logging impacts device and application performance. At a minimum, debug and verbose logging should be used only for development purposes and removed prior to application publication.

Debugging Applications with DDMS

When it comes to debugging on the emulator or device, you need to turn your attention to the Dalvik Debug Monitor Service (DDMS) tool. DDMS is a debugging utility that is integrated into the Android IDE through a special perspective. It is also available as a standalone executable in the /toolsdirectory of the Android SDK installation.

The DDMS perspective in the Android IDE (see Figure A.3) provides a number of useful features for interacting with emulators and handsets and debugging applications. You use DDMS to view and manage processes and threads running on the device, view heap data, attach to processes to debug, and a variety of other tasks.

Image

Figure A.3 Using DDMS integrated into the Android IDE perspective.

You can find out all about DDMS and how to use its features in Appendix C, “Quick-Start Guide: Android DDMS.” We suggest that you review this detailed appendix after you have looked over the other material covered in this appendix.

Using Android Debug Bridge (ADB)

The Android Debug Bridge (ADB) is a client/server command-line tool that enables developers to debug Android code on the emulator and the device using the Android IDE. Both the DDMS and the Android Developer Tools use ADB to facilitate interaction between the development environment and the device (or emulator). You can find the adb.exe command-line tool in the /platform-tools directory of the Android SDK.

Developers can also use ADB to interact with the device file system, install and uninstall Android applications manually, and issue shell commands. For example, the logcat and sqlite3 shell commands enable you to access logging data and application databases.

For an exhaustive ADB reference, see the Android SDK documentation at http://d.android.com/tools/help/adb.html.

Using the Resource Editors and UI Designer

The Android IDE is a solid, well-designed development environment for Java applications. When using the Android IDE, you have access to a bunch of simple Android-specific tools to help you design, develop, debug, and publish applications. Like all applications, Android apps are made up of functionality (Java code) and data (resources such as strings and graphics). The functionality is handled with the Android IDE Java editor and compiler. The ADT plugin integrated into the IDE adds numerous special editors for creating Android-specific resource files to encapsulate application data such as strings and user interface resource templates called layouts.


Image Note

The Android tools team has been hard at work integrating Android support into another IDE for developers. This IDE is known as Android Studio, which is based on the Community Edition of IntelliJ IDEA. We should continue to see more energy put into Android Studio, as well as feature parity with Android IDE and Eclipse with the ADT plugin.


Most Android resources are stored in specially formatted XML files. The resource editors and UI designer that come as part of the ADT plugin allow you to work with application resources in a structured, graphical way, or by editing the raw XML. Two examples of resource editors include the string resource editor and the Android manifest file editor. The generic resource editor will load when you are working with XML files in the /res project directory hierarchy, such as string, style, or dimension resources.

The Android manifest file resource editor will load when you open the Android manifest file associated with your project. Figure A.4 shows that the resource editor for editing Android manifest files consists of numerous tabs, which organize the contents of that resource type. Note that the last tab is always the XML tab, where you can edit the XML resource file manually, if necessary.

Image

Figure A.4 Editing the Android manifest file in the Android IDE.

Android layout (user interface templates) resources are also technically XML files. However, some people prefer to be able to drag and drop controls, move them around, and preview what the user interface would look like to an actual user. Recent updates to the ADT plugin have greatly improved UI designer features for developers.

The UI designer loads whenever you open an XML file within the /res/layout project directory hierarchy. You can use the UI designer in Graphical Layout mode, which allows you to drag and drop controls and see what your application will look like with a variety of AVD-style configuration options (Android API level, screen resolution, orientation, theme, and more), as shown in Figure A.5. You can also switch to XML editing mode to edit controls directly or set specific properties.

Image

Figure A.5 Using the UI designer in the Android IDE.


Image Tip

We recommend anchoring the Properties pane of the Android IDE to the right of the UI designer to edit properties of a selected control in a more structured manner, as seen in the lower right of Figure A.5.


We discussed the details of designing and developing user interfaces, as well as working with layouts and user interface controls, in Chapter 7, “Exploring User Interface Building Blocks,” and Chapter 8, “Designing with Layouts.” For now, we just want you to be aware that your application user interface components are generally stored as resources, and that ADT provides some helpful tools for designing and managing these resources.

Using the Android Hierarchy Viewer

The Android Hierarchy Viewer is a tool that identifies layout component relationships (the hierarchy) and helps developers design, debug, and profile their user interfaces. Developers can use this tool to inspect the user interface control properties and develop pixel-perfect layouts. The Hierarchy Viewer is available as a standalone executable in the /tools subdirectory of your Android SDK installation, as well as an Android IDE perspective. Figure A.6 shows what the Hierarchy Viewer looks like when first launched and connected to an emulator instance, before any of the graphical views are shown. The application to be inspected has a package name called com.example.android.appnavigation.

Image

Figure A.6 Screenshot of the Android Hierarchy Viewer standalone app when first launched.

The Hierarchy Viewer is a visual tool that can be used to inspect your application user interfaces in ways that allow you to identify and improve your layout designs. You can drill down on specific user interface controls and inspect their properties at runtime. You can save screenshots of the current application state on the emulator or the device.

The Hierarchy Viewer application is divided into two main modes:

Image Layout View mode: This mode shows the hierarchy of user interface controls loaded by your application in tree form. You can zoom in and select specific controls to find out lots of information about their current state; there is also profiling information to help you optimize your controls.

Image Pixel Perfect mode: This mode shows the user interface pixels in a zoomed-in grid fashion. This is useful for designers who need to look at very specific layout arrangements or line up views on top of images.

You can switch between the modes by using the buttons in the bottom-left corner of the tool.

Launching the Hierarchy Viewer

To launch the Hierarchy Viewer with your application in the emulator, perform the following steps:

1. Launch your Android application in the emulator.

2. Navigate to the Android SDK tools subdirectory and launch the Hierarchy Viewer application (hierarchyviewer.bat on Windows), or use the Hierarchy View Android IDE perspective. We find using the standalone executable more convenient because we often want to tweak the user interface using the Android IDE while we work.

3. Choose your emulator instance from the Device listing.

4. Select the application you want to view from the options available. The application must be running on that emulator to show up on the list.

Working in Layout View Mode

The Layout View mode is invaluable for debugging drawing issues related to your application user interface controls. If you wonder why something isn’t drawing correctly, try launching the Hierarchy Viewer and checking the properties for that control at runtime.


Image Note

When you load an application in the Hierarchy Viewer, you will want to be aware of the fact that your application user interface does not begin at the root of the hierarchy in the tree view. In fact, there are several layers of layout controls above your application content that will appear as parent controls of your content. For example, the system status bar and title bar are higher-level controls. Your application contents are actually child controls within a FrameLayout control called @id/content. When you load layout contents using thesetContentView() method within your Activity class, you are specifying what to load within this high-level FrameLayout.


Figure A.7 shows the Hierarchy Viewer loaded in Layout View mode.

Image

Figure A.7 The Hierarchy Viewer tool (Layout View mode).

When you first load your application in Layout View mode, you will see several panes of information. The main pane shows the parent/child control relationships as a tree view. Each tree node represents a user interface control on the screen and shows the control’s unique identifier, type, and profiling information for optimization purposes (more on this in a moment). There are also a number of smaller panes on the right side of the screen. The loupe/zoom pane allows you to quickly navigate a large tree view. The property pane shows the various properties for each tree node, when highlighted. Finally, the wireframe model of the currently loaded user interface is displayed, with a red box highlighting the currently selected control.


Image Tip

You’ll have better luck navigating your application View objects with the Hierarchy Viewer tool if you set your View object ID properties to friendly names you can remember instead of the autogenerated sequential ID tags provided by default. For example, a Button control called SubmitButton is more descriptive than Button01.


You can use the Hierarchy Viewer tool to interact and debug your application user interface. Specifically, you can use the Invalidate and Request Layout features that correspond to the View.invalidate() and View.requestLayout() functions of the UI thread. These functions initiate Viewobjects and draw or redraw them as necessary.

Optimizing Your User Interface

You can also use the Hierarchy Viewer to optimize your user interface contents. If you have used this tool before, you may have noticed all the little red, yellow, and green dots in the tree view. These are performance indicators for each specific control:

Image The left dot represents how long the measuring operation for this view takes.

Image The middle dot represents how long the layout-rendering operation for this view takes.

Image The right dot represents how long the drawing operation for this view takes.

Indicators represent how each control renders in relation to other controls in the tree. They are not a strict representation of a bad or good control, per se. A red dot means that this view renders the slowest, compared to all views in the hierarchy. A yellow dot means that this view renders in the bottom 50% of all views in the hierarchy. A green dot means that this view renders in the top 50% of all views in the hierarchy. When you click a specific view within the tree, you will also see the actual performance times on which these indicators are based.


Image Tip

The Hierarchy Viewer provides control-level precision profiling. But it won’t tell you if your user interface layouts are organized in the most efficient way. For that, you’ll want to check out the lint command-line tool (formerly the layoutopt command-line tool) available in the /tools subdirectory of the Android SDK installation. This tool will help you identify unnecessary layout controls in your user interface, among other inefficiencies. Find out more at the Android Developer website: http://d.android.com/tools/debugging/debugging-ui.html#lint.


Working in Pixel Perfect Mode

You can use the Pixel Perfect mode to closely inspect your application user interface. You can also load PNG mock-up files to overlay your user interface and adjust your application’s look. You can access the Pixel Perfect mode by clicking the button with the nine pixels on it at the bottom left of the Hierarchy Viewer.

Figure A.8 illustrates how you can inspect the currently running application screen at the pixel level by using the loupe feature of this mode.

Image

Figure A.8 The Hierarchy Viewer tool (Pixel Perfect mode).

Working with Nine-Patch Stretchable Graphics

Android supports Nine-Patch Stretchable Graphics, which provide flexibility for supporting different user interface characteristics, orientations, and device screens. Nine-Patch Stretchable Graphics can be created from PNG files using the draw9patch tool included with the /tools directory of the Android SDK.

Nine-Patch Stretchable Graphics are simply PNG graphics that have patches, or areas of the image, defined to scale appropriately, instead of the entire image being scaled as one unit. Figure A.9 illustrates how the image (shown as the square) is divided into nine patches. Often the center segment is transparent.

Image

Figure A.9 How a Nine-Patch graphic of a square is scaled.

The interface for the draw9patch tool is straightforward. In the left pane, you can define the guides to your graphic to specify how it scales when stretched. In the right pane, you can preview how your graphic behaves when scaled with the patches you defined. Figure A.10 shows a simple PNG file loaded in the tool, prior to its guides being set.

Image

Figure A.10 A simple PNG file before Nine-Patch processing.

To create a Nine-Patch Stretchable Graphic file from a PNG file using the draw9patch tool, perform the following steps:

1. Launch draw9patch.bat in your Android SDK tools subdirectory.

2. Drag a PNG file onto the pane (or use File, Open Nine-Patch).

3. Click the Show patches check box at the bottom of the left pane.

4. Set your Patch scale appropriately (set it higher to see more marked results).

5. Click along the left edge of your graphic to set a horizontal patch guide.

6. Click along the top edge of your graphic to set a vertical patch guide.

7. View the results in the right pane; move the patch guides until the graphic stretches as desired. Figures A.11 and A.12 illustrate two possible guide configurations.

Image

Figure A.11 A Nine-Patch PNG file after Nine-Patch processing with some patch guides defined.

Image

Figure A.12 A Nine-Patch PNG file after Nine-Patch processing with some different patch guides defined.

8. To delete a patch guide, press Shift and click the guide pixel (black) or left-click the guide pixel.

9. Save your graphics file. Nine-Patch graphics should end with the extension .9.png (for example, little_black_box.9.png).

10. Include your graphics file as a resource in your Android project and use it just as you would a normal PNG file.

Working with Other Android Tools

Although we’ve already covered the most important tools, a number of other special-purpose utilities are included with the Android SDK. Many of these tools provide the underlying functionality that has been integrated into the Android IDE. However, if you are not using the Android IDE, these tools may be used on the command line.

A complete list of the development tools that come as part of the Android SDK is available on the Android Developer website at http://d.android.com/tools/help/index.html.

There, you’ll find a description of each tool as well as a link to its official documentation. Here is a list of some useful tools we haven’t yet discussed:

Image android: This command-line tool provides much the same functionality as the Android SDK and Android Virtual Device Managers; it also helps you create and manage projects if you are not using the Android IDE.

Image bmgr: This shell tool is accessed through the adb command line to interact with the Android Backup Manager.

Image dmtracedump, hprof-conv, traceview: These tools are used for diagnostics, debug logging, and profiling of applications.

Image etc1tool: This command-line tool lets you convert between PNG files and compressed Ericsson Texture Compression (ETC1) files. The specification for ETC1 is available at http://www.khronos.org/registry/gles/extensions/OES/OES_compressed_ETC1_RGB8_texture.txt.

Image logcat: This shell tool is accessed through the adb command line to interact with the platform logging tool, LogCat. Although you’ll normally access log output through the Android IDE, you can also use this shell tool to capture, clear, and redirect log output (a useful feature if you’re doing any automation, or not using the Android IDE). Although the command-line logcat tool is used to provide better filters, the Android IDE logcat view has brought this filtering power to the graphical version.

Image mksdcard: This command-line tool lets you create SD card disk images independent of a specific AVD.

Image monkey, monkeyrunner: These are tools you can use to test your application and implement automated testing suites. We discussed unit testing and test opportunities for applications in Chapter 18, “Testing Android Applications.”

Image ProGuard: This is a tool for obfuscating and optimizing application code. We talked more about ProGuard, and specifically how to protect the intellectual property of an application, in Chapter 19, “Publishing Your Android Application.”

Image sqlite3: This shell tool is accessed through the adb command line to interact with SQLite databases.

Image systrace: This is a performance analysis tool for learning about the execution of applications.

Image Tracer for OpenGL ES: This tool allows you to analyze the execution of OpenGL ES code to understand how an application is processing and executing graphics.

Image uiautomator: This is an automated functional UI testing framework for creating and running user interface tests for an application.

Image zipalign: This command-line tool is used to align an APK file after it has been signed for publication. This tool is necessary only if you do not use the Android IDE Export Wizard to compile, package, sign, and align your application. We discussed these steps in Chapter 19, “Publishing Your Android Application.”

Summary

The Android SDK ships with a number of powerful tools to help with common Android development tasks. The Android documentation is an essential reference for developers. The Android emulator can be used for running and debugging Android applications virtually, without the need for an actual device. The DDMS debugging tool, which is integrated into the Android IDE as a perspective, is useful for monitoring emulators and devices. ADB is the powerful command-line tool behind many of the features of DDMS and the ADT plugin. The Hierarchy Viewer and lint tools can be used to design and optimize your user interface controls, and the Nine-Patch tool allows you to create stretchable graphics for use within your apps. There are also a number of other tools to aid developers with different development tasks, from design to development, testing, and publication.

Quiz Questions

1. True or false: DDMS is available as a standalone executable.

2. Which SDK subdirectory folder holds the adb command-line tool?

3. True or false: Android string resource files are stored in the json format.

4. What are the two modes for working with Android layout files using the Android IDE?

5. Which tool is used for inspecting and optimizing the user interface?

Exercises

1. Using the Android documentation, create a list of the logcat command-line options.

2. Using the Android documentation, determine which adb command is used for printing a list of all attached emulator instances.

3. Using the Android documentation, describe how to use DDMS for tracking the memory allocation of objects.

References and More Information

Google’s Android Developers Online SDK Reference:

http://d.android.com/reference/packages.html

Android Tools: “Android Emulator”:

http://d.android.com/tools/help/emulator.html

Android Tools: “Using DDMS”:

http://d.android.com/tools/debugging/ddms.html

Android Tools: “Android Debug Bridge”:

http://d.android.com/tools/help/adb.html

Android Tools: “Draw 9-Patch”:

http://d.android.com/tools/help/draw9patch.html

Android Tools: “Optimizing your UI”:

http://d.android.com/tools/debugging/debugging-ui.html