2. Setting Up for Development

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’ll become familiar with all the tools you need to master in order to develop Android applications. You’ll learn information about configuring your development environment both on a virtual device and on real hardware. You’ll also explore the Android SDK and all it has to offer.


Image Note

The Android SDK and Android Studio 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’s 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 7, 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). If you are developing on Mac OS X, you should use the Java Runtime Environment (JRE), Version 6, to run Android Studio, and then configure your projects to run JDK 7.

Image The latest Android SDK. In this book, we will cover using the Android SDK included with Android Studio, which is available for Windows, Mac, or Linux and can be downloaded at http://d.android.com/sdk/index.html. Android Studio includes everything you will need for working on the examples in this book and for developing Android applications. Other items included with Android Studio 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, Android Studio is provided, and is based on the free Community Edition of IntelliJ IDEA, and IDE for Java Development. Android Studio comes with the most recent Android SDK already installed. This book focuses on using Android Studio. An alternative to using Android Studio would be to use IntelliJ IDEA Community or Ultimate Edition, or your own copy of Eclipse, but unfortunately, the Eclipse Android Developer Tools plugin is no longer supported so we can only recommend that you use Android Studio for this reason.

An up-to-date and complete list of Android development system requirements is available at http://d.android.com/sdk/index.html.


Image Tip

Developers should use Android Studio for Android development, as this is the official IDE for Android application development. The JetBrains development team has integrated the Android development tools directly into Android Studio. Although previous editions of this book covered how to develop Android applications with Eclipse, this edition will only cover using Android Studio. For information on using Android Studio, read the overview at http://d.android.com/tools/studio/index.html.

For information on managing your projects from the command-line, begin by reading http://d.android.com/tools/projects/projects-cmdline.html; that link discusses using the command-line tools, which may be useful for developing with other environments. You may download the stand-alone SDK tools here: http://d.android.com/sdk/index.html#Other. 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 JRE/JDK for your operating system.

2. Download and install or unzip the appropriate Android Studio package for your operating system. When installing Android Studio, make sure to select all the available components for your system setup, as seen on the Choose Components dialog of the Android Studio Setup wizard seen in Figure 2.1. The last component, Performance (Intel® HAXM) may not be available on your device for installation, but don’t worry if that particular component is not available. Make sure the Android SDK and the Android Virtual Device are selected for installation.

Image

Figure 2.1 The Choose Components dialog of the Android Studio Setup installation wizard.

3. Launch Android Studio and use the Android SDK Manager to download and install specific Android platform versions and other components you might use, including the documentation, USB drivers, and additional tools. The Android SDK Manager has been integrated directly into Android Studio and is also available as a stand-alone tool accessible from within Android Studio. In terms of which components you’ll want to choose for installing, we recommend a full installation (choose everything).

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

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

6. 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 these main reasons:

Image This is an intermediate/advanced book, and we expect you to have some familiarity with installing Java development tools and SDKs.

Image The Android Developer website provides fairly extensive information about installing development tools and configuring them on a variety of different operating systems. Instructions for installing Android Studio are available at http://d.android.com/sdk/installing/index.html?pkg=studio and instructions for installing the stand-alone SDK are available at http://d.android.com/sdk/installing/index.html?pkg=tools.

Image The exact steps required to install the Android SDK tend to change subtly with each release, so you’re always better off checking the Android Developer website for the latest information.

Keep in mind that the Android SDK, Android Studio, and tools are updated frequently and may not exactly match the development environment used in this book, as defined in the Introduction, and may look different on your machine than what has been presented in this book. 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 Android Studio in step 2 of the basic installation process listed above. 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 device via the USB cable (see Figure 2.2). On some operating systems, such as Mac OS X, this may just work automatically. However, for Windows installations, you need to install the appropriate USB driver. You can learn how to download and install the Windows USB driver for any Google Nexus device from the following link: http://d.android.com/sdk/win-usb.html. If you are using the Galaxy Nexus device, you may download the drivers here: http://www.samsung.com/us/support/owners/product/GT-I9250TSGGEN. For other manufacturer’s devices, you can learn more about the appropriate USB drivers here: http://d.android.com/tools/extras/oem-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.2 Android application debugging using an IDE and the device emulator on a development machine and an Android handset connected to a development machine.

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.2+, 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 navigating to Settings, then choosing 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,” where X is the quantity of presses you have remaining. 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’ll 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, then choosing Security. Here, you should check (enable) the option called Unknown sources, as shown in Figure 2.3. 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.3 Enabling Unknown sources on the device.

