Compiling and Installing the Application

Appportunity

Build your own Android App Dev Empire

Get Instant Access

It is time for the real test. You can now compile your command-line project. To compile your project, use ANT. Once the project is compiled, you will install it on your Emulator.

Compiling Your Project with ANT

After you have your JAVA_HOME environment variable set and have ANT in your PATH statement, you should be able to navigate the directory containing your build.xml file and simply run the command ant. Open a Windows command prompt to your project directory and run ant, as follows:

HB C :\Wi ii dow5\iyste in 3 m d, ejfe-;

-Inlxl

:\nndi-oidHelloWorld>ant.

J

The result of running ant will be an .apk file that you will install directly onto your phone (Emulator). However, whereas Eclipse installs the .apk file for you directly on the Emulator, you need to install it manually. You use the Android Debug Bridge (adb) Android tool to install the application, as described in the next section.

What to Do if Running ant Produces an Error If ANT produces an error when you run it, fear not. Because Android is still in its initial release stages as of the writing of this book, several items may need to be tweaked. Small changes here and there can always be expected when you are working in a new technology. When I first tried to run ant and compile my project, I received an error like that shown in the following illustration.

Some research on the issue at the Google Android developer's forum turned up an interesting solution: a rewrite of build.xml that tweaked some of the commands offered to ANT. What follows is the modified build.xml file, in which the key changes have been bolded. Compare this file with the original and you will see that it differs quite noticeably.

<project name="HelloWorldCommandLine" default="package" basedir=".">

<property name="sdk-folder" value="c:\Android\android-sdk_m5 rc14_windows\android-sdk_m5-rc14_windows" />

<property name="android-tools" value="c:\Android\android-sdk_m5 rc14_windows\android-sdk_m5-rc14_windows\tools" />

<property name="android-framework" value="${android-tools}/lib/framework.aidl"

<!-- Eclipse uses "bin" for its own output, so we do the same. --> <!-- Use full path for output dir - FIX - BLOCK START --> <property name="outdir" value="${basedir}/bin" />

<!-- Use full path for output dir - FIX - BLOCK END -- >

<!-- No user servicable parts below. --> <!-- Input directories -- >

<property name="resource-dir" value="res" /> <property name="asset-dir" value="assets" /> <property name="srcdir" value="src" />

<property name="outdir-classes" value="${outdir}/classes" />

<!-- Create R.java in the source directory --> <property name="outdir-r" value="src" />

<property name="dex-file" value="classes.dex" />

<property name="intermediate-dex" value="${outdir}/${dex-file}" /> <!-- The final package file to generate -- >

<property name="out-package" value="${outdir}/${ant.project.name}.apk"/> <!-- Tools -- >

<property name="aapt" value="${android-tools}/aapt" /> <property name="aidl" value="${android-tools}/aidl" />

<condition property="dx" value="${android-tools}/dx.bat" else="${android tools}/dx" >

<os family="windows"/> </condition>

<property name="dx" value="${android-tools}/dx" /> <property name="zip" value="zip" />

<property name="android-jar" value="${sdk-folder}/android.jar" /> <!-- Rules -- >

<!-- Create the output directories if they don't exist yet. --> <target name="dirs">

<mkdir dir="${outdir}" /> <mkdir dir="${outdir-classes}" /> </target>

<!-- Generate the R.java file for this project's resources. --> <target name="resource-src" depends="dirs"> <echo>Generating R.java...</echo> <exec executable="${aapt}" failonerror="true"> <arg value="compile" /> <arg value="-m" /> <arg value="-J" /> <arg value="${outdir-r}" /> <arg value="-M" />

<arg value="AndroidManifest.xml" />

<arg value="-S" /> <arg value="${resource-dir}" /> <arg value="-I" /> <arg value="${android-jar}" /> </exec> </target>

<!-- Generate java classes from .aidl files. --> <target name="aidl" depends="dirs">

<apply executable="${aidl}" failonerror="true"> <fileset dir="${srcdir}">

<include name="**/*.aidl"/> </fileset> </apply> </target>

<!-- Compile this project's .java files into .class files. -- > <target name="compile" depends="dirs, resource-src, aidl">

<javac encoding="ascii" target="1.5" debug="true" extdirs="" srcdir="."

destdir="${outdir-classes}" bootclasspath="${android-jar}" />

<!-- Convert this project's .class files into .dex files. --> <target name="package-dex" depends="dex, package-res"> <echo>Packaging dex...</echo>

<exec executable="${zip}" failonerror="true"> <!--<arg value="-Xmx384M" />-->

<!-- Move Xmx parameter to dx.bat - FIX - BLOCK END -- > <arg value="--dex" />

<arg value="--output=${intermediate-dex}" /> <arg value="--locals=full" /> <arg value="--positions=lines" /> <arg path="${outdir-classes}" /> </exec> </target>

<!-- Put the project's resources into the output package file. --> <target name="package-res-and-assets">

