Android Application Development For Dummies, 3rd Edition (2015)
Part I. Getting Started with Your First Android Application
Chapter 2. Prepping Your Development Headquarters
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.
Developing the Android Developer Inside You
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:
· Do I want to develop Android applications?
· Do I like free software development tools?
· Do I like to pay no developer fees?
· Do I have a computer to develop on?
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.
If you’re developing an application for a client, you can publish your application as a redistributable package to give to him. Then your client can publish the application to the Google Play Store, using his Google account, to ensure that you don’t have to pay a fee for client work. You can then be a bona fide Android developer and never have to pay a fee. That’s cool.
Assembling Your Toolkit
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)
· Security model: The Linux kernel handles security between the application and the system.
· Memory management: The kernel handles memory management, leaving you free to develop your app.
· Process management: The Linux kernel manages processes well, allocating resources to processes as they need them.
· Network stack: The Linux kernel also handles network communication.
· Driver model: The goal of Linux is to ensure that the application works. Hardware manufacturers can build their drivers into the Linux build.
Android source code
You should be aware that the full Android source code is open source, which means that it’s not only free to use but also free to modify. If you want to download the Android source code and create a new version of Android, you’re free to do so. Check out the Android Open Source Project at https://source.android.com.
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:
· The Android runtime: The Android runtime is composed of Java core libraries and ART (the Android RunTime). Older versions of Android (4.x and earlier) use the Dalvik runtime.
· Open GL (graphics library): This cross‐language, cross‐platform application program interface (API) is used to produce 2D and 3D computer graphics.
· WebKit: This open source web browser engine provides the functionality to display web content and to simplify page loading.
· SQLite: This open source relational database engine is designed to be embedded in devices.
· Media frameworks: These libraries allow you to play and record audio and video.
· Secure Sockets Layer (SSL): These libraries are responsible for Internet security.
See Figure 2-1 for a list of common Android libraries.
Figure 2‐1: Android and other third‐party libraries that sit atop the Linux 3.4 kernel.
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
· Activity manager: Manages the activity lifecycle.
· Telephony manager: Provides access to telephony services as well as to certain subscriber information, such as phone numbers.
· View system: Handles the views and layouts that make up your user interface (UI).
· Location manager: Finds the device’s geographic location.
Take a look at Figure 2-2 to see the libraries that make up the application framework.
Figure 2‐2: A glimpse at part of the Android 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.
Figure 2‐3: How the Android application framework stacks up.
Sometimes when you’re developing an Android application, you want to use the same resource as in the core Android system. A good example is an icon for a Settings menu option. By accessing the Android source code, you can browse the various resources and download the resources you need for your project. Having access to the source code also allows you to dig in and see exactly how Android does what it does. Be aware though that you need to follow the requirements of the license, as well as follow Google’s branding guidelines when borrowing these resources. Find out more at http://d.android.com/distribute/googleplay/promote/brand.html.
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’re expected to understand the basics of the Java programming language before you write your first Android application. If you’re feeling rusty and need a refresher course on Java, you can visit the Java tutorials site at http://docs.oracle.com/javase/tutorial.
Though you find a little Java information in this book, you may want to spend some time with a good book like Java All‐in‐One For Dummies, by Doug Lowe (John Wiley & Sons, Inc.), if you have no Java experience.
Tuning Up Your Hardware
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:
· Windows XP or later
· Mac OS X 10.5 or later
· Linux with GNOME or KDE
Throughout this book, some examples use Windows 7 64‐bit Edition. Windows paths look similar to this:
Some examples use Mac OS X; a Mac or Linux path looks similar to this:
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, athttps://developer.android.com/sdk/installing/studio.html.
To save you time, you need at least 3GB of free hard drive space to install all the tools and frameworks necessary to develop Android applications.
Installing and Configuring Your Support Tools
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:
· Android Studio: An easy to use Integrated Development Environment (IDE) that brings together Java and the Android SDK to make it simple to write Android apps.
· Java JDK: The Java Development Kit. Lays the foundation for the Android SDK.
· Android SDK: Included in Android Studio. Provides access to Android libraries and allows you to develop for Android.
The following sections show you how to acquire and install all these tools.
A benefit of working with open source software is that, most of the time, you can get the tools to develop the software for free. Android is no exception to that rule. All the tools that you need to develop rich Android applications are free.
Installing Android Studio
To download Android Studio, first go to https://developer.android.com/sdk/installing/studio.html and download Android Studio. Then go through the following steps to get to the Android Studio boot screen, as in Figure 2-4:
Figure 2‐4: The Android Studio boot screen.
If you’re a Mac user, follow these steps to download Android Studio:
1. Open the downloaded DMG file, android‐studio‐ide‐*.dmg .
2. From the resulting Android Studio DMG volume, drag and drop Android Studio into theApplicationsfolder.
3. Eject the Android Studio DMG volume so that you don’t accidentally run the wrong Android Studio.
4. Open theApplicationsfolder 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.
5. 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:
1. Launch the downloaded EXE file, android‐studio‐bundle‐*.exe .
2. Follow the setup wizard to install Android Studio.
3. 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:\ProgramFiles\Java\jdk1.8.0_20.
If you do not have the Java JDK installed, see the next section.
Installing Java 7
On some systems, you may see an error that the system cannot find the Java 7 JDK.
If this happens, visit www.oracle.com/technetwork/java/javase/downloads/jdk7‐downloads‐1880260.html to download the Java 7 JDK appropriate for your machine. Install it, then run Android Studio again.
If you’re on a Mac and you continue to see the same error after installing Java 7, you may also need to install Java 6. Visit http://support.apple.com/kb/DL1572 to install Java 6 on your Mac, then try again.
Adding SDK Packages
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:
1. Choose Tools⇒Android⇒SDK Manager to launch the SDK manager tool.
2. Click New to select all new packages that are not currently on your machine, then click the Install button.
3. Click each group in the list and click Accept License for each, then click Install.
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.
Navigating the Android SDK
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>\AppData\Local\Android\android‐studio\sdk\" 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
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.
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.
Specifying Android Platforms
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.
Several versions of Android are still widely used on phones. If you want to reach the largest number of users, target an earlier version. If you want to keep your development quick and simple, or if your app requires functionality that older platforms can’t support, then by all means specify the newer platform. It would make no sense to write a Bluetooth toggle widget targeting any platform earlier than 2.0 because earlier platforms can’t use Bluetooth.
To view current platform statistics, visit http://d.android.com/resources/dashboard/platform‐versions.html.
Using SDK Tools for Everyday Development
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.
Saying hello to the emulator
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.
Getting physical with a real Android device [Windows]
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:
1. Plug in your device.
2. Choose Control Panel⇒Device Manager.
3. Expand Other Devices (Figure 2-5), right‐click your device, and select Update Driver Software.
Select Browse my computer for driver software.
4. Type \C:\Users\<user>\AppData\Local\Android\android‐studio\sdk\extras\google\usb_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.
5. When asked if you would like to install this device, click Install.
Figure 2‐5: Finding your device in the Windows Device Manager.
Debugging your work
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.
Trying out the API and SDK samples
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 http://d.android.com/samples/ to find samples of almost anything, from using Bluetooth to making a two‐way text application or a 2D game.
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.
Giving the API demos a spin
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
· Background services
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/.