Chapter 2
In This Chapter
Becoming an Android application developer
Collecting your tools of the trade
Downloading and installing the Android software development kit (SDKs)
Getting and configuring Android Studio
Working with the Android development tools
All the software that you need to develop Android applications is free. That’s where the beauty of developing Android applications lies. The basic building blocks you need to develop rich Android applications — the tools, the frameworks, and even the source code — are free. No one gives you a free computer, but you get to set up your development environment and start developing applications for free, and you can’t beat free. Well, maybe you can — if someone pays you to write an Android application, but you’ll reach that. This chapter walks you through the necessary steps to install the tools and frameworks so that you can start building kick‐butt Android applications.
Becoming an Android developer isn’t a complicated task. And it’s likely simpler than you believe. To see what’s involved, ask yourself these questions:
If you answered yes to every question, today is your lucky day — you’re ready to become an Android developer.
There’s always a catch, right? You can develop for free to your heart’s content, but as soon as you want to publish your application to the Google Play Store, where you upload and publish your apps, you need to pay a small, nominal registration fee. At this writing, the fee is $25.
After you know that you’re ready to be an Android developer, grab your computer and get cracking on installing the tools and frameworks necessary to build your first blockbuster application.
Android was created on top of the open source Linux kernel. The Android team chose to use this kernel because it provided proven core features on which to develop the Android operating system. The features of the Linux kernel include (but aren’t limited to)
Atop the Linux kernel, the Android framework was developed with various features. These features were pulled from numerous open source projects. The output of these projects resulted in these elements:
See Figure 2-1 for a list of common Android libraries.
If you’ve read the preceding section, you may say, “Well, that’s all nice and well, but how do these libraries affect me as a developer?” It’s simple: All these open source frameworks are available to you via Android. You don’t have to worry about how Android interacts with SQLite and the surface manager; you use them as tools in your Android tool belt.
The Android team has built on a known set of proven libraries, built in the background, and has given them to you, all exposed through Android interfaces. These interfaces wrap up the various libraries and make them useful to the Android platform and to you as a developer. You benefit from these features because you don’t have to build any of the functionality they provide. Some of these interfaces include
Take a look at Figure 2-2 to see the libraries that make up the application framework.
From kernel to application, the Android operating system has been developed with proven open source technologies. You, as a developer, can therefore build rich applications that have been fostered in the open source community. See Figure 2-3 for a full picture of how the Android application framework stacks up. The Applications section is where your application sits.
The Java programming language is one of the glorious tools that make programming Android a breeze compared with programming for other mobile platforms. Whereas other languages insist that you manage memory, allocate and de‐allocate bytes, and then shift bits around like a game of dominoes, the Java runtime helps take care of that for you. The Java runtime allows you to focus on writing code to solve a business problem by using a clean, understandable programming language (or to build that next cool first‐person shooter game you’ve been dreaming of) instead of focusing on the “plumbing” just to get the screens to show up.
You can develop Android applications on various operating systems, including Windows, Linux, and Mac OS X. In this book, you find a combination of the Windows 8 operating system and Mac OS X, but you can use Linux as well.
Android supports these platforms:
c:path ofile.txt
Some examples use Mac OS X; a Mac or Linux path looks similar to this:
/path/to/file.txt
Before you start installing the required software, make sure that your computer can run it adequately. Just about any desktop or laptop computer manufactured in the past four years will suffice. A computer with 4 or 8 gigabytes (GB) of RAM should work just fine.
To ensure that you can install all the tools and frameworks you’ll need, make sure that you have enough hard drive space to accommodate them. The Android developer site has a list of hardware requirements, outlining how much hard drive space each component requires, at
.https://developer.android.com/sdk/installing/studio.html
It’s time to put these exciting Android concepts into action, but before you can do so, you need to install and configure a few tools, including the SDKs:
The following sections show you how to acquire and install all these tools.
To download Android Studio, first go to
and download Android Studio. Then go through the following steps to get to the Android Studio boot screen, as in Figure 2-4:https://developer.android.com/sdk/installing/studio.html
If you’re a Mac user, follow these steps to download Android Studio:
Open the Applications folder and double‐click Android Studio.
Depending on your security settings, when you attempt to open Android Studio, you might see a warning that says the package is damaged and should be moved to the trash. If this happens, choose System Preferences⇒Security & Privacy and, under Allow applications downloaded from, select Anywhere. Then open Android Studio again.
Depending on your Mac, you may be asked to install Java at this point.
Click OK and Java will be automatically installed for you.
If you’re a Windows user, follow these steps to download Android Studio:
Open the Start screen and launch Android Studio.
On some Windows systems, the launcher script does not find where Java is installed. If you encounter this problem, you need to set an environment variable indicating the correct location.
Choose Start menu⇒Computer⇒System Properties⇒Advanced System Properties. Then choose Advanced tab⇒Environment Variables and add a new system variable, JAVA_HOME, that points to your JDK folder — for example, C:ProgramFilesJavajdk1.8.0_20.
If you do not have the Java JDK installed, see the next section.
On some systems, you may see an error that the system cannot find the Java 7 JDK.
If this happens, visit
to download the Java 7 JDK appropriate for your machine. Install it, then run Android Studio again.www.oracle.com/technetwork/java/javase/downloads/jdk7‐downloads‐1880260.html
Now that you have Android Studio installed, you need to make sure that you download all the latest SDK components. The first time you launch Android Studio, it may install part of the SDK for you, but you need to install the full SDK using the following steps:
Now hum the theme from Jeopardy.
Once this is done, do it again. No seriously. Click New to select all the new packages, accept the license agreements again, and click Install again. You shouldn’t have to do it more than twice, but heck, check it a third time while you’re at it.
Now that you’ve installed the Android SDK, take a look inside the SDK folder:
On a Mac, open the Terminal app in Applications⇒Utilities (or by searching for “Terminal” in Spotlight), then type cd "/Applications/Android Studio.app/sdk". On Windows, open the Start menu and search for “cmd” to launch a command prompt, then type cd "Users<user> AppDataLocalAndroidandroid‐studiosdk" where <user> is your username.
Whoa — you’ll find a lot of folders in the SDK! Don’t worry: The folder structure of the Android SDK is easy to understand when you get the hang of it. You need to understand the structure of the SDK to master it. Table 2-1 outlines the contents of each folder.
Table 2‐1 Folders in the Android SDK
SDK Folder |
What It Contains |
tools, build‐tools, and platform‐tools |
Various tools that are available for use during development — such as for debugging, view management, and building. |
platforms |
The platforms you target when you build Android applications, such as folders named android‐16 (which is Android 4.1) and android‐8 (which is Android 2.2). |
extras and/or add‐ons |
Additional APIs that provide extra functionality. The Google APIs in this folder include mapping functionality. This folder remains empty until you install any of the Google Maps APIs. |
Android platform is a fancy way of saying Android version. At this writing, many versions of Android are available, ranging up through version 5.0. When we say “specifying an Android platform,” that means that we are developing our app so that it will work on devices running that specific Android version or later.
To view current platform statistics, visit
.http://d.android.com/resources/dashboard/platform‐versions.html
The SDK tools are the building blocks you use in developing Android apps. New features packed into every release enable you to develop for the latest version of Android.
Google provides not only the tools you need to develop apps but also an awesome little emulator to test your app. The emulator has some limitations (for example, it cannot emulate certain hardware components, such as the accelerometer) but not to worry — plenty of apps can be developed and tested using only an emulator.
When you’re developing an app that uses Bluetooth, for example, you should use a physical device that has Bluetooth on it. If you develop on a speedy computer, testing on an emulator is fast; on slower machines, however, the emulator can take a long time to complete a seemingly simple task. If you’re developing on an older machine, use a physical device. When you’re developing on a newer, faster machine, use the emulator.
The emulator is handy for testing apps at different screen sizes and resolutions. It isn’t always practical or possible to have several devices connected to your computer at the same time, but you can run multiple emulators with varying screen sizes and resolutions.
If you develop on a Windows machine and you want to test your app on a real‐life device, you need to install a driver. If you’re on a Mac or Linux machine, you can skip this section because you don’t need to install the USB driver.
When you downloaded the SDK, you also downloaded the USB driver that you need. To install it, do the following:
Expand Other Devices (Figure 2-5), right‐click your device, and select Update Driver Software.
Select Browse my computer for driver software.
Type C:Users<user>AppDataLocalAndroidandroid‐studiosdkextrasgoogleusb_driver (replacing <user> with your username), and click Next.
If you can’t find the AppData directory on your computer, it’s because it’s hidden by default. What you can do is type %appdata% in the location field and then click Browse. That unhides the directory and allows you to navigate the rest of the way there.
The Android Device Monitor equips you with the necessary tools to find those pesky bugs, allowing you to go behind the scenes as your app is running to see the state of its hardware, such as the wireless radio. But wait — there’s more! The Device Monitor also simulates actions normally reserved for physical devices, such as sending global positioning system (GPS) coordinates manually, simulating phone calls, or simulating text messages. Get all the Device Monitor details at
.http://d.android.com/tools/help/monitor.html
The API and SDK samples are provided to demonstrate how to use the functionality provided by the API and SDK. If you’re ever stuck and can’t figure out how to make something work, visit
to find samples of almost anything, from using Bluetooth to making a two‐way text application or a 2D game.http://d.android.com/samples/
You also have a few samples in your Android SDK. Simply open the Android SDK and navigate to the samples directory, which contains various samples that range from interacting with services to manipulating local databases. Spend some time playing with the samples — the best way to learn to develop Android applications is to look at existing working code bases and then experiment with them in Android Studio.
The API demos inside the samples folder in the SDK are a collection of apps that demonstrate how to use the included APIs. You can find sample apps with a ton of examples, such as
If you get stuck or you simply want to prepare yourself for writing your next spectacular Android application, check out the complete details at
.http://d.android.com/samples/
98.84.25.165