Your First Native Android

You will need an Android project to host your native application, but before you create that, you must create an Android Virtual Device (AVD).

Creating an AVD

With version 1.5 of the SDK, Google introduced the concept of virtual devices (AVDs). An AVD is simply a set of configuration attributes applied to an emulator image that allows the developer to target a specific version of the SDK. The following are the basic versions of the SDK targeted by AVDs:

• Android 1.1: This target maps to the Android SDK version 1.1 R2.

• Android 1.5: This target maps to the Android SDK version 1.5 R2.

• Android 1.6: This target maps to the Android SDK versions 1.6 and 2.0.

• Google APIs -1.5 or 1.6: This target must be used if you are building an application that uses Google APIs such as Maps and Search.

■ Caution In Android 1.5 and later, an AVD does not include the Google Maps API. Keep this in mind if you are building a maps-enabled app. Your map application will not start in an Android 1.5 AVD.

Let's create an AVD to target an SDK 1.5 for the sample application.

1. In Eclipse Galileo, click the black phone icon on the Android toolbar (see Figure 2-1). This opens the Android Virtual Devices Manager dialog box.

File Edit Run Source Refactor Navigat tv a e * ^ o- <4-1 #

[S ■■>11 S Hierarc

a g

e %

Figure 2-1. Android toolbar within Eclipse

2. In the dialog box, enter a name for the AVD (droid1.5 in this example).

3. Select a target SDK (Android 1.5 in this example).

4. Optionally, enter an SD card absolute path (if you already have one), such as /home/user/sdcard.iso, or a size, such as 128Mb. Figure 2-2 shows the Android Virtual Devices Manager dialog box for this example.

5. Click Create AVD.

■ Tip You should create an SD card for the AVD, and use it for the examples in later chapters to store large game files (for Doom and Wolfenstein 3D). Game files should not be saved in the device main file system, unless you want to run out of space after installing one game.

Download at WoweBook.com

$ Android Virtual Devices Manager

Android Virtual Devices Manager

Displays existing Android Virtual Devices. Lets you create new ones 01 delete existing ones.

List of existing Android Virtual Devices:

Refresh

AVD Name

Target Name

Platfo API Level

droidl.5

Android 1.5

1.5 3

LideteJW«:.

Create AVD

Name

Target

Android 1.5 -1.5 :

SDCard

/liome/user/sdcard.iso

Skin

Default (H VGA)

SDCard

/liome/user/sdcard.iso

Skin

Default (H VGA)

Finish

Figure 2-2. Android Virtual Device Manager dialog box settings for SDK 1.5

Note that in Android 1.6 and later, the GUI has changed a little, as shown in Figure 2-3. To create an AVD, click New to open the Create New AVD dialog box, enter a name, select a firmware target plus SD card size, and then click Create AVD.

Figure 2-3. Creating a new AVD in Android SDK 1.6

Creating the Android Project

With the AVD in place, the next task is to create the project for this chapter, as follows:

1. Click the New Android Project icon on the main toolbar (see Figure 2-1). This opens the New Android Project dialog box.

2. In the dialog box, enter a project name (ch02 .Project in this example).

3. Enter an application name (Chapter2 in this example).

4. Enter a package name (ch02. project in this example).

5. Enter an activity name (MainActivity in this example).

6. Specify a minimum SDK version (3 in this example). Figure 2-4 shows the completed New Android Project dialog box for this example.

7. Click Finish.

Figure 2-4. New Android Project dialog box for this chapter's example

Application Architecture

Let's consider what we wish to accomplish with this application:

• We want to create the basic Android application. When run in the emulator, the app will create a default view with the text "Hello Chapter2!".

• Within the project, we will create a native folder with files to accomplish the following:

• Create a native library with a main subroutine that will be called from the Android main activity using JNI.

• The main library subroutine will invoke a Java method (using JNI) within the Android project, sending a text message back to the Android Java layer.

• The library will be loaded at runtime within Java using a System.load(path). Figure 2-5 shows the file system layout of the project.

■ Tip It would be helpful to import the project source (ch02.Project) into your workspace to go along with this chapter.

The following Java files describe the project:

• ch02.project.MainActivity.java: This file is created by the wizard and should already exist in the project.

• jni.Natives.java: This is a new file that contains native methods to be invoked within the native library and callbacks that the C library will perform within Java.

B ' n 1i: Hierarc = □ > I*gen [Generated ]sva ill AnrlrnirlManifpst.xm I

Figure2-5. Project layout

You must create the following files within the native folder (see Figure 2-5):

• lib.c: This is the main library code. It contains all the necessary JNI system calls to cascade information back and forth between Android and C.

• testlib.c: This is a test program for the library. Its main function is to make sure there are no symbols missing from the native library.

• main.c: This is a static program used to demonstrate how to compile a static native program that can be run in the device.

• Makefile: This is the project's native build file. It has targets for the following:

• Build the native library

• Build the library test program

• Generate JNI header files required to cascade information back and forth between Java and C

• Build the static test program

• Deploy the files to the device for testing

Let's look at the files in more detail to understand what they do. We'll start with the Java layer.

0 0

Post a comment