Before You Begin

In this section, you’ll set up your computer for use with this book. Google frequently updates the Android development tools, so before reading this section, check

to see if we’ve posted an updated version of this Before You Begin section.

Software and Hardware System Requirements

To develop Android apps, you need a Windows®, Linux® or Mac® OS X® system. To view the latest operating-system requirements visit

We developed the apps in this book using the following software:

• Java SE Software Development Kit (JDK)

• Android Studio 1.4 Integrated Development Environment (IDE)

• Android 6 SDK (API 23)

You’ll see how to obtain each of these in the following sections.

Installing the Java Development Kit (JDK)

Android versions 5 and higher require the Java Development Kit version 7 (JDK 7). All Java language features in JDK 7 are supported in Android Studio, however the try-with-resources statement is supported only for Android platform versions with API levels 19 (Android 4.4) and higher. To download JDK 7 for Windows, OS X or Linux, go to

Choose the appropriate 32-bit or 64-bit version for your computer hardware and operating system. Be sure to follow the installation instructions at

Android does not yet support Java 8 language features, such as lambda expressions, new interface features and the stream APIs. You can use JDK 8 (as we did when developing this book’s apps), provided that you do not use any Java 8 language features in your code.

Setting the PATH Environment Variable

The PATH environment variable on your computer designates which directories the computer searches when looking for applications, such as the applications that enable you to compile and run your Java applications (called javac and java, respectively). Carefully follow the installation instructions for Java on your platform to ensure that you set the PATH environment variable correctly. The steps for setting environment variables differ by operating system and sometimes by operating system version (e.g., Windows 7 vs. Windows 10). Instructions for various platforms are listed at:

If you do not set the PATH variable correctly on Windows and some Linux installations, when you use the JDK’s tools, you’ll receive a message like:

'java' is not recognized as an internal or external command,
operable program or batch file.

In this case, go back to the installation instructions for setting the PATH and recheck your steps. If you’ve downloaded a newer version of the JDK, you may need to change the name of the JDK’s installation directory in the PATH variable.

JDK Installation Directory and the bin Subdirectory

The JDK’s installation directory varies by platform. The directories listed below are for Oracle’s JDK 7 update 80 (the most current version at the time of this writing):

• 32-bit JDK on Windows:
C:Program Files (x86)Javajdk1.7.0_80

• 64-bit JDK on Windows:
C:Program FilesJavajdk1.7.0_80

• Mac OS X:
/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home

• Ubuntu Linux:
/usr/lib/jvm/java-7-oracle

The JDK installation folder’s name might differ if you’re using a different update of JDK 7 or using JDK 8. For Linux, the install location depends on the installer you use and possibly the version of Linux that you use. We used Ubuntu Linux.

The PATH environment variable must point to the JDK installation directory’s bin subdirectory. When setting the PATH, be sure to use the proper installation-directory name for the version of the JDK you installed. As JDK updates become available, the installation-directory name changes to include the updated version number.

Setting the CLASSPATH Environment Variable

If you attempt to run a Java program and receive a message like

Exception in thread "main" java.lang.NoClassDefFoundError: YourClass

then your system has a CLASSPATH environment variable that must be modified. To fix the preceding error, follow the steps in setting the PATH environment variable to locate the CLASSPATH variable, then edit the variable’s value to include the local directory—typically represented as a dot (.). On Windows add

.;

at the beginning of the CLASSPATH’s value (with no spaces before or after these characters). On other platforms, replace the semicolon with the appropriate path separator characters—typically a colon (:).

Installing Android Studio

Android Studio comes with the latest Android Software Development Kit (SDK) and is based on the popular Java IDE from JetBrains called IntelliJ® IDEA. To download, go to

and click the Download Android Studio button. When the download completes, run the installer and follow the on-screen instructions to complete the installation. If you previously installed an earlier Android Studio version, a Complete Installation window will appear at the end of the install process and give you the option to import your previous settings. At the time of this writing, Android Studio 1.4 is the current released version and Android Studio 1.5 is available as an early access release.

Configure Android Studio to Show Line Numbers

By default, Android Studio does not show line numbers next to the code that you write. To turn on line numbers to make it easier to follow our line-numbered code examples:

1. Open Android Studio (Image).

2. When the Welcome to Android Studio window appears, click Configure, then click Settings to open the Default Settings window. If the Welcome to Android Studio window does not appear, use the menus on Mac to select Android Studio > Preferences... or on Windows/Linux to select File > Other Settings > Default Settings....

