D. Mastery: Android SDK 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 SDK tools are integrated by default into Android Studio, or integrated with Eclipse after installing the ADT plugin, whereas other Android SDK 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 SDK tools 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. To see exactly which tools were used during the writing of this book, check out the book’s Introduction and read the section “Development Environments Used in This Book.”

The Android SDK 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’s 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 D.1:

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

Image Up and running with material design: This section provides an introduction to material design, with download links for designers and articles written by Google about material design.

Image Pure Android: This section includes Android design best practices and training for helping you develop applications that deliver a high-quality user experience.

Image Resources: This section includes links to material design resources such as layout templates, sticker sheets, icons, fonts, color palettes, and more.

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 the Android development learning process, 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 Android Studio and the SDK tools. 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. This is also where you download Android Studio and the SDK tools, 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, and will help you learn what you need to know about Google Play developer tools.

Image Samples: This tab provides many sample applications categorized by topic, to help you learn exactly how to use particular APIs. You can browse the various sample projects, or you can download them to your system so that you can import them into your IDE and run them on your devices and emulators to see how important features have been implemented.

Image Preview: This tab provides information about the current preview release of Android. Here, you will learn about features that are coming to the next version of Android, and how to get started with developing for these new APIs.

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 provides an overview of Google Play and introduces the various opportunities and programs Google Play provides developers.

Image Essentials: This tab provides in-depth explanations of the different quality guidelines developers should follow when building their applications. There is also a section dedicated to tools and resources—checklists, guidelines, generators, and more—that will help you make better applications.

Image Get Users: The Get Users section teaches you what you need to know about user acquisition. Topics covered include creating a quality Play store listing, using ads, search, internationalization, and invites to grow, along with other content worth looking into for growing your user base.

Image Engage & Retain: This tab covers techniques that go beyond getting users. Here, you will learn about techniques you can implement for keeping your users active with your application.

Image Earn: This section covers the many different ways you can monetize your application. Monetization topics covered include discussions about freemium, premium, subscription, ad revenue, e-commerce, and other business models for generating income from your users.

Image Analyze: The Analyze tab discusses topics to educate you on tracking and analytics. You will learn how proper tracking can be incorporated into your application, and you will also learn about how to use the data gathered for improving your applications.

Image Stories: 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

Figure D.1 The Android Developer website.

Figure D.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 was 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 D.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 rather 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 for Development,” 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 Android Studio. For more information about the emulator, please check out Appendix B, “Quick-Start: Android Emulator.” We suggest that you review that detailed appendix after you have looked over the other materials covered in this appendix.

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

Image

Figure D.2 The Android emulator in action running the Support 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, “Creating Your First Application,” using the android.util.Log class. The log output appears in the logcat tool window of Android Studio. 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 Android Studio 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 A, “Tips and Tricks: Android Studio.”

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 should be removed prior to application publication.

Debugging Applications with Device Monitor

When it comes to debugging on the emulator or device, you need to turn your attention to the Android Device Monitor tool. Device Monitor is a debugging utility that is integrated into Android Studio. It is also available as a stand-alone executable in the tools/ subdirectory of the Android SDK installation.

The Device Monitor available from Android Studio (see Figure D.3) provides a number of useful features for interacting with emulators and handsets, and debugging applications. You use Device Monitor to view and manage processes and threads running on the device, view heap data, and attach to processes to debug, as well as a variety of other tasks.

Image

Figure D.3 Device Monitor launched from Android Studio.

You can find out all about the Android Device Monitor and how to use its features in Appendix C, “Quick-Start: Android Device Monitor.” We suggest that you review that 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 Android Studio. Both the Device Monitor and the Android SDK tools use ADB to facilitate interaction between the development environment and the device (or emulator). You can find the adb 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 Layout Editor

Android Studio is a solid, well-designed development environment for Java applications. When using Android Studio, 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 Studio Java editor, compiler, and the Gradle build system. The Android SDK tools integrated into Android Studio add numerous special editors for creating Android-specific resource files to encapsulate application data such as strings and user interface resource templates called layouts.

Android layout (user interface templates) resources are technically XML files. However, some people prefer to be able to drag and drop controls, move them around, and preview what the user interface will look like to an actual user. Android Studio has amazing support for designing your layouts visually with the layout editor.

The layout editor loads whenever you open an XML file within the res/layout/ project directory hierarchy. You can use the layout editor in Design view, 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 D.4. You can also switch to Text view to edit controls directly or set specific properties.

