Chapter 3
Your First Android Project
In This Chapter
▶
Creating a new blank project in Eclipse
▶
Understanding errors
▶
Creating an emulator
▶
Setting up and copying launch configurations
▶
Running your first app
▶
Studying the anatomy of a project
Y
ou’re excited to get started building the next best Android application known to man, right? Good But before you create that next blockbuster
application, I’m going to walk you through how to create your first Android application to help solidify a few key aspects in the Android project creation
process. You will be creating a very simple “Hello Android” application that requires no coding whatsoever. What? No coding? How’s that possible?
Follow along; I’ll show you.
Starting a New Project in Eclipse
First things first: You need to start Eclipse. After it’s started, you should see something that looks similar to Figure 3-1. Now you’re ready to start cooking
with Android.
Remember setting up your development environment in the previous chap- ter? I’m sure you do In that chapter, you set up all the tools and frameworks
necessary to develop Android applications, and in the process of doing so, the Eclipse Android Development Tools ADT plug-in was installed. The ADT
plug-in gives you the power to generate new Android applications directly from within the Eclipse File menu. That’s exactly what you’re about to do; I
think you’re ready to create your first Android Application project. Follow these steps:
56
Part II: Building and Publishing Your First Android Application
Figure 3-1:
The Eclipse development
environment has been
opened.
1. In Eclipse, choose File➪New➪Project.
The New ProjectSelect a Wizard dialog box opens, as shown in Figure 3-2.
Figure 3-2:
The New Project
Select a Wizard
dialog box.
57
Chapter 3: Your First Android Project
2. From the New ProjectSelect a Wizard dialog box, expand the Android item by clicking the Android folder.
3. After the Android folder is expanded, click Android Project and then click the Next button.
The New Android Project dialog box appears, as shown in Figure 3-3.
4. In the Project Name field, type Hello Android.
The Project Name field is very important, the descriptive name that you provide identifies your project in the Eclipse workspace. After your proj-
ect is created, a folder in the workspace is named with the project name you define here.
5. In the Contents panel, leave the default radio button Create New Project in Workspace and the check box Use Default Location selected.
These defaults are selected automatically when a new project is cre- ated. The Contents panel identifies where the contents of your Eclipse
projects are going to be stored in the file system. The contents are the source files that make up your Android project.
Figure 3-3:
The New Android
Project dialog box.
58
Part II: Building and Publishing Your First Android Application
When you set up Eclipse in Chapter 2, the Eclipse system asked you to set your default workspace. The workspace usually defaults to your
home directory. A home directory is where the system places files perti- nent to you. Figure 3-4 shows my home directory.
If you would rather store your files in a location other than the default workspace location, deselect the Use Default Location check box. This
enables the Location text box. Click the Browse button, and select a location where you’d like your files to be stored.
6. In the Build Target section, select Android 2.2.
The Build Target section identifies which application programming interface API you want to develop under for this project. By selecting
Android 2.2, you have elected to use the Android 2.2 framework. Doing so allows you to develop with the Android 2.2 APIs, which include new
features such as the Backup Manager and new speech-recognition APIs. If you selected Android 1.6 as the target, you would not be able to use
any features supported by version 2.2 or 2.1. Only the features in the targeted framework are supported. If you installed other software devel-
opment kits SDKs in Chapter 2, you might have the option of selecting them at this point. If you selected version 1.6, you’d have access only to
version 1.6 APIs.
Figure 3-4:
My default workspace
location for the Hello
Android
project is
C: Users
dfelker work
space.
59
Chapter 3: Your First Android Project
For more information, see the section “Understanding the Build Target and Min SDK Version settings,” later in this chapter.
7. In the Properties section, type Hello Android in the Application Name box.
The application name is the name of the application as it pertains to Android. When the application is installed on the emulator or physical
device, this name will appear in the application launcher.
8. In the Package Name box, type com.dummies.android.helloandroid.
This is the name of the Java package see the nearby sidebar “Java pack- age nomenclature”.
9. In the Create Activity box, type MainActivity. The Create Activity section defines what the initial activity will be called.
This is the entry point to your application. When Android runs your application, this is the first file that gets accessed. A common naming
pattern for the first activity in your application is MainActivity.java how creative, right?.
10. In the Min SDK Version box, type 8. Your screen should now look similar to Figure 3-5.
Java package nomenclature
A package in Java is a way to organize Java classes into namespaces similar to modules. Each
package must have a unique name for the classes it contains. Classes in the same package can
access one another’s package-access members.
Java packages have a naming convention defined as the hierarchical naming pattern. Each level of
the hierarchy is separated by periods. A package name starts with the highest-level domain name
of the organization; then the subdomains are listed in reverse order. At the end of the package name,
the company can choose what it would like to call the package. The package name com.
dummies.android.helloandroid is the name you will use for this example.
Notice that the highest-level domain is at the front of the package name com. Subsequent
subdomains are separated by periods. The package name traverses down through the
subdomains to get to the final package name of helloandroid.
A great example of another use for a pack- age would be having a Java package for all
your Web-related communications. Any time you needed to find one of your Web-related
Java classes, you could open that Java pack- age and work on your Web-related Java
classes. Packages allow you to keep your code organized.
60
Part II: Building and Publishing Your First Android Application
Understanding Android versioning
Version codes are not the same as version names. Huh? Android has version names and version codes. Each version name has one and only one version code associated with it. The following
table outlines the version names and their respective version code.
Version Name Platform Level Version Code API Level
1.5 3
1.6 4
2.0 5
2.0.1 6
2.1 7
2.2 8
You can also find this information in the Build Target section of the New Android Project dialog box.
Figure 3-5:
A completed
New Android
Project wizard.