Introduction to Android Application Development, Fourth Edition (2014)

Part I. An Overview of the Android Platform

Chapter 2. Setting Up Your Android Development Environment

Android developers write and test applications on their computers and then deploy those applications onto the actual device hardware for further testing.

In this chapter, you become familiar with all the tools you need to master in order to develop Android applications. You learn information about configuring your development environment both on a virtual device and on real hardware. You also explore the Android SDK and all it has to offer.


Image Note

The Android SDK and Android Development Tool Bundles 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 chapter may change at any time. Please refer to the Android development website (http://d.android.com/sdk/index.html) and our book website (http://introductiontoandroid.blogspot.com) for the latest information.


Configuring Your Development Environment

To write Android applications, you must configure your programming environment for Java development. The software is available online for download at no cost. Android applications can be developed on Windows, Macintosh, or Linux systems.

To develop Android applications, you need to have the following software installed on your computer:

Image The Java Development Kit (JDK), Version 6, available for download at http://oracle.com/technetwork/java/javase/downloads/index.html (or http://java.sun.com/javase/downloads/index.jsp, if you’re nostalgic).

Image The latest Android SDK. In this book, we will cover using the Android SDK included in the ADT Bundle, which is available for Windows, Mac, or Linux and can be downloaded at http://d.android.com/sdk/index.html. The ADT Bundle includes everything you will need for working on the examples in this book and for developing Android applications. Other items included in the ADT Bundle are the SDK Tools, Platform Tools, the latest Android platform, and the latest Android System image for the emulator.

Image A compatible Java IDE is required. Luckily, the ADT Bundle provides the Android IDE, which is a special version of the Eclipse IDE. The Android IDE comes ready with the ADT plugin already installed. This book focuses on using the Android IDE. An alternative to using the Android IDE would be to use your own copy of Eclipse. You will need Eclipse 3.6.2 (Helios) or later and either Eclipse IDE for Java Developers, Eclipse Classic, or Eclipse IDE for Java EE Developers. When using your own copy of Eclipse, you should install the ADT plugin for Eclipse. This plugin is available for download through the Eclipse software update mechanism. For instructions on how to install this plugin, see http://d.android.com/tools/sdk/eclipse-adt.html. Although this tool is optional for development, we highly recommend installing it when using a regular Eclipse IDE. We will use many of this plugin’s features frequently throughout this book, although this will be done using the Android IDE.

A complete list of Android development system requirements is available at http://d.android.com/sdk/index.html.


Image Tip

Most developers use the Android IDE for Android development. The Android development team has integrated the Android development tools directly into the Eclipse IDE. However, developers are not constrained to using the Android IDE or Eclipse; they can also use other IDEs, such as the new Android Studio based on IntelliJ IDEA. This book does not cover how to develop Android applications with Android Studio. For information on using Android Studio, read the getting-started guide athttp://d.android.com/sdk/installing/studio.html. For information on using other development environments, begin by reading http://d.android.com/tools/projects/projects-cmdline.html; it talks about using the command-line tools, which may be useful for using other environments. In addition, read http://d.android.com/tools/debugging/debugging-projects-cmdline.html for information on debugging from other IDEs, and http://d.android.com/tools/testing/testing_otheride.html for testing from other IDEs.


The basic installation process follows these steps:

1. Download and install the appropriate JDK.

2. (Android IDE users only) Download and unzip the appropriate Android SDK ADT Bundle.

3. (Eclipse users only) Download and install the appropriate Android SDK Tools, Eclipse IDE, and the Android ADT plugin for Eclipse. Configure the Android settings in the Eclipse preferences. Make sure you specify the directory where you installed the Android SDK and save your settings.

4. Launch the IDE and use the Android SDK Manager to download and install specific Android platform versions and other components you might use, including the documentation, sample applications, USB drivers, and additional tools. The Android SDK Manager is available as a standalone tool in the Android ADT Bundle, as well as from within the Android IDE (or Eclipse once you have installed the plugin in the previous step). In terms of which components you’ll want to choose, we recommend a full installation (choose everything).

5. Configure your computer for device debugging by installing the appropriate USB drivers, if necessary.

6. Configure your Android device(s) for device debugging.

7. Start developing Android applications.

In this book, we do not give you detailed step-by-step instructions for installing each and every component listed in the preceding steps for three main reasons. First, this is an intermediate/advanced book, and we expect you have some familiarity with installing Java development tools and SDKs. Second, the Android Developer website provides fairly extensive information about installing development tools and configuring them on a variety of different operating systems. Installation instructions for setting up the ADT Bundle are available athttp://d.android.com/sdk/installing/bundle.html, and instructions for setting up an existing IDE are available at http://d.android.com/sdk/installing/index.html. Third, the exact steps required to install the Android SDK tend to change subtly with each and every major release (and some minor releases), so you’re always better off checking the Android Developer website for the latest information. Finally, keep in mind that the Android SDK, ADT Bundle, and tools are updated frequently and may not exactly match the development environment used in this book, as defined in the Introduction. That said, we will help you work through some of the later steps in the process described in this section, starting after you’ve installed and configured the JDK and the ADT Bundle in step 2. We’ll poke around in the Android SDK and look at some of the core tools you’ll need to use to develop applications. Then, in the next chapter, you’ll test your development environment and write your first Android application.

Configuring Your Operating System for Device Debugging

To install and debug Android applications on Android devices, you need to configure your operating system to access the phone via the USB cable (see Figure 2.1). On some operating systems, such as Mac OS, this may just work. However, for Windows installations, you need to install the appropriate USB driver. You can download the Windows USB driver from the following website: http://d.android.com/sdk/win-usb.html. Under Linux, there are some additional steps to perform. See http://d.android.com/tools/device.html for more information.

Image

Figure 2.1 Android application debugging using the emulator and an Android handset.

Configuring Your Android Hardware for Debugging

Android devices have debugging disabled by default. Your Android device must be enabled for debugging via a USB connection to allow the tools to install and launch the applications you deploy.

Devices that have Android 4.2+ require enabling Developer Options for testing your applications on real hardware. We will be discussing how to configure your hardware for working with Android 4.3, but if you are working with a different version of Android, check out the following link to learn how to get set up with your version: http://d.android.com/tools/device.html#setting-up. Different versions of Android use different setup methods, so just perform the method for the version you have.


Image Note

You can enable the Developer Options by selecting Home, All Apps, Settings, About Phone (or About Tablet); then scroll down to Build Number, and press Build Number seven times. After a few presses, you will notice a message displaying “You are now X steps away from being a developer.” Continue to press Build Number until you are told that Developer Options have been enabled. If you do not enable the Developer Options, you will not be able to install your applications on your device.


You need to enable your device to install Android applications other than those from the Google Play store. This setting is reached by navigating to Settings, Security. Here, you should check (enable) the option called Unknown sources, as shown in Figure 2.2. If you do not enable this option, you cannot install developer-created applications, the sample applications, or applications published on alternative markets without the developer tools. Loading applications from servers or even email is a great way to test deployments.

Image

Figure 2.2 Enabling Unknown sources on the device.

Several other important development settings are available on the Android device by selecting Home, All Apps, Settings, Developer options (see Figure 2.3).

Image

Figure 2.3 Enabling Android developer settings on the device.

Here, you should enable the following options:

Image USB debugging: This setting enables you to debug your applications via the USB connection.

Image Allow mock locations: This setting enables you to send mock location information to the phone for development purposes and is very convenient for applications using LBS.

Upgrading the Android SDK

The Android SDK is upgraded from time to time. You can easily upgrade the Android SDK and tools from within the Android IDE using the Android SDK Manager, which is installed as part of the ADT plugin.

Changes to the Android SDK might include addition, update, and removal of features; package name changes; and updated tools. With each new version of the SDK, Google provides the following useful documents:

Image An Overview of Changes: A brief description of the major changes to the SDK

Image An API Diff Report: A complete list of specific changes to the SDK

Image Release Notes: A list of known issues with the SDK

These documents are available with every new release of the Android SDK. For instance, Android 4.3 information is available at http://d.android.com/about/versions/android-4.3.html, and Android 4.2 information is available at http://d.android.com/about/versions/android-4.2.html.

You can find out more about adding and updating SDK packages at http://d.android.com/sdk/installing/adding-packages.html.

Problems with the Android Software Development Kit

Because the Android SDK is constantly under active development, you might come across problems with it. If you think you’ve found a problem, you can find a list of open issues and their status at the Android project’s Issue Tracker website. You can also submit new issues for review.

The Issue Tracker website for the Android open-source project is https://code.google.com/p/android/issues/list. For more information about logging your own bugs or defects to be considered by the Android platform development team, check out the following website:http://source.android.com/source/report-bugs.html.


Image Tip

Frustrated with how long it takes for your bug to get fixed? It can be helpful to understand how the Android bug-resolution process works. For more information on this process, see http://source.android.com/source/life-of-a-bug.html.


Exploring the Android SDK

The Android SDK ADT Bundle comes with several major components: the Android Application Framework, Platform Tools, SDK Tools, extras, and sample applications.

Understanding the Android SDK License Agreement

Before you can download the Android SDK ADT Bundle, you must review and agree to the Android SDK License Agreement. This agreement is a contract between you (the developer) and Google (copyright holder of the Android SDK).

Even if someone at your company has agreed to the licensing agreement on your behalf, it is important for you, the developer, to be aware of a few important points:

Image Rights granted: Google (as the copyright holder of Android) grants you a limited, worldwide, royalty-free, nonassignable, and nonexclusive license to use the SDK solely to develop applications for the Android platform. Google (and third-party contributors) are granting you license, but they still hold all copyrights and intellectual property rights to the material. Using the Android SDK does not grant you permission to use any Google brands, logos, or trade names. You may not remove any of the copyright notices therein. Third-party applications that your applications interact with (other Android apps) are subject to separate terms and fall outside this agreement.

Image SDK usage: You may only develop Android applications. You may not make derivative works from the SDK or distribute the SDK on any device or distribute part of the SDK with other software.

Image SDK changes and backward compatibility: Google may change the Android SDK at any time, without notice, without regard to backward compatibility. Although Android API changes were a major issue with prerelease versions of the SDK, recent releases have been reasonably stable. That said, each SDK update does tend to affect a small number of existing applications in the field, thus necessitating updates.

Image Android application developer rights: You retain all rights to any Android software you develop with the SDK, including intellectual property rights. You also retain all responsibility for your own work.

Image Android application privacy requirements: You agree that your application will protect the privacy and legal rights of its users. If your application uses or accesses personal and private information about the user (usernames, passwords, and so on), your application must provide an adequate privacy notice and keep that data stored securely. Note that privacy laws and regulations may vary by user location; you as a developer are solely responsible for managing this data appropriately.

Image Android application malware requirements: You are responsible for all applications you develop. You agree not to write disruptive applications or malware. You are solely responsible for all data transmitted through your application.

Image Additional terms for specific Google APIs: Use of the Google Maps Android API is subject to further Terms of Service. You must agree to these additional terms before using those specific APIs and always include the Google Maps copyright notice provided. Use of Google APIs (Google apps such as Gmail, Blogger, Google Calendar, YouTube, and so on) is limited to access what the user has explicitly granted permission for during installation time.

Image Develop at your own risk: Any harm that comes about from developing with the Android SDK is your own fault and not Google’s.

Reading the Android SDK Documentation

The Android documentation is provided in HTML format online at http://d.android.com/index.html. If you would like to have a local copy of the docs, you need to download them using the SDK Manager. Once you have downloaded them, a local copy of the Android documentation is provided in the docs subfolder of the Android installation directory (as shown in Figure 2.4).

Image

Figure 2.4 The Android SDK documentation viewed offline.

Exploring the Core Android Application Framework

The Android application framework is provided in the android.jar file. The Android SDK is made up of several important packages, which are listed in Table 2.1.

Image

Table 2.1 Important Packages in the Android SDK

Several optional third-party APIs are available outside the core Android SDK. These packages must be installed separately from their respective websites. Some packages are from Google, whereas others are from device manufacturers and other providers. Some of the most popular third-party APIs are described in Table 2.2.

Image

Image

Table 2.2 Popular Third-Party Android APIs

Exploring the Core Android Tools

The Android SDK provides many tools to design, develop, debug, and deploy your Android applications. For now, we want you to focus on familiarizing yourself with the core tools you need to know about to get up and running with Android applications. We discuss many Android tools in greater detail in Appendix A, “Mastering the Android Development Tools.”

The Android IDE and ADT

You’ll spend most of your development time in your IDE. This book assumes you are using the Android IDE that comes with the ADT Bundle, because this is the official development environment configuration.

ADT incorporates many of the most important Android SDK tools seamlessly into the Android IDE and provides various wizards for creating, debugging, and deploying Android applications. ADT adds a number of useful functions to the Android IDE. Several buttons are available on the toolbar, including buttons to perform the following actions:

Image Launch the Android SDK Manager

Image Launch the Android Virtual Device Manager

Image Run Android lint

Image Create a new Android XML resource file

The Android IDE organizes its workspace into perspectives (each a set of specific panes) for different tasks such as coding and debugging. You can switch between perspectives by choosing the appropriate tab in the top-right corner of the Android IDE environment. The Java perspective arranges the appropriate panes for coding and navigating around the project. The Debug perspective enables you to set breakpoints, view LogCat information, and debug. ADT adds several special perspectives for designing and debugging Android applications. The Hierarchy View perspective integrates the Hierarchy Viewer tool into the Android IDE so that you can design, inspect, and debug user interface controls within your applications. The DDMS perspective integrates the Dalvik Debug Monitor Server (DDMS) tool into the Android IDE so that you can attach to emulator and device instances and debug your applications. Figure 2.5 shows the Android IDE toolbar with the Android features (left) and the Android perspectives (right).

Image

Figure 2.5 Android features and perspectives on the Android IDE toolbar.

You can switch perspectives within the Android IDE by choosing Window, Open Perspective or by clicking the appropriate perspective tab in the top-right corner of the Android IDE toolbar.

Android SDK and AVD Managers

The first Android toolbar icon with the little green Android and the down arrow will launch the Android SDK Manager (see Figure 2.6, top). The second Android toolbar icon, looking like a tiny phone, will launch the Android Virtual Device Manager (see Figure 2.6, bottom).

Image

Figure 2.6 The Android SDK Manager (top) and Android Virtual Device Manager (bottom).

These tools perform two major functions: management of Android SDK components installed on the development machine and management of the developer’s AVD configurations.

Much like desktop computers, different Android devices run different versions of the Android operating system. Developers need to be able to target different Android SDK versions with their applications. Some applications target a specific Android SDK, whereas others try to provide simultaneous support for as many versions as possible.

The Android SDK Manager facilitates Android development across multiple platform versions simultaneously. When a new Android SDK is released, you can use this tool to download and update your tools while still maintaining backward compatibility and use older versions of the Android SDK.

The Android Virtual Device Manager organizes and provides tools to create and edit AVDs. To manage applications in the Android emulator, you must configure different AVD profiles. Each AVD profile describes what type of device you want the emulator to simulate, including which Android platform to support as well what the device specifications should be. You can specify different screen sizes and orientations, and you can specify whether the emulator has an SD card and, if so, what its capacity is, among many other device configuration settings.

Android Emulator

The Android emulator is one of the most important tools provided with the Android SDK. You will use this tool frequently when designing and developing Android applications. The emulator runs on your computer and behaves much as a mobile device would. You can load Android applications into the emulator, test, and debug them.

The emulator is a generic device and is not tied to any one specific phone configuration. You describe the hardware and software configuration details that the emulator is to simulate by providing an AVD configuration. Figure 2.7 shows what the emulator might look like with a typical Android 4.3 smartphone-style AVD configuration.

Image

Figure 2.7 The Android emulator (smartphone-style, Jelly Bean AVD configuration).

Figure 2.8 shows what the emulator might look like with a typical Android 4.3 tablet-style AVD configuration. Both Figures 2.7 and 2.8 show how the popular Gallery application behaves differently on different devices.

Image

Figure 2.8 The Android emulator (tablet-style, Nexus 7 Jelly Bean AVD configuration).


Image Tip

You should be aware that the Android emulator is a substitute for a real Android device, but it’s an imperfect one. The emulator is a valuable tool for testing but cannot fully replace testing on actual target devices.


Exploring the Android Sample Applications

The Android SDK provides many samples and demo applications to help you learn the ropes of Android development. These demo applications are not provided as part of the Android SDK by default, but you can download them for different API levels using the Android SDK Manager. Once downloaded, they will be located in the /samples subdirectory of the Android SDK.


Image Tip

To learn how to download the Android SDK sample applications using the Android IDE, read the section titled “Adding the Android Samples Using the SDK Manager” in Chapter 3, “Writing Your First Android Application.”


More than five dozen sample applications are available to demonstrate different aspects of the Android SDK. Some focus on generic application development tasks such as managing application lifecycle or user interface design.

Some of the most straightforward demo applications to take a look at are

Image ApiDemos: a menu-driven utility that demonstrates a wide variety of Android APIs, from user interface widgets to application lifecycle components such as services, alarms, and notifications

Image Snake: a simple game that demonstrates bitmap drawing and key events

Image NotePad: a simple list application that demonstrates database access and Live Folder functionality

Image LunarLander: a simple game that demonstrates drawing and animation

Image SkeletonApp and SkeletonAppTest: a simple application that demonstrates accepting text input

Image Spinner and SpinnerTest: a simple application that demonstrates some application lifecycle basics, as well as how to create and manage application test cases using the unit test framework

Image TicTacToeMain and TicTacToeLib: a simple application that demonstrates how to create and manage shared code libraries for use with your applications

There are numerous other sample applications that cover specific topics, but they demonstrate Android features that are discussed later in this book. Some other sample apps are found only online. Some of the most useful online sample applications to take a look at are listed here:

Image Support4Demos: a sample application that demonstrates the key features in the Android API Level 4+ Support Library, including fragments and loaders

Image Support7Demos: a sample application that demonstrates the key features in the Android API Level 7+ Support Library, including support for ActionBar, GridLayout, and MediaRouter (API Level 18) for Google Cast

Image Support13Demos: a sample application that demonstrates the key features in the Android API Level 13+ Support Library, including enhanced Fragment support

Image SupportAppNavigation: a sample application that demonstrates the key features of app navigation for use with the support libraries


Image Tip

We discuss how to set up your development environment for using the sample applications in Chapter 3, “Writing Your First Android Application.” Once the environment is set up, to add a sample project within the Android IDE or Eclipse, select File, New, Other, and then under Android, choose Android Sample Project. The sample applications available depend on which specific build target is chosen. Proceed as you normally would, by creating a Run or Debug configuration, compiling and running the application in the emulator or on a device. You will see these steps performed in detail in the next chapter when you test your development environment and write your first application.


Summary

In this chapter, you installed, configured, and began to explore the tools you need to start developing Android applications, including the appropriate JDK, the Android SDK, and the Android IDE or ADT Bundle. You also learned that there are alternative development environments you can choose from, such as Android Studio. You learned how to configure your Android hardware for debugging. Further, you explored many of the tools provided along with the Android SDK and understand their basic purposes. Finally, you perused the sample applications provided along with the Android SDK. You should now have a reasonable development environment configured to write Android applications. In the next chapter, you’ll be able to take advantage of all this setup and write an Android application.

Quiz Questions

1. What version of the Java JDK is required for Android development?

2. What security option must be selected on an Android hardware device for installing your own applications without using an Android marketplace?

3. What option must be enabled on your hardware device to debug your applications?

4. What is the name of the .jar file that comprises the Android application framework?

5. What is the top-level package name for unit-testing support?

6. Which optional Android SDK is provided by Google for integrating advertising in your applications?

Exercises

1. Open a local copy of the Android documentation provided with the Android SDK.

2. Launch the Android SDK Manager and install at least one other version of Android.

3. Name five other sample applications provided with the Android SDK that were not mentioned in this chapter.

References and More Information

Google’s Android Developers Guide:

http://d.android.com/guide/components/index.html

Android SDK download site:

http://d.android.com/sdk/index.html

Android SDK License Agreement:

http://d.android.com/sdk/terms.html

The Java Platform, Standard Edition:

http://oracle.com/technetwork/java/javase/overview/index.html

The Eclipse Project:

http://eclipse.org