Vous êtes sur la page 1sur 2

Lab 1

Eclipse & Android Installation


Gene Apps are written in Java, and run on Android, Googles mobile device operating sys-
tem. Android is based on Linux, and is freely available under the Apache license. The An-
droid Software Developers Kit (SDK) is freely available from Google, but is not completely
free; there are some restrictions in the license. The SDK contains tools for compiling Java
Bytecode into Dalvik executables, which is what Android runs rather than a standard JVM.
It also contains tools for loading applications into an Android emulator, and an emulator
that you can run that looks and behaves like a running GPhone.
Developing Android applications is a bit like developing Java Applets, but with a few cru-
cial differences. Rather than extending Applet, you extend Activity; so you have to learn
what the Activity object does, and what methods you need to override. Also, unlike Ap-
plets, you dont use Swing or the AWT to control your user interface, but instead you can
specify Androids View objects using XML (almost like writing a Web page).
An Activity is one of four types of Object that may be bundled into a complete application:
it represents what the user is doing right now, so it is useful for us to focus on as part of a
UI course. The other three components are Services, which run in the background a bit like
Unix daemons; Broadcast Receivers which do nothing but react to broadcast announcements
(such as that the timezone has changed, or a download is complete); and Content Providers
which make data from one application available to other applications.
An application is a set of one or more of each of the four types of object above, all packaged
up into a .apk le (a little bit like a .jar le in the regular Java world).
1.1 The Supported Tools
To begin writing an Android application, you write a Java class that extends the Activity
object. You can do this using a text editor, but there is a lot of conguration to get right, so
we are going to use Googles ofcially supported IDE, which is Eclipse.
You also need to install the Android SDK, which is a set of command-line tools. To do this,
you need enough Unix knowledge to add a set of executable les to your PATH environment
variable.
Todays task is to get the Android emulator running on a lab Macintosh, and to get it to run
a Hello World application. Were going to do this in a few stages:
3
4 LAB 1. ECLIPSE & ANDROID INSTALLATION
1. Get the Android SDK installed as a command-line set of programs.
2. Get Eclipse set up and running.
3. Get a plain Hello World Java application running in Eclipse.
4. Get Eclipse to recognise the Android SDK.
5. Get an Android Hello World activity running.
The following instructions assume that you are developing on a Macintosh rather than on
Linux, but should translate to Linux without a great deal of difculty. (Note that the proce-
dures have been tested on Macs, not Linux.)
1.2 The Android SDK
In the directory /Applications/android-sdk-mac_x86-1.5_r3 youll nd the Android SDK.
In particular, youll nd a subdirectory called tools, which is where the command-line
programs live, and it is handy to have them added to your PATH environment variable.
Open your ~/.bashrc le with a text editor (if you dont have a .bashrc le, now is a good
time to create one). We want to do two things: add the tools directory to your path, and
export your path so that it is available to processes started by your shell.
Add the line to your .bashrc le:
PATH=$PATH:/Applications/android-sdk-mac_x86-1.5_r3/tools
Then, at the end of the le, add the line (if it isnt already there):
export PATH
This will cause all non-interactive shells to have the tools directory in their path. To get
login shells to have it too, you should add the following lines to your .bash_profile le.
(Again, if you dont have a .bash_profile yet, you should create one now.)
if [ -f $HOME/.bashrc ]; then
source $HOME/.bashrc
fi
Then open a new terminal window. You can tell if the procedure worked by typing
echo $PATH
You should see the directories that you typically have on your path (e.g. /bin and /usr/bin)
and you should also see the Android SDK tools directory tacked on the end.
You can double-check this by typing:
which android
1.3. ECLIPSE 5
If the android binary has been successfully added to your path, you should see something
like /Applications/android-sdk-mac_x86-1.5_r3/tools/android in response.
1.3 Eclipse
The Eclipse IDE has been installed in the /Applications directory, with the Android Devel-
opers Toolkit already installed as a plugin. Note: At this point, our systems staff are not
supporting Eclipse as a standard IDE; it is just provided for the purpose of trying out some
Android development. If you try to add other Eclipse plugins in your local conguration
directory, youre on your own. Drag the Eclipse icon on to your dock so that you can launch
it with a single click.
Were now going to do a whirlwind tour of developing a Java application on Eclipse. For
the in-depth stuff, there is plenty of documentation on Eclipses Web site.
Launch Eclipse. The IDE likes to store all of its project les in a particular directory, which it
refers to as the workspace. On the initial launch, it will prompt you for where youd like
this to be. The default is quite sensible: a directory called workspace, under Documents. If
youre happy with that, check the Use this as default option and click OK.
At this point you get a desktop-like thing with a st-full of mystery-meat icons. Ignore them
all and look up at the top menu-bar (the Eclipse menu along the very top of your screen).
To start doing Java programming, click on Window > Open Perspective > Java.
At some point in the next minute or two, Eclipse is going to realise that it has the Android
Developers Toolkit installed, but no knowledge of where it lives, and it will prompt you to
set the appropriate path in the preferences. It is safe to dismiss this warning now, and ignore
it until after you have established that a plain Hello World program works.
Eclipse is also likely to prompt you to give Google your usage statistics.
Choose File > New > Java Project, name it HelloWorld, and accept all the other defaults.
Click the Finish button on the bottom right-hand corner.
At this point, you should see a small projects window in the top left of the Eclipse environ-
ment, and not a lot else. To create a Hello World application, click on File > New > Class,
enter HelloWorld as the class name, check the box that says to create a main method stub,
and click Finish.
The editing window should now open in the bottom left of the Eclipse desktop. (You can
make more room by closing the welcome tab on the right hand side.) Add a
System.out.println() call to print out the message Hello World and test it by clicking on
the green Run button. A console window should pop up along the bottom of the Eclipse
desktop, and you should see your message printed out.
Eclipse is massive! It contains some great features like support for unit testing, refactoring,
and source code control.
1.4 Making Eclipse Aware of the Android SDK
Now we need to tell Eclipse where the Android SDK directory is.
6 LAB 1. ECLIPSE & ANDROID INSTALLATION
1. Select Eclipse > Preferences... to open the Preferences panel.
2. Select Android from the left panel.
3. For the SDK Location in the main panel, click Browse... and locate the SDK directory
under /Applications. (That is, get the SDKnot the toolsdirectory into the top
drop-down menu.)
4. Click Apply, then OK.
At this point, Eclipse and the Android SDK should be all ready to write your rst Android
application.
1.5 Hello Android!
Now, were ready to set up an Android project, get an Android Virtual Device going, and
see the output of a program running on the Android emulator.
The best way to do this is to follow the Hello, World tutorial from Google, which you can
nd at http://developer.android.com/guide/tutorials/hello-world.html.
Note: the project-setup window will require you to provide a package name, which must
have at least two components. Something like cosc346.lab1 would be appropriate here.
Another thing to note: the rst time the Android emulator starts up, it takes a long time
to boot. In fact, it can look like its hanging. Just wait for a minute or two while the word
ANDROID is displayed on the screen, and it will eventually complete the boot process
and load your program. It is best to leave the emulator up and running while you develop,
so that the only waiting time you have is for your .apk package to to be transferred to the
emulator.
You should follow through to the end of point 3 of the section titled Upgrade the UI to an
XML Layout. (If you have time, it would be of benet to you to continue on through to
the end of the tutorial, as there is useful stuff regarding the project manifest les, and on
debugging your application in Eclipse.)
1.6 Extras
If youre wanting some more to do you could:
1. Have a play around with the emulatortry out the browser and other features. How
easy is the user interface to use?
2. Take a look at: http://developer.android.com/videos; in particular, the Androidol-
ogy segments. (Part 1 of 3 - Architecture Overview; Part 2 of 3 - Application Lifecycle;
Part 3 of 3 - APIs.)

Vous aimerez peut-être aussi