Chapter 2
In This Chapter
Getting software tools for your development computer’s operating system
Installing the tools on your computer
Running the tools
There are two kinds of people — people who love tools, and people who don’t have strong feelings about tools. (As far as I know, no one dislikes tools.) I’m a tool lover because I enjoy the leverage that tools give me. With the right tool, I can easily do things that would otherwise require monumental effort. And I can do these things over and over again, getting better with practice using the tools so that the tasks I’m dealing with become easier as time goes on.
Of course, my tool-o-philia isn’t always a good thing. I’m not handy with skills like carpentry, car repair, or plumbing, but I can’t resist buying greasy old screwdrivers and other such tools at garage sales. Among other things, I have what I think is the world’s biggest monkey wrench, which I bought several years ago for only seven dollars. But I’d be useless (if not dangerous) using the wrench, so it sits in my attic waiting for my kids to deal with it when, years from now, they inherit my house full of junk.
But software tools are great. They’re not greasy; many good tools are free; and if you lose a tool, you can usually find it by searching your computer’s hard drive.
Anyway, this chapter is about Android development tools. Enjoy!
Java is the lingua franca of Android application development. To write Android apps, you normally use Java.
You can get your required dose of Java by visiting www.oracle.com/technetwork/java/javase/downloads
and getting the JDK for the Java Platform, Standard Edition (Java SE). Figure 2-1 shows me clicking a Download JDK button (circa October 2014) at the Oracle website.
In Figure 2-1, you can see that I’m shunning the two JRE buttons in favor of the JDK Download button.
Figure 2-2 shows you what I see after clicking the JDK Download button
In Figure 2-2, I select the 64-bit Windows download. Oracle’s website offers many more download alternatives. (In the “Many Faces of Java” section, I explain the reasons for these alternatives and help you make the right choice.).
After downloading a big file from the Oracle website, you double-click the file’s icon to start the installation. Here’s what happens with each of the major operating systems:
.exe
filename extension. When you double-click the .exe
file’s icon, a wizard guides you through the installation.Macintosh OS X: The downloaded file has the .dmg
filename extension. After double-clicking the .dmg
file’s icon, you see a .pkg
file’s icon. When you double-click the .pkg
file’s icon, a setup assistant guides you through the installation.
When you try to run software that’s not from the Apple’s App Store, you might get a “can’t be opened” message because the app “is from an unidentified developer.” To get past this stumbling block, Ctrl-click the app’s icon. Then, in the resulting context menu, select Open. When you do, the “can’t be opened” message turns into an “Are you sure you want to open it?” message. Click Open, and you’re on your way.
Linux:
What happens in Linux depends on your Linux distribution. Here’s what works on many of my Linux machines:
.rpm
. The .rpm
file ends up in the Downloads
directory — a subdirectory of my user home directory. So I open a Terminal window and type the following commands:
cd ~/Downloads
rpm -ivh name-of-the-downloaded-file.rpm
For distributions with no RPM package manager: I download the archive file whose name ends in .tar.gz
. The .tar.gz
file ends up in the Downloads
directory — a subdirectory of my user home directory. Then I extract the contents of the .tar.gz
archive file to a convenient place on my computer’s hard drive. If I’m feeling particularly geeky, I extract the contents by opening a Terminal window and typing the following commands:
cd
mkdir Development
cd ~/Downloads
tar zxvf name-of-the-downloaded-file.tar.gz
--directory ~/Development
Notice the backslash () in the next-to-last command line. When I end a command line with a backslash, the computer displays its own little prompt at the start of the next line. That’s okay. The little prompt means “You didn’t finish typing the
tar
command, so finish typing the command on this last line.”
If you’re a Linux user, you can try skipping the visit to Oracle’s website. Instead, issue the command sudo apt-get install default-jdk
(or the equivalent yum
command) in a Terminal window.
Make a note of the location on the hard drive where you’ve installed Java. I have a name for this place on my hard drive. I call it the JAVA_HOME directory. According to Figure 2-3, JAVA_HOME is C:Program FilesJavaJDK1.8.0
on my Windows computer. And, according to Figure 2-4, JAVA_HOME is /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
on my Mac.
That’s all you really need to know from this section. If you’re a Windows user and you’re happy with your Java installation, you can skip to the “Setting Up the Software.” If you’re a Mac user, go to the “Adding Apple’s Java to the Stew” section and then to the “Setting Up the Software.” But if you’re any kind of user and if you want more details about installing Java, read on.
To develop Android programs on a Mac, you need two flavors of Java — Oracle Java and Apple Java. You need OS X 10.5.8 or later, and your Mac must have an Intel processor. (The Android docs say that you can’t develop Android apps on a PowerPC Mac or on a Mac with OS X 10.5.7. Of course, for every hardware or software requirement, someone tries to create a workaround, or hack. Anyway, apply hacks at your own risk.)
Fortunately, installing Apple Java is easy. Search the web for Apple Java 6, and look for a web page in the apple.com
domain. As of October 2014, the best page seems to be support.apple.com/kb/dl1572
. Click the Download button on that page.
The downloaded file has the .dmg
filename extension. When you double-click the .dmg
file’s icon, you get a .pkg
file’s icon. When you double-click the .pkg
file’s icon, a setup assistant guides you through the installation.
When you visit www.oracle.com/technetwork/java/javase/downloads
, you find lots of options — too many options. Readers often send me email asking which options to select. Sad to say, the answer depends on your computer’s configuration. One way or another, keep a few things in mind:
There’s more than one kind of Java. The “official” Java comes from Oracle, but some organizations have created their own kinds of Java.
Here are some examples:
When you create an Android app, you use Google’s Android Java. Android Java comes originally from Apache Harmony, so Android Java isn’t quite the same as Oracle Java.
But here’s the strange thing: When you create an Android app, you use both Android Java and Oracle’s Java. You write your app’s code in Android Java, but to create the code, you use software tools that were written using Oracle Java. If you’re a Mac user, you also need Apple’s Java on your computer. Believe it or not, this doesn’t normally confuse people.
By the way, Google’s use of Android Java doesn’t have Oracle’s blessing. If you’re curious, search the web for info about the never-ending series of Oracle versus Google lawsuits.
The rest of this section deals almost exclusively with Oracle Java — the flavor that you find on the www.oracle.com/technetwork/java/javase/downloads
page.
Oracle Java comes in three separate editions.
A bit of background here: A programming language is a bunch of rules describing the way you can write instructions for the computer to follow. An application programming interface (API) is a bunch of reusable code for performing common tasks in a particular language. (Another name for an API is a library.)
Oracle Java has three official APIs. When you download Java from www.oracle.com
, you download some Java language tools and one of the three Java APIs. Taken together, the big bundle containing the language tools and one of the three APIs is called an edition of the Java Software Development Kit (SDK). The three available Java SDK editions are as follows:
Java Platform, Standard Edition (Java SE)
The Standard Edition has code for anything you can imagine doing on a single desktop computer, and much more. This edition does text-handling, mathematical calculations, input/output, collections of objects, and much more.
To develop Android apps, you want the Java Platform, Standard Edition.
Java Platform, Enterprise Edition (Java EE)
The Enterprise Edition has code for things you do on an industrial-strength server. This edition includes web server tools, sophisticated database tools, messaging between servers and clients, management of systems, and the entire kitchen sink.
Java Platform, Micro Edition (Java ME)
The Micro Edition has code for small devices, such as phones, TV set-top boxes, and Blu-ray players. This edition has limited capabilities that fit nicely into special-purpose devices that aren’t as powerful as today’s computers.
At first glance, the Micro Edition seems perfect for Android app development. But the creators of Android decided to bypass Java ME and create their own micro edition of sorts. In a way, the Android SDK is an alternative to Java ME. To be more precise, the Android SDK is both an alternative to Java ME and a user of the Java SE. (That is, the Android SDK defers to Java SE to perform some important jobs.)
The stewards of Java flip-flop between the names Java Software Development Kit and Java Development Kit (JDK). The two names are synonymous.
As you plow through various pieces of documentation, you see several uses of the acronym API. In this section, the Java API is a bunch of reusable code for performing common tasks with the Java programming language. In later sections, the Android libraries form the Android API — a bunch of reusable code for performing common tasks inside Android apps. To create Android apps, you need both APIs — the Java API and the Android API.
To develop Android apps, you want the Java Platform, Standard Edition. If you already have Java’s Enterprise Edition and you don’t want more stuff on your hard drive, the Enterprise Edition is okay. But the Enterprise Edition has much more than you need for developing Android apps, and the extra Enterprise features might confuse you. (I know this because the extra Enterprise features confuse me!)
For Windows and Linux, Oracle’s site provides two different Java downloads — one for the 32-bit word length, and another for the 64-bit word length.
You can spot the 64-bit downloads because they have the number 64 somewhere in their filenames or their descriptions. You can find the 32-bit downloads because they have x86 or i586 in their filenames or descriptions. (The characters x86 and i586 refer to model numbers for some older 32-bit processors.)
You can fuss all day deciding whether to download 32-bit Java or 64-bit Java. But instead of fussing, consider these guidelines:
Java comes in several different versions, with several updates to each version.
Java’s version numbering demonstrates what can happen when the marketing department disrupts the timeline in the space-time continuum. Java’s earliest releases were numbered “1.0.” Next came version “1.1,” and then the strangely named “Java 2, version 1.2.” The extraneous digit 2 hung around through “Java 2, version 1.3,” “Java 2, version 1.4,” and finally “Java 2, version 5.0.” (The spontaneous jump from 1.4 to 5.0 was lots of fun.)
Next up was “Java 6” (with no extra 2 and no “.0”). After that came Java 7. Each version is updated often, so a visit to www.oracle.com
may offer Java SE 8 Update 25 (Java SE 8u25) for download.
Any version of Java starting with Java 6 and onward is fine for Android development. Oracle’s Java 7 is best for developing Android apps, but Java 8 will also work.
On Oracle’s website, Java has two kinds of downloads.
When you visit www.oracle.com
, you see two acronyms floating around: JRE (Java Runtime Environment) and JDK (Java Development Kit). The JRE has everything you need in order to run existing Java programs. Whether you know it, your desktop computer probably has a version of the JRE.
The JDK has everything you need in order to run existing Java programs and everything you need in order to create new Java programs. The JDK has the entire JRE and more.
As an Android developer, you must create your own Java programs. So the download that you want is the JDK, which includes the JRE. You do not want the JRE alone.
Download and install the Java JDK, not the Java JRE.
On Oracle’s website, Java might come with other tools.
A glance at the Java download page shows several options — options to download Java with NetBeans, JavaFX, the Java source code, and some other stuff. In general, these extras (NetBeans, JavaFX, and the others) don’t help with Android app development.
The Android Software Development Kit (SDK) contains the libraries that you need for developing Android applications. The SDK has code for drawing forms on a device’s screen, code for dialing phone numbers, code for taking pictures with the device’s camera, and a lot more.
The kit also contains bare-bones tools for creating, running, and testing your Android applications. By bare-bones tools, I mean tools that you can run by typing instructions in your development computer’s command window (in the Command Prompt on Windows, or in the Terminal application on Linux and on a Mac). These tools perform all the logic required to do full-fledged Android development, but the SDK has no friendly user interface for invoking these tools.
An integrated development environment (IDE) is a user interface for invoking software development tools. An IDE helps you create software easily and efficiently. You can develop software (including Android apps) without an IDE, but the time and effort you save using an IDE makes the IDE worthwhile. (Some hard-core developers disagree with me, but that’s another matter.)
The official IDE for Android app development is called Android Studio. Android Studio is based on a more general-purpose product called IntelliJ IDEA.
All operating systems require at least 2GB RAM. For performance that isn’t very, very slow, you want at least 4GB RAM.
If your computer doesn’t meet the requirements, you might be able to find a workaround. Search the web and you might find what you’re looking for.
You can download Android’s SDK and Android Studio in one big gulp. Here’s how:
Visit developer.android.com/sdk
.
Figure 2-5 shows you what this web page looks like in May of 2015 (commonly known as “the good old days”) when I visit using my Windows computer.
The page has a big button for downloading Android Studio. The Android Studio download includes the much-needed Android SDK.
By the time you read this book, the web page will probably have changed. But you’ll still see an Android Studio download.
Save the download to the local hard drive.
The Android Studio download is either an .exe
file (for Windows), a .dmg
file (for a Mac), or a .tgz
file (for Linux).
When you download Android Studio, you have either an .exe
file (for Windows), a .dmg
file (for a Mac), or a .tgz
archive file (for Linux).
In Windows: Double-click the .exe
file’s icon.
When you double-click the .exe
file’s icon, a wizard guides you through the installation.
On a Mac: Double-click the .dmg
file’s icon.
When you double-click the .dmg
file’s icon, you see an Android Studio
icon (also known as an Android Studio.app
icon). Drag the Android Studio
icon to your Applications
folder.
In Linux: Extract the contents of the .tgz
or .zip
archive file. (Where you put the extracted contents is up to you.) The steps for extracting the file’s contents vary from one Linux distribution to another. Anyway, the following commands work for me:
For a .tgz
file:
cd ~/Downloads
tar zxvf name-of-the-downloaded-file.tgz
--directory ~/Development
For a .zip
file:
cd ~/Downloads
unzip -d ~/Development name-of-the-downloaded-file.zip
In recommending these commands, I assume that your web browser placed the file in a Downloads
directory (a subdirectory of your home directory). I also assume that your home directory has a Development
subdirectory. (Refer to the earlier section “Installing Oracle’s Java Development Kit”.)
While you’re still in the mood to follow my advice, note the location on the hard drive where the Android sdk
lands. For example, in Figure 2-6, the sdk
directory is UsersBarryAppDataLocalAndroidsdk
. I have a name for this sdk
directory: the ANDROID_HOME directory.
Take a few minutes to browse your ANDROID_HOME directory’s contents. The directory has folders named docs
, platform-tools
, platforms
, samples
, tools
, and others. The tools
and platform-tools
folders contain items that Android developers use all the time.
In the previous sections, you downloaded and installed Android Studio. Your next task (should you decide to accept it) is to launch Android Studio. This section has the details.
In the File Explorer (also known as Windows Explorer), navigate to the directory where Android Studio is installed.
Depending on your Windows version, this directory is probably C:Program Files (x86)Androidandroid-studio
or C:Program FilesAndroidandroid-studio
. The directory contains folders named bin
, lib
, sdk
, and others.
bin
subdirectory of the Android Studio directory.bin
subdirectory, double-click the studio.bat
file.What you do to launch Android Studio depends on the steps you followed previously in this chapter. In particular, did you add your JAVA_HOME directory to your PATH
environment variable? (For info about this, refer to the sidebar entitled “Caring for the environment.”)
In a Terminal window, type the following commands:
cd ~/Development/android-studio/bin
./studio.sh
(I assume that you extracted Android Studio to a Development
subdirectory of your home directory. If that’s not the case, change the first command appropriately.)
In a Terminal window, navigate to the bin
subdirectory of the directory in which you installed Android Studio.
To do this, you might type
cd ~/Development/android-studio/bin
startStudio.sh
file in this bin
subdirectory.startStudio.sh
file, put the following text:
export JAVA_HOME=~/Development/jdk1.8.0_25
export PATH=$PATH:$JAVA_HOME/bin
./studio.sh
In the Terminal window, give yourself permission to execute the startStudio.sh
file’s commands.
To do so, type
chmod u+x startStudio.sh
In the Terminal window, tell Linux to execute the startStudio.sh
file’s commands.
To do so, type
./startStudio.sh
In recommending these steps for Linux, I assume that your Java version is 1.8.0_25 (which is probably close, but not entirely correct). I also assume that you extracted both Java and Android Studio to a Development
subdirectory of your user home directory. If any of these assumptions is inaccurate, change the commands appropriately.
Android Studio is running. Now the fun begins.
In the earlier “Setting Up the Software,” you install a small (but usable) portion of the Android SDK. At some point in your travels, you’ll want to install more of the SDK. The following section tells you how.
When you download the Android SDK, you get the code library (the API) for a particular release of Android. You also get several developer tools — tools for compiling, testing, and debugging Android code. For example, to test your code, you can run the code on an emulator. The emulator program runs on your development computer (your PC, your Mac, or your Linux computer). The emulator displays a picture of a mobile device (for example, a phone or a tablet device). The emulator shows you how your code will probably behave when you later run your code on a real phone or a real tablet device.
Another tool, the Android Debug Bridge (adb
), connects your development computer to a device that’s executing your new Android code. (The adb
also “connects” your development computer to a running emulator, even though the emulator is running on your development computer.) The adb
is an invaluable tool for testing Android applications.
Neither the basic emulator nor Android’s adb
tool change very much over time. But Android’s user interface and the features available to Android developers change considerably from one version to another. One month developers work with Android 4.4, codenamed Jelly Bean. Later that year, developers use Android 5.0, codenamed Lollipop. Each version of Android represents a new platform, a new API level, or a new codename depending on the way you refer to the version.
To help you juggle the different Android versions, the people at Google have created two tools.
The next few sections cover these stories in depth.
To manage all the Android versions that are available to you (the developer), use the Android SDK Manager. Here’s how:
In Android Studio’s main menu, choose Tools ⇒ Android ⇒ SDK Manager.
After selecting this option, you see a new window, — namely, the Android SDK Manager window. (See Figure 2-7.)
In the Android SDK Manager, you see a tree of Android versions and tools. For each item in the tree, the Status column tells you if you’ve already installed that item (and, if applicable, that an update is available for the item).
Place check marks next to any items that you want to install, and next to any items that you want to update. (Refer to Figure 2-7.)
You can expand parts of the tree in order to pick and choose from among the items in an Android version. For example, in Figure 2-7, I select only the SDK Platform and Intel x86 Atom System Image items in Android 4.1.2 (API 16). If you have lots of space on your hard drive, and you don’t mind waiting a long time for the download to finish, you can select more items.
In the lower-right corner of the Android SDK Manager window, click the Install Packages button.
After clicking this button, you see a Choose Packages to Install dialog box.
You might be itching to run some code, but first you must have something that can run an Android program. By “something,” I mean either a real Android device (a phone, a tablet, an Android Wear watch, an Android-enabled refrigerator, whatever) or a virtual device. By a virtual device, I mean a program that runs on your development computer and creates a window that looks (and acts) like a real Android device. People call this device an emulator, but in truth, the virtual device has three parts:
A system image is a copy of one version of the Android operating system.
For example, in Figure 2-7, Intel x86 Atom_64 System Image refers to the software that runs Android 4.1.2 on a 64-bit Atom processor.
You get a system image when you follow the steps in the section “Installing the software,” earlier in this chapter. To get other system images, use the Android SDK Manager.
The emulator bridges the gap between the system image and the processor on your development computer.
You might have a system image for an Atom_64 processor, but your development computer runs a Core i5 processor. The emulator translates instructions for the Atom_64 processor into instructions that the Core i5 processor can execute.
You get an emulator when you follow the steps in the section “Installing the software,” earlier in this chapter.
An Android Virtual Device (AVD) is the representation of a real (physical) device’s hardware.
The emulator translates Android code into code that your development computer can execute. But the emulator doesn’t display a particular phone or tablet device on your screen. The emulator doesn’t know what kind of device you want to display. Do you want a camera phone with 800-x-480-pixel resolution, or have you opted for a tablet device with its own built-in accelerometer and gyroscope? All these choices belong to a particular AVD. An AVD is actually a bunch of settings, telling the emulator all the details about the device to be emulated.
What kind of a processor does the device have? What’s the screen resolution of the device? Does the device have a physical keyboard? Does it have a camera? How much memory does it have? Does it have an SD card? All these facts about a virtual device live in an AVD. An AVD runs on a particular system image which, in turn, runs on the emulator.
You create one or more AVDs before testing your code on a development computer. When you tell Android Studio to do a test run of your code, you can choose one of these AVDs.
So before you can run Android apps on your computer, you must first create at least one AVD. In fact, you normally create several AVDs and then use one of them to run a particular Android app.
To create an AVD, follow these steps:
In Android Studio’s main menu, choose Tools ⇒ Android ⇒ AVD Manager.
After selecting this option, you see a new window — namely, the Android AVD Manager window.
In the Android AVD Manager window, click Create, as shown in Figure 2-8.
The Create New Android Virtual Device (AVD) window opens. That’s nice!
Create a name for your virtual device.
You can name your device My Sweet Petunia, but in Figure 2-9, I name my device Lollipop1. The name serves to remind me of this device’s features.
Select values in the drop-down lists.
In Figure 2-9, I select Nexus 5, Android 5.0, Intel Atom (x86_64), No skin, Webcam0, and Emulated back camera.
Sometimes the choice you make in one drop-down list affects the options in other lists. For example, in the Target list, I might select Android 4.4W. (The W stands for wear. The target API applies to watches rather than phones or tablet devices.) As it happens, I haven’t installed a system image for Android Wear devices, so when I make this Target selection, the CPU/API drop-down list is empty. It’s time to install a system image for Android Wear, or to select a different target.
Fill in the other entries in the Create New Android Virtual Device window.
For a novice developer, most of the defaults are okay. But a few of these options might make a big difference.
If the emulator runs on a Windows computer, keep the RAM entry at or below 768.
With the value 768, the virtual device has 768MB of random access memory. A device with more RAM probably won’t start if you try to run it on a Windows computer. To be safe, I always enter 512 or less in the RAM field.
If you select the Snapshot option, the emulator takes less time to start running.
An emulator snapshot is like a laptop computer’s sleep option. The snapshot is a picture of the virtual device’s state when you shut down the emulator. The next time you launch this AVD, the emulator picks up where it left off. This takes less time than restarting the Android operating system anew.
The Android emulator can take a long time to start running. A normal startup can take minutes rather than seconds. Sometimes the startup takes so long that I wonder if the startup is making any progress at all. It’s worth your while to try out any tricks that you find for speeding up the emulator’s startup. (This includes my advice about enabling the Snapshot option.)
The Use Host GPU option can be a deal-breaker.
Not long ago, I had trouble launching the emulator. Every attempt to fire up the emulator failed with either an error message or a stalled startup. In a desperate attempt to get the emulator going, I found an online post advising me to put a check mark in the AVD’s Use Host GPU option. It seemed to be good advice, but when I looked at the AVD’s properties, I found that the Use Host GPU option was already checked. So I experimented by unchecking the Use Host GPU option. Oddly enough, that solved the problem.
Recently, my department hired a new person. We offered a salary of $50K, which (we thought) meant $50,000 per year. Little did we know that the new person expected to be paid $51,200 each year. Computer scientists use the letter K (or the prefix “Kilo”) to mean 1,024 because 1,024 is a power of 2 (and powers of 2 are very handy in computer science). The trouble is, the formal meaning of “Kilo” in the metric system is 1,000, not 1,024. To help clear things up (and to have fun creating new words), a commission of engineers created the Kibibyte (KiB), meaning 1,024 bytes, the Mebibyte (MiB), which is 1,048,576 bytes, and the Gibibyte (GiB), meaning 1,073,741,824 bytes. Most people (computer scientists included) don’t know about KiBs or MiBs, and don’t worry about the difference between MiBs and ordinary megabytes. I’m surprised that the developers of Android’s AVD Manager thought about this issue.
Click the OK button.
After a somewhat uncomfortable delay, your computer returns you to the Android AVD Manager window, where you see a brand-new AVD in the list.
Android’s standard emulator (the emulator that you download from Google’s website) is notoriously messy. The startup is slow, and even after startup, the emulator’s response can be painfully sluggish. In Chapter 3 of this minibook, I tell you how to test Android code on a real device connected via USB to your computer. The performance on a real device (even on an older Android device) is much snappier than on the standard emulator.
If you don’t like the standard emulator, and you don’t want to attach devices to your development computer, you have a third choice. At www.genymotion.com
, you can download an alternative to the standard Android emulator. The alternative is available for Windows, Macintosh, and 64-bit Debian-based systems.
Genymotion’s emulator is must faster and has more features than the standard emulator. If there’s a downside to the Genymotion emulator, it’s the cost. Genymotion’s product is free for personal use, but costs from 100 to 300 Euros for commercial use.
If you develop apps only for personal use, or if you have money to spend on a commercial venture, then Genymotion’s emulator is definitely worth considering.
And that does it! You’re ready to run your first Android app. I don’t know about you, but I’m excited. (Sure, I’m not watching you read this book, but I’m excited on your behalf.) Chapter 3 in this minibook guides you through the run of an Android application. Go for it!
44.220.184.63