3. Expand the Editor > General node and select Appearance, then ensure that Show line numbers is selected and click OK.

Configure Android Studio to Disallow Code Folding

By default, Android Studio’s code-folding feature is enabled. This feature collapses multiple lines of code into a single line so you can focus on other aspects of the code. For example, all the import statements in a Java source-code file can be collapsed into a single line to hide them, or an entire method can be collapsed into a single line. You can expand these lines if you need to look at the code in detail. We disabled this feature in our IDE. If you wish to do so, follow the steps in the preceding section, then under Editor > General > Code Folding uncheck Show code folding outline.

Android 6 SDK

This book’s code examples were written using Android 6. At the time of this writing, the Android 6 SDK was bundled with Android Studio. As new Android versions are released, the latest version will be bundled, which may prevent our apps from compiling properly. When you work with this book, we recommend using Android 6. You can install prior Android platform versions as follows:

1. Open Android Studio (Image).

2. When the Welcome to Android Studio window appears, click Configure, then click SDK Manager to display the Android SDK manager. If a project window appears rather than the Welcome to Android Studio window, you can access the Android SDK manager via Tools > Android > SDK Manager.

3. In the SDK Platforms tab, check the versions of Android you wish to install, then click Apply and OK. The IDE then downloads and installs the additional platform versions. The IDE also will help you keep your installed versions up to date.

Creating Android Virtual Devices (AVDs)

The Android SDK’s Android emulator allows you to test apps on your computer rather than on an Android device—this is essential, of course, if you do not have Android devices. To do so, you create Android Virtual Devices (AVDs) that run in the emulator. The emulator can be slow, so most Android developers prefer testing on actual devices. Also, the emulator does not support various features, including phone calls, USB connections, headphones and Bluetooth. For the latest emulator capabilities and limitations, visit

That page’s Using Hardware Acceleration section discusses features that can improve emulator performance, such as using the computer’s graphics processing unit (GPU) to increase graphics performance, and using the Intel HAXM (hardware accelerated execution manager) emulator to increase overall AVD performance. There are also faster third-party emulators, such as Genymotion.

After you’ve installed the Android Studio and before you run an app in the emulator, you must create at least one Android Virtual Device (AVD) for Android 6. Each AVD defines the characteristics of the device you wish to emulate, including

• its screen size in pixels

• its pixel density

• its screen’s physical size

• the size of the SD card for data storage

• and more.

To test your apps for multiple Android devices, you can create AVDs that emulate each unique device. You also can use Google’s new Cloud Test Lab

a website that will enable you to upload your app and test it on many of today’s popular Android devices. By default, Android Studio creates for you one AVD that’s configured to use the version of Android bundled with the IDE. For this book, we use AVDs for two of Google’s Android reference devices—the Nexus 6 phone and the Nexus 9 tablet—which run standard Android without the modifications made by many device manufacturers. It’s easiest to create AVDs in Android Studio once you already have a project open in the IDE. For this reason, we’ll show how to create the Android 6 AVDs in Section 1.9.

Setting Up an Android Device for Testing Apps

Testing apps on Android devices tends to be quicker than using AVDs. In addition, recall that there are some features you can test only on actual devices. To execute your apps on Android devices, follow the instructions at

If you’re developing on Microsoft Windows, you’ll also need the Windows USB driver for Android devices that you installed earlier in this Before You Begin section. In some cases on Windows, you may also need the manufacturer’s device-specific USB drivers. For a list of USB driver sites for various device brands, visit

Downloading the Book’s Code Examples

The source code for Android How to Program, 3/e is available for download at

Click the Download Code Examples link to download a ZIP archive file containing the examples to your computer. Depending on your operating system, double click the ZIP file to unzip the archive or right click and select the option to extract the archive’s contents. Remember where the extracted files are located on your system so you can access them later.

A Note Regarding Android Studio and the Android SDK

If you import one of our apps into Android Studio and receive an error message, this could be the result of updates to Android Studio or the Android platform tools. For such issues, please check Android questions and answers on StackOverflow at:

and the Google+ Android Development community at:

or write to us at

You’ve now installed all the software and downloaded the code examples you’ll need to study Android app development with Android How to Program, 3/e and to begin developing your own apps. Enjoy!

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

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