Image

Figure D.4 Using the layout editor in Android Studio in Design view.

We discussed the details of designing and developing user interfaces, as well as working with layouts and user interface controls, in Chapter 7, “Exploring Building Blocks,” and Chapter 8, “Positioning with Layouts.” For now, we just want you to be aware that your application’s user interface components are generally stored as resources, and that the Android SDK 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 perspective in the Android Device Monitor, and also as a stand-alone executable in the tools/ subdirectory of your Android SDK installation, which has been deprecated.

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 moving between the Hierarchy View and Pixel Perfect perspectives of Device Monitor.

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. Launch Device Monitor and make sure the application process is selected in the Devices view.

3. Switch to the Hierarchy View perspective in Device Monitor. As an alternative to Device Monitor, navigate to the Android SDK tools/ subdirectory and launch the Hierarchy Viewer application (hierarchyviewer), but note the stand-alone version has been deprecated in favor of the Device Monitor integration.

4. Choose your emulator instance from the Device listing.

5. 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 displayed within the Hierarchy View perspective of Device Monitor and is invaluable for debugging drawing issues related to your application’s user interface controls. If you wonder why something isn’t drawing correctly, try launching the Hierarchy View perspective 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 is a higher-level control. Your application contents are actually child controls within a FrameLayout control called @id/content. When you load layout contents using the setContentView() method within your Activity class, you are specifying what to load within this high-level FrameLayout.


Figure D.5 shows the Device Monitor in the Hierarchy View perspective showing the tree view and loaded in Layout View mode.

Image

Figure D.5 The Hierarchy View perspective (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. The wireframe model of the currently loaded user interface is displayed, with a red box highlighting the currently selected control. To the far left, there is the Windows pane, which lists the currently running devices and their running applications.


Image Tip

You’ll have better luck navigating your application View objects with the Hierarchy View perspective 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 View perspective to interact and debug your application user interface.

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 little red, yellow, or 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. However, 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 available in the tools/ subdirectory of the Android SDK installation. This tool is also integrated into Android Studio and runs automatically when you compile your application. 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 selecting the perspective of the same name in Device Monitor.

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

Image

Figure D.6 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/ subdirectory 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 D.7 illustrates how the image (shown as the square) is divided into nine patches. Often, the center segment is transparent.

Image

Figure D.7 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 D.8 shows a simple PNG file loaded in the tool, prior to its guides being set.

Image

Figure D.8 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 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 D.9 and D.10 illustrate two possible guide configurations.

Image

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

Image

Figure D.10 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 Android Studio. However, if you are not using Android Studio, 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 Android Studio.

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

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

Image jobb: This tool is for encrypting and decrypting your expansion APK files to and from the Opaque Binary Blob (OBB) format.

Image lint: This tool is integrated into Android Studio and runs automatically upon compilation of your application. In addition, it can be run as a command-line tool for inspecting your code for potential bugs and it provides improvement suggestions when found.

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. Although you’ll normally access log output through Android Studio, 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 Android Studio). Although the command-line logcat tool is used to provide better filters, the Android Studio logcat tool window 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 21, “Testing Your 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 22, “Distributing Your Applications.”

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 Android Studio Export Wizard to compile, package, sign, and align your application. We discussed these steps in Chapter 22, “Distributing Your Applications.”

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 Device Monitor debugging tool, which is accessible from within Android Studio, is useful for monitoring emulators and devices. ADB is the powerful command-line tool behind many of the features of Device Monitor. 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 useful tools to help developers with different development tasks, from design to development, testing, and publication.

Quiz Questions

1. True or false: Device Monitor is available as a stand-alone executable.

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

3. What are the two views for working with Android layout files using the Android Studio?

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

Exercises

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

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

3. Using the Android documentation as a reference, describe how to use Device Monitor for tracking the memory allocation of objects.

References and More Information

Android Developers “Package Index” reference:

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

Android Tools: “Android Emulator”:

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

Android Tools: “Device Monitor”:

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

Android Tools: “android”:

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

Android Tools: “Android Debug Bridge”:

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

Android Tools: “logcat”:

http://d.android.com/tools/help/logcat.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

Android Tools: “Profiling with Hierarchy Viewer”:

http://d.android.com/tools/performance/hierarchy-viewer/profiling.html

Android Tools: “Using the Layout Editor”:

http://d.android.com/sdk/installing/studio-layout.html

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.7.13