Bringing It All Together Creating Your First Android Application

There are two ways to create an Android application using the tools we've discussed thus far. One is to create the application by issuing commands in a terminal or command window, another is to create the application from within the IDE, Eclipse. This section discusses creating the application using both methods. First, from the command window and then from Eclipse.

To create a project, use the android script (found in the android/tools directory). Open a command prompt and run this command:

android create project -a HelloAndroidGeek -k .

^com.samples.HelloAndroidGeek -t 2 -p /home/<username>/AndroidProjects/HelloAndroidGeek

The complex android script is used for creating many Android-related items, including new projects (as shown here) and Android virtual devices. The "-a HelloAndroidGeek" specifies the name of the derived activity class for the project. The "-k com.samples.HelloAndroidGeek" specifies the project's package name. The "-t 2" specifies the Target ID of the new project, "2" being the Target ID for an Android version 1.5 program. You can see more about these Target IDs by entering the command, "android list targets." Finally, the "-p /home/username/AndroidProjects/HelloAndroid/Geek" specifies the directory to create all of the project's subdirectories and files in. This creates the necessary directory structure and build file for your project to easily build your Android application (see Figure 11.5).

Dr\>activitycreator -o HelloAndroidGeek com,samples.HelloAndroidGeek

Output directory: HelloAndroidGeek

Activity name: HelloAndroidGeek

Created directory D:\HelloAndroidGeek\srcXcomNsamples

Added file HelloAndroidGeekVsrcVcomVsarftpleaVHelloAndroidGeek.Java

Created directory D:VHelloAndroidGeekVbin

Created directory D:\HelloAndroidGeek\libs

Created directory D: MHel loAndroidGeekN.res^values

Added file HelloAnd ro i dGeekNresVva1ues\st r i ngs.xml

Created directory D:\HellcAndroidGeek\res\layout

Added file HelloAnd roi dGeek^-res Vlayout^mai n. xml

Added file Hel loAndroidGeek'vAndroidManif est, xml

Added file He 1loAndroidGeekSbuiid.xml


Running android script output.

If you navigate to the src/com/samples folder inside the HelloAndroidGeek folder, you will find that a Java source code file named has been generated for you. Edit the source file using your favorite editor so that it looks similar to the following code (don't worry about understanding what the code does—that's the focus of the next chapter):

package com.samples; import; import android.os.Bundle; import android.widget.TextView;

public class HelloAndroidGeek extends Activity {

/** Called when the activity is first created. */

@Override public void onCreate(Bundle savedlnstanceState) {

super.onCreate(savedlnstanceState); TextView textView = new TextView(this); textView.setText("Hello, Android Geek"); setContentView(textView);

The easiest way to build your application is to open a command prompt and run the command "ant -v debug" from the HelloAndroidGeek project folder (see Figure 11.6). You notice that in the project folder is a file named build.xml. This file contains tasks to compile the code and package it using tools from JDK and Android SDK. The command invokes a build by using the build.xml file and generates the necessary binaries in the bin folder.

FIGURE 11.6_

Running ant to build the project.

With the build complete, the bin folder now contains (among other files) a file named classes.dex file and a file named HelloAndroidGeek-debug.apk. The classes.dex file is a Dalvik executable file and is packaged with other required resources into the HelloAndroidGeek-debug.apk Android package file.

The next step is to deploy the Android package file to the device emulator. The Android device emulator is an emulation of an Android handset. This

D: MHelloAndroidGeek>ant Buildfile: build.xml

[echo] Creating output directories if needed... Cmkdlr] Created dir: 0: \HelloAndroidGeek\bin\classes

[echo] Compiling aidl files into Java classes... coinpi le:

[javacl Compiling 2 source files to D:\HelloAndroidGeek\bin'\clas3es dex:

[echo] Converting compiled files and external libraries into bin/ package-res:


Lecho] Packaging resources...


[echo] Packaging bin/HelloAndroldGeek-debug.apk, and signing it with a debu g key.

[exec] Using keystore; C:'^UsersM'rasannaVAppDat a^Local^AndroidVJebug. keysto re

BUILD SUCCESSFUL Total time: 3 seconds Dr MHelloAndroidGeek helps you test your application before you deploy it to a physical handset. Before you can run the emulator, you need to create an Android Virtual Device. The following command does this:

android create avd -t 2 -n G1Emu -p ^/home/<username>/AndroidProjects/G1Emu

The " - t 2" tells the android script that the target device is running the "Cupcake" version of android (v1.5). The "-n G1Emu" gives the AVD a name, and the " -p" parameter tells the script which directory to put the new AVD in.

The Android emulator is a powerful tool with many options. You can see a list of those options with the command "emulator -help | less" on Linux or just "emulator -help" on Windows.

To deploy the application, start the emulator by running the command "emulator -avd G1Emu" on the command line. This starts the Android device emulator (see Figure 11.7).

FIGURE 11.7_

Running the Android emulator.

FIGURE 11.7_

Running the Android emulator.

Now you can deploy the application to the emulator by running the command "adb -e install HelloAndroidGeek-debug.apk" in a new command prompt window (see Figure 11.8). (If you want to deploy directly to your connected Android device, use "-d" instead of "-e" in the command.)

D;\HelloAndroidGeek\bin>adb install HelloAndroidGeek-debug.apk 33 KB/s (4500 bytes in 0.131s)

pkg: /'dota/local/tmp/HelloAndroidGeek-debug.apk




Deploying the application to the emulator.

This installs the newly created application in the Android device emulator. Now you can run the application by invoking it from the list of programs in the emulator. This displays the greeting message "Hello, Android Geek" from your first Android application, as shown in Figure 11.9.


Android application running in the emulator.

0 0

Post a comment