Several other important development settings are available on the Android device by selecting Settings, and then Developer options (see Figure 2.4).

Image

Figure 2.4 Enabling Android developer settings on the device.

Here, you should enable the USB debugging option. This setting enables you to debug your applications via the USB connection.

Upgrading Android Studio

Android Studio is updated fairly frequently. By default, Android Studio is configured to check for updates to the IDE automatically. When there is an update, you will be notified of the new update, and prompted to install the update upon opening the IDE. You may also configure from which channel to install the update. The options are: Stable Channel, Beta Channel, Dev Channel, and Canary Channel. Unless you already know what the other channels are for, you should only consider installing from the default update channel named Stable Channel, which has features that are known to be stable.

The Beta Channel is reserved for installing features that are near the stable, beta features of Android Studio. The Dev Channel is for installing features that were once bleeding edge features that have survived the cut. The Canary Channel is for installing the most recent bleeding edge features. With that said, expect to run into many bugs and issues if you decide to use the Canary Channel.

Upgrading the Android SDK

The Android SDK is upgraded from time to time. You can easily upgrade the Android SDK platform and tools from within Android Studio using the built-in SDK Manager or by launching the stand-alone SDK Manager, which is installed as part of the Android SDK.

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 5.1 information is available at http://d.android.com/about/versions/android-5.1.html, and Android 5.0 information is available at http://d.android.com/about/versions/android-5.0.html.

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

Problems with Android Studio

When developing with the Stable Channel of Android Studio, you may not experience any bugs during development, but even with stable software, an occasional bug may creep in. On the other hand, when you use any of the other channels of Android Studio, the odds of encountering a bug increase. Luckily, Android Studio has a reporting mechanism built into the IDE. When such an error occurs, you will receive a notification informing you that you have encountered an error. If you follow the notification instructions, you will be led to a dialog for submitting the bug report to JetBrains. Submitting bug reports not only helps JetBrains and the Android developer community, but it will also benefit you when JetBrains is made aware of any issues encountered, so that it may follow up with a fix. You may submit these reports anonymously or through your JetBrains account if you have one.

Problems with the Android SDK

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 for consideration 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 Android SDK 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.


IntelliJ IDEA as an Android Studio Alternative

Because Android Studio is based on the Community Edition of IntelliJ IDEA, you may also use the Community or Ultimate Edition of IntelliJ IDEA for developing Android applications. Both of these IDEs support Android application development. There is not much of an advantage to using IntelliJ IDEA Community Edition over Android Studio if you only program with the Android SDK. If that is the case, you are better off using Android Studio.

However, IntelliJ IDEA Ultimate Edition may be worth considering if the project you are working on involves other aspects such as Web development or backend server development using languages like Python, Ruby, PHP, HTML, CSS, or JavaScript, or if you use Web frameworks such as Spring, GWT, Node.js, Django, Rails, or others. Having a single IDE may be beneficial, rather than having to manage a single project across many different IDEs. If you find your project and job duties extend beyond just Android development, take a look at IntelliJ IDEA Ultimate.

IntelliJ IDEA Community Edition is free to download and use, but the Ultimate Edition requires purchasing a commercial or personal license. JetBrains does offer special pricing to qualifying startups, and offers Ultimate for free to qualifying students and teachers, open-source projects, and for education and training purposes. To learn more about IntelliJ IDEA or pricing information, please see https://www.jetbrains.com/idea. For a full list of the differences between the Community and Ultimate Edition of IntelliJ, see https://www.jetbrains.com/idea/features/editions_comparison_matrix.html.

Exploring the Android SDK

The Android SDK comes with several major components: the Android SDK Platform by version, SDK Platform Tools, SDK Build Tools, System Images, Google APIs, Sources for Android SDK, extras, and sample applications.

Understanding the Android SDK License Agreement

Before you can download Android Studio, 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 the 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 and 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 accessing what the user has explicitly granted permission for during installation time.

Image Develop at your own risk: Any harm, financial or otherwise, 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 (see Figure 2.5).

Image

Figure 2.5 The Android SDK documentation viewed offline.

Exploring the Core Android Application Framework

