Lesson 3: Explore and Build Your First App
After installing the Android SDK and an Android platform, you are ready to create apps for this platform. Lesson 3 introduces you to a first app and shows you how to build this app at the command line.
It’s helpful to introduce a technology by presenting a short program that outputs some kind of welcome message. Listing 1 presents the Java source code to a small Android app that performs this task.
Listing 1. Greetings from Android
public class W2A extends Activity
// The following method is invoked when the activity is first created.
public void onCreate(Bundle savedInstanceState)
TextView tv = new TextView(this);
tv.setText(“Welcome to Android.”);
Listing 1 reveals the contents of a source file named
W2A.java – Android apps are written in the Java language, which probably isn’t surprising, given that the Android SDK is dependent on a JDK being installed. The listing begins with a mandatory package statement, because every Android app is associated with a package. The subsequent import statements identify three Android-specific types that are referenced from
W2A.java’s W2A (Welcome to Android) class, and which help the compiler locate these types.
Tip: Familiarize yourself with Google’s Java API documentation, so you can quickly locate Android-specific types. You’ll find this documentation at http://developer.android.com/reference/packages.html.
Android apps are based on one or more components that interact with each other. Components include activities, broadcast receivers, content providers, and services. For brevity, I ignore broadcast receivers, content providers, and services.
An activity presents part of an app’s user interface and is described by the
android.app.Activity class. Listing 1’s
W2A class subclasses Activity, making
W2A a specialized activity.
When the user launches an app, Android instantiates the app’s main activity class (the app might consist of multiple activity classes), and invokes that class’s overridden version of
void onCreate(Bundle savedInstanceState) method to initialize the activity.
If the activity previously saved its state before terminating, Android makes that state available to
onCreate() by passing an
android.os.Bundle instance argument to
savedInstanceState. Otherwise, it passes a null argument (there is no saved state).
onCreate() first invokes the superclass version of this method with the passed argument. It then instantiates the
android.widget.TextView class to create a textview widget (user interface control), which is the extent of the activity’s UI.
onCreate() is an example of a lifecycle callback method. Android calls
onCreate() and other methods during an app’s lifecycle. Each lifecycle callback method must first call the same-named method in the
this is passed to
TextView’s constructor to connect this widget to the current activity. The text passed to
setText(CharSequence text) method will be displayed to the user.
After creating and initializing the widget instance,
void setContentView(View view) method to place the widget instance, which is also known as a view, directly into the activity’s view hierarchy so that the widget will be displayed.
Let’s build W2A. First, create a W2A project with the android tool, which provides the following syntax (split across multiple lines for readability) for creating projects:
android create project
--target target_ID --name project_name
--name (or -n), all of the following options must be specified:
--target(or -t) identifies the app’s build target.
target_IDis an integer that identifies an Android platform. Obtain this value by invoking
android list targets. Because you’ve installed only version 2.3.3, this command should output a single Android 2.3.3 platform target identified as integer ID 1.
--name(or -n) identifies the project’s name. This name is used for the resulting Android PacKage (APK)’s filename. An APK is a ZIP-based archive that stores an app’s files, and which is installed on a device. Its file extension is
--path(or -p) identifies the project directory. The directory is created if it doesn’t exist.
--activity(or -a) identifies the default activity class. The resulting classfile is generated inside
/path/to/project/project_name/src/package_namespace/, and is used as the APK’s filename if
--name(or -n) isn’t specified.
--package(or -k) identifies the project’s package namespace, which must follow the package rules laid out by the Java language specification.
Assuming a Windows platform and a
C:prjdevW2A hierarchy that will store the W2A project, the following command line (split across two lines for readability) creates this project:
android create project -t 1 -p C:prjdevW2A -a W2A
This command line creates several directories and adds various files to some of them. Specifically, it generates the following file and directory structure within
AndroidManifest.xmlis the XML-based manifest file for the app being built. The Android manifest identifies an APK’s app components, such as the
Activitysubclass that was previously specified via
--activity(or -a), and provides other useful information to Android.
ant.propertiesis a customizable properties file for the Ant build system. You can edit this file to override Ant’s default build settings, and provide a pointer to your keystore and key alias so that the build tools can sign your app when built in release mode (discussed later in this lesson).
binis the output directory that’s used by the Apache Ant build script. Generated APKs are stored here.
build.xmlis this project’s Apache Ant build script.
libscontains private libraries, which aren’t required by this project but may be present for more sophisticated projects.
local.propertiesis a generated file that contains the Android SDK home directory location.
proguard.cfgcontains configuration data for the SDK’s ProGuard tool, which lets developers obfuscate (hide) their code (making it very hard to reverse engineer) as an integrated part of a release build.
project.propertiesis a generated file that identifies the project’s target Android platform.
rescontains project resources (images, strings, layouts, and so on). No resources are used by this project. (For brevity, I don’t discuss resources in this course.)
srccontains the project’s source code in a series of subdirectories that reflects the package structure of the project.
src contains a
catutortutorw2a directory hierarchy, and
w2a contains a skeletal
W2A.java source file. Replace this file’s contents with Listing 1.
C:prjdevW2A is current, build this app by specifying
ant debug or
ant release –
ant defaults to processing this directory’s
ant debugbuilds the app for testing and debugging. The build tools sign the resulting APK file with a debug key and then optimize the APK with Android’s
ant releasebuilds the app for release to users. You must sign the resulting APK file with your private key and then optimize the APK with the SDK’s
zipalignbasic tool. (These tasks are beyond the scope of this lesson.)
Note: Android requires developers to sign their APKs with their certificates’ private keys. It uses the certificate to identify the app’s author and establish a trust relationship between apps; Android doesn’t use the certificate to determine which apps can be installed. A self-signed certificate is acceptable – certificates don’t have to be signed by certificate authorities. Because a special debug certificate is used to sign an APK built in debug mode, the APK cannot be released to the public. Wikipedia’s “Public key certificate” entry (http://en.wikipedia.org/wiki/Public_key_certificate) introduces you to certificates.
C:prjdevW2A is the current directory, build W2A in debug mode by invoking
ant debug. This command creates a gen subdirectory that stores a generated
R.java resource-oriented file (which you can ignore), and creates
W2A-debug.apk in the bin subdirectory. We’ll install this APK and run its app in a later lesson.
The following review questions help you test your mastery of Lesson 3’s material:
- What is an activity?
- Identify the package in which Android’s
Activityclass is stored.
Activitymethod does Android invoke to initialize an activity?
- Specify the syntax that the
androidtool requires when creating an Android project. Which of this syntax’s options is optional?
- Identify the file and directories that are created in the project directory. • How do you build the Android project’s app?