<echo>Packaging resources and assets...</echo> <exec executable="${aapt}" failonerror="true"> <arg value="package" /> <arg value="-f" /> <arg value="-c" /> <arg value="-M" />

<arg value="AndroidManifest.xml" />

<arg

value=

-A" />

<arg

value=

'${asset-dir}" />

<arg

value=

'-I" />

<arg

value=

${android-jar}"

<arg

value=

'${out-package}"

<!-- Same as package-res-and-assets, but without "-A ${asset-dir}" --> <target name="package-res-no-assets"> <echo>Packaging resources...</echo> <exec executable="${aapt}" failonerror="true"> <arg value="package" /> <arg value="-f" /> <arg value="-c" /> <arg value="-M" />

<arg value="AndroidManifest.xml" /> <arg value="-S" /> <arg value="${resource-dir}" /> <!-- No assets directory -- > <arg value="-I" /> <arg value="${android-jar}" /> <arg value="${out-package}" /> </exec> </target>

<!-- Invoke the proper target depending on whether or not an assets directory is present. --> <!-- TODO: find a nicer way to include the "-A ${asset-dir}" argument only when the assets dir exists. --> <target name="package-res">

<available file="${asset-dir}" type="dir"

property="res-target" value="and-assets" /> <property name="res-target" value="no-assets" /> <antcall target="package-res-${res-target}" /> </target>

<!-- Put the project's .class files into the output package file. --> <target name="package-java" depends="compile, package-res"> <echo>Packaging java...</echo> <jar destfile="${out-package}"

basedir="${outdir-classes}" update="true" />

<!-- Put the project's .dex files into the output package file. --> <target name="package-dex" depends="dex, package-res"> <echo>Packaging dex...</echo>

<exec executable="${zip}" failonerror="true"> <arg value="-qj" /> <arg value="${out-package}" /> <arg value="${intermediate-dex}" /> </exec>

<!-- Create the package file for this project from the sources. --> <target name="package" depends="package-dex" /> </project>

After modifying build.xml, you can then try to run ant again.

Installing Your Application with adb

The first step is to start your Emulator. In the Android /tools folder, find the emulator.exe file and execute it. This starts your Android server. That is, starting the Emulator also starts a virtual cell phone on your PC, as shown next. You can then use different tools to interact with the server, to do such things as install applications and call a shell environment.

To install your command-line application on your Android server, you need to use adb. adb is your connection to the Android server, which is started with your Emulator. adb contains many useful functions that allow you to interact with your Android server; one of these enables you to install applications.

Table 6-1 lists and describes the commands that adb accepts.

Command

Description

install <path>

Installs applications to the server

pull <remote file> <local file>

Pulls a remote file off the server

push <local file> <remote file>

Pushes a local file to the server

shell

Opens a shell environment on the sever

forward <local port> <remote port>

Forwards traffic from one port to another (to and from the server)

start-server

Starts the server

kill-server

Stops the server

ppp <tty> <params>

Uses a PPP connection over USB

devices

Lists the available emulators

help

Lists the adb commands

version

Displays the adb version

Table 6-1 adb Commands

Table 6-1 adb Commands

To copy your application to the server, open a Windows command prompt and navigate to the directory of your build.xml file. The command syntax for adb is as follows:

adb install <apk path>

If the application installs to the phone properly, you will just get the package size as feedback in return from the command, as shown next.

5i3! Q:\Wmdows\system32Virit1,exe

_ n x

G:VUsersNJFBihai'zio>sdJj install c »i*

71 KB/s <3970 bytes in 0.0G4s>

:\andi*o idlielloworldSbinSHe lloUor-ldComriandLine .a a|

C: \Users\JFDiMai-z io >_

Switching over to your running Android Emulator, you should now see the application installed on your phone.

What to Do if Running the Application Produces an Error The first time I ran this application, after using the new build.xml file, I received an error on the Android Emulator. Shown in the following illustration, the error pointed to a missing class.

Application Error: a nd roid. progra mmers.gu id e

An error has occurred in a nd roid. progra mmers.gu id e. Unable to instantiate activity Com ponentlnfo{and roid. progra rnmers.guide/android.progra mm ers.guide.HelloWorldCommandLi ne};

java Jang.ClassNotFound Exceptto n:

android, progra mrners.gLi id e.H el I oWorldCommandLine in loader android. lang.PathClassLoaderGM 00 la 540.

Application Error: a nd roid. progra mmers.gu id e

An error has occurred in a nd roid. progra mmers.gu id e. Unable to instantiate activity Com ponentlnfo{and roid. progra rnmers.guide/android.progra mm ers.guide.HelloWorldCommandLi ne};

java Jang.ClassNotFound Exceptto n:

android, progra mrners.gLi id e.H el I oWorldCommandLine in loader android. lang.PathClassLoaderGM 00 la 540.

HIH

4 5

6

7

6

9

0

Q W E

r r

y

u

1

0

H

HEM

F G

H

J

K

L

ML <23

"i Z X

C V

3

N

M

iYM @

/

Was this article helpful?

0 0

Post a comment