The Android SDK platform is provided in the android.jar file. This file 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 or from within the Android SDK Manager when available. 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 Just a Few Popular Third-Party Android APIs

For a complete list of Google Play services APIs, see https://developers.google.com/android/guides/setup. For a complete list of all Google products that may have Android APIs, see https://developers.google.com/products/.

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 D, “Mastery: Android SDK Tools.”

Android Studio

You’ll spend most of your development time in your IDE. This book assumes you are using Android Studio because this is the official development environment configuration.

Android Studio incorporates many of the most important Android SDK tools seamlessly and provides various wizards for creating, debugging, and deploying Android applications. The Android SDK tools add useful functions to Android Studio. Buttons are available on the toolbar, including those that perform the following Android actions (see Figure 2.6):

Image Launch the Android Virtual Device Manager

Image Launch the Android SDK Manager

Image Launch the Android Device Monitor

Image

Figure 2.6 Android features on the Android Studio toolbar.

Android SDK and AVD Managers

In Figure 2.6, note the left-most icon within the red border that looks like a tiny phone with an Android head in the lower-right corner; this will launch the Android Virtual Device Manager (see Figure 2.7). The second Android toolbar icon within the red border, with the little green Android head at the top and the down arrow, will launch the built-in Android SDK Manager (see Figure 2.8) that also includes a link to launch the stand-alone SDK Manager (see Figure 2.9).

Image

Figure 2.7 The Android Virtual Device Manager.

Image

Figure 2.8 The built-in Android SDK Manager shows the currently installed Android SDK Platforms (top) and SDK Tools (bottom).

Image

Figure 2.9 The stand-alone Android SDK Manager.

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

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 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 as 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.

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.

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.10 shows what the emulator might look like with a typical Android 5.1 smartphone-style AVD configuration.

Image

Figure 2.10 The Android emulator (Nexus 5 smartphone-style, Android API 22 AVD configuration).

Figure 2.11 shows what the emulator might look like with a typical Android 5.1 tablet-style AVD configuration. Both Figures 2.10 and 2.11 show how the Settings application behaves differently on different devices.

Image

Figure 2.11 The Android emulator (Nexus 9 tablet-style, Android API 22 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. Android Studio provides a dialog for importing the sample applications—from GitHub—as a project.


Image Tip

To learn how to import an Android SDK sample application using Android Studio, in Chapter 3, “Creating Your First Application,” read the section titled “Importing the BorderlessButtons Sample into Android Studio.”


Many sample applications are available to demonstrate different aspects of the Android SDK. Some focus on generic application development tasks while others focus on demonstrating specific APIs.

As of API 23, some of the sample code that you should look into further is categorized as follows:

Image Getting started: includes samples demonstrating common Android components such as the action bar, floating action button, and more

Image Background: includes samples demonstrating common tasks that should be run in the background

Image Input: includes samples demonstrating common input methods such as gestures, multitouch, and swipes

Image Media: includes samples demonstrating media-related features such as the camera, recording, effects, and others

Image Connectivity: includes samples demonstrating various networking methods such as Bluetooth and HTTP

Image Notification: includes samples demonstrating various Notification APIs

Image Wearable: includes many samples demonstrating the various features available to developers for wearable devices

The examples above are just a few of the many categories of applications that are available as samples that demonstrate various features of the Android APIs.


Image Tip

We discuss how to import a sample application in Chapter 3, “Creating Your First Application.” Once Android Studio is installed, to add a sample project from the “Welcome to Android Studio” screen, select Import an Android Code Sample, select the sample application to import, click Next, then click Finish and you should now be in the editor with your sample project ready for editing. Proceed as you normally would, 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 will need to start developing Android applications, including the appropriate JDK, the Android SDK, and Android Studio. You also learned that there are alternative development environments you can choose from, such as IntelliJ IDEA Community or Ultimate Edition. You learned how to configure your Android hardware for debugging. In addition, you explored many of the tools provided with the Android SDK and now understand their basic purposes. Finally, you perused the sample applications provided 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 Studio SDK Manager of Android Studio and install at least one other version of Android.

3. Name five sample applications provided with the Android SDK.

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

JetBrains:

https://www.jetbrains.com/

Android Developer Tools:

https://developer.android.com/tools/help/adt.html

The Eclipse Project:

http://eclipse.org

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

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