Android2FirstProject

advertisement
Android 2: A First Project
Kirk Scott
1
2
• 2.1 Creating a New, Example Android
Application Project in Eclipse
• 2.2 Creating a Virtual Device, an Emulator
• 2.3 Running an App on the Emulator in
Eclipse
• 2.4 Running the Project on an Attached
Device Instead of an Emulator
3
2.1 Creating a New, Example Android
Application Project in Eclipse
4
• These overheads give a general overview of
what is involved in getting a sample
application to work—without even looking at
its code, how it works, or what Android
features it is making use of or depends on
• This is the initial monkey work mentioned in
the previous set of overheads
5
• These overheads are based on the initial
example app that was given in the tutorial on
the Android developer’s Web site at one time
• The Web site may or may not still use the
same example in the same form
6
• If you discover that something in the
overheads doesn’t seem to work, you can go
to the tutorial for additional information
• Unfortunately, as time passes, there may be
divergence between what you find in the
overheads and what you find on the Web
page
7
A New Android Application
• In the Eclipse menu take the option File, New,
Android Application Project
• The result of this is shown in the screenshot
on the following overhead
8
9
• In the first box, type in the following:
• My First App
• The other boxes will auto-complete as shown
in the following screenshot
• (If the other boxes don’t auto-complete, this is
the first stage where you will have to go fish)
10
11
• At the very top there is a warning about the
auto-completed package name
• I have always just used the placeholder that
they provide without any problems
• If things auto-completed as hoped, then you
click the Next > button
12
• I will not show the next screenshots for the
wizard
• Just keep on accepting the defaults and taking
Next until you reach the Finish option
• Notice how this is an area where you’re just
doing monkey work
• God knows what all of the options mean at
this point
13
• Once you click Finish “things will happen”
• They may happen slowly
• I have a reasonably powerful machine and it
takes things a while to happen
• There is a progress bar at the bottom which
gives you some indication of what’s going on
14
• The initial Welcome screen may remain visible
• If so, it covers up what has happened
• Minimize anything in the way, and with luck,
what you will see in the Eclipse environment
will look more or less like the screenshot on
the following overhead
15
16
• There are several things to notice in this
screenshot
• The simplest and most reassuring is that if you
can actually read the fine print, you will see
that My First App is a Hello World app
• Whenever a new app is created using the
defaults, this Hello World layout is provided by
default
17
• Obviously you’re not looking at code
• The name of the file that is being shown has
an XML extension
• What you’re seeing is the layout of the output
of the app
• This is a graphical representation of the layout
as provided by the development environment
• This is not the output showing in the emulator
18
• If you have an extremely good visual memory,
you might notice that this screenshot of the
editor doesn’t look quite like the one given at
the end of the previous set of overheads
• For example, the toolbar has disappeared
• Just like all development environments, you
can mistakenly (or intentionally) change
what’s shown
19
• For the purposes of these overheads, it’s just
as worthwhile to emphasize that fact as it
would be to try to make every screen shot
consistent
• Keep in mind that you can access the tools
through the menu if needed
20
• Also, under the Window option in the menu,
you can do various things to restore the
contents or appearance of the environment if
something seems to have gone missing
• For example, there is an option to show the
toolbar again, an option to open a
perspective, an option to reset a perspective,
and so on
21
2.2 Creating a Virtual Device, an
Emulator
22
Running the Project on a Virtual
Device, an Emulator
• If you want to run the project using the
emulator, you need to use the Android Virtual
Device Manager to create a new virtual device
• Recall that the Android Virtual Device
Manager tool was mentioned in the previous
set of overheads
• If you take that tool (or menu option) you
should arrive at something similar to the
screenshot shown on the following overhead
23
24
• Click the New button to create an Android
Virtual Device
• The window shown in the screenshot on the
following overhead comes up
25
26
• It’s shown on the following overhead with the
drop down list for Device expanded
27
28
• I tried using various devices
• For initial test purposes I finally settled on the
device at the bottom of the list
• My theory was that emulation for older,
simpler devices might work better or faster
than for newer devices
• On the following overhead the form is shown
filled out with representative values for that
simple device
29
30
Using Snapshot
• Another mystery:
• At the bottom is a checkbox for the Snapshot option
• Certain reference sources waffle on the desirability of
the Snapshot
• I recommend not using Snapshot
• It is possible that this causes things to run more slowly
• The reference sources indicate that if you do select
Snapshot and then make other parameter changes
later, this may lead to unexplained problems
31
Warning
• Here is an example of what can go wrong
• I also tried making a virtual device that was a
Nexus tablet (the real hardware we’re working
with)
• The memory option defaulted to a RAM value
of 1024
• Unfortunately, when I continued with that
value of 1024, I got a mystery error
32
• My Web search informed me that if I changed
the RAM value to 512, things should be OK
• For the device I’ve chosen to illustrate with, I
took memory size values of 256
• For whatever reason, these values worked,
and for the time being, that’s good enough for
me
33
• The usual rules apply:
• When in doubt, accept the default, if one is
given, or make a random guess
• When things inevitably go wrong, do a Web
search to try and find out what it was
• Stackoverflow has helped me many a time
34
Continuing with Virtual Device
Creation
• Once you’ve got the parameters set, click on
OK
• The system can drag on for a noticeable
amount of time creating the virtual device
• Successful completion is indicated by the
appearance of the device in the list as shown
in the screenshot on the following overhead
35
36
• There is no final OK step after virtual device
creation
• You’re done—and you have to close this
window
• If you just minimize it, it sits there but is still
the active window
• To go on from here, close this window
37
2.3 Running an App on the Emulator
in Eclipse
• There are several different ways of running an
app in the emulator
• Later on you will find out that you can create
run configurations and debug configurations
which are linked to a project
• That may be a fine way of doing business, but
it’s more complicated than necessary initially
38
• Starting the emulator can take a lot of time
• In theory, running an app (without an
attached device) will cause the system to start
the emulator and then cause the app to run
on it
• My experience tells me that this is a bad
approach
39
• The preferable approach:
• Start the emulator and wait patiently until you
have confirmed that it has started
• Then run the app in it
• Waiting for the emulator to start may drive
you crazy, but this is just a little craziness
40
• These are bigger crazinesses:
• Thinking your app doesn’t work, when in fact
all that’s happening is the slow starting of the
emulator
• Worse yet, you get impatient waiting for your
app to run, don’t realize that the delay is just
the slowness of starting the emulator, and you
break something by taking an action before
the emulate/run sequence is complete
41
•
•
•
•
I repeat:
Start the emulator first
Wait patiently for it to start
Then try to run your app
42
Starting the Emulator
• The Android Virtual Device screen is shown
again on the following overhead with the
created virtual device in it
• The screenshot shows the device as being
selected and you’ll note a Start… button on
the right hand side
43
44
• If you click the Start button the following
screen should appear
• This is the magic moment
• Click Launch
45
46
• After clicking launch, you should see the
screen with the progress bar shown on the
following overhead
• So far so good
47
48
• Even before the previous screen goes away, or at
the very least, as soon as it finishes, you should
see the emulator on the screen
• It’s conceivable that you’ll have to close some
things to find it
• It may be hidden
• In any case, the initial stage of launch shouldn’t
take a long time, and you will see something like
this, depending on what type of device you chose
to emulate
49
50
Even If Things are Going OK, This is the
Painful Part
• What you need now is patience (potentially a lot
of it) and faith
• The Android message on the emulated device will
flash white for an indeterminate period of time
• The emulator is still launching
• As long as that process is going on, there’s
nothing else that you should do
• Find something to kill a little time with, because a
watched pot never boils
51
Edging Towards Success
• Eventually, with good luck, the emulator will
look as shown on the following overhead
• In the screenshot that is shown, the emulator
screen is grayed out
52
53
• If your emulator is grayed out, click and drag
over the emulator screen with the mouse
• This should wake it up, as shown on the
following overhead
54
55
• You’re directed to touch the circle
• On your desktop, click your mouse on it
• If the apps are shown, as they are in the
following screenshot, you have been
successful
• The emulator has been launched and is
running
56
57
Running the Project in the Emulator
• If you want to run the project (without setting
up run or debug configurations) you need a
source file for the project open in the Eclipse
environment
• We haven’t actually seen that yet
• What we’ve seen is the default view, which
shows the app layout
• This is repeated on the following overhead for
reference
58
59
• The directory down the left hand side is the
project explorer
• In it you need to find
MyFirstApp/src/com.example.myfirstapp/MainAc
tivity.java
• Double clicking on that will open the file
containing the source code for the app in the
editor window
• The screenshot on the following overhead
illustrates this
60
61
• In the interests of thoroughness, you can build
the project before running it
• In the menu you would take Project, Build All
• Sometimes you can also solve mystery
problems by taking the Project, Clean… option
• The first time, and most times, you run
something you will not have to take
preliminary steps like these
62
• To run the app, just go to the Run menu in the
menu bar and take the Run option in it
• Or, there is a Run tool in the toolbar which you
can click on
• There’s no need to take the dropdown option
on the tool
63
• The first time you to try to run a app (a
project), you may get another dialog box
asking what kind of thing you’re trying to run
• You’re given a list of choices, and the choice
you want to make is “Android application”
• This dialog is shown on the following
overhead (for the Wari app, not for
MyFirstApp)
64
65
• After choosing the right run type, once again
there may be a certain amount of waiting
• With luck, when you look at the emulator,
you’ll see what’s shown in the screen shot on
the following overhead
• (This is back to MyFirstApp, not Wari)
66
67
• Click and drag over the emulator screen, and it
comes to life, as shown on the following
overhead
68
69
• Just to reiterate the obvious:
• What was shown earlier was the static layout
of the app as displayed in the development
environment
• That was essentially a visual editor view of
how the app would look when it ran
• What we have just seen is the actual
appearance of the running app in the
emulator
70
2.4 Running the Project on an Attached
Device Instead of an Emulator
71
• Most of you have probably run an app that
was already on a device
• Some of you have probably downloaded an
app from the Internet, installed it, and run it
on a device
• We are now interested in the steps for
installing an app directly from a development
environment and running it on a device
72
The 3 Steps for Installing and Running an
App from Your Development Environment
• Getting the Device Ready
– You have to get into developer mode so that you
it’s possible to transfer the app to the device
• Downloading and Installing the Driver
– You need a USB driver in order to transfer through
a USB port
• Running the App on the Device
– Then you can run the app on the device
73
Background
• For reference purposes, these overheads were
prepared with the following:
• Windows 7
• Eclipse, as already described
74
• A Nexus 7 tablet
• The fact that it was the Nexus 7 tablet is
relevant to the specific instructions for getting
the device ready
• The process of accessing the device settings
an making it possible to install an app in the
way described may differ on different kinds of
hardware
75
Getting the Device Ready
•
•
•
•
•
On the running device, go to the App screen
Tap on Settings
Under System, find About tablet
Tap Build number 7 times
When you return from doing this you should
find Developer options under System
76
• Take Developer options
• Under Debugging, enable USB debugging
• You can now connect the device to your
system using a USB cable
77
Downloading and Installing the Driver
• For information on the Nexus 7 USB driver, if
you want any, go to the Android Web site:
• http://developer.android.com
• A search on “USB driver” will take you to a
page that has information on the driver
• The driver is only necessary when developing
in a Windows environment
78
• You have a choice on getting the driver
• Either you can download from the Web page
mentioned above
• Or you can use the Android SDK Manager
from the ADT bundle in Eclipse
• If you’re following these overheads and using
the development environment as explained in
them, using the SDK Manager is easier
79
• Using the manager is so easy, there’s not
much to say about it
• Remember that the tool was introduced in the
previous set of overheads
• Find the tool in the Eclipse environment and
click on it
• Find the line for the driver, possibly scrolling
down to do so
• Click that line and follow any instructions
80
Doing it by Hand (Not Recommended)
• If for some reason you want to download and
install the driver the hard way, here is a brief list
of the steps involved:
• Note which folder the driver is saved in
• Right click on the “computer” icon on your
machine
• Take the Manage option
• Select the Device Manager on the left
• Find and expand the Other devices
81
• Find the Nexus 7 device in the list
• You may find the Nexus 7 device somewhere else
depending on your situation
• When you find the Nexus 7 device, right click on
it
• Take the Update Driver Software… option
• Follow the directions and browse to the location
where the driver was saved to
• Blah blah blah
82
Running the App on the Device
• The assumption at this point is that you
successfully installed the driver and your
device is attached with a USB cable
• Open your app in Eclipse
• Take the Run tool or the Run option in the
menu
83
• This should cause the Android Device Chooser
to appear on the screen, with the Nexus 7
device in the list
• This is shown on the following overhead
• Note that the area showing the actual
attached device is distinct from the area that
shows any emulators
84
85
•
•
•
•
Select your device and click OK
The magic should happen
The app should run on your device
In addition to running, it will effectively be
installed on the device at the time it runs
• If you go to the apps screen you’ll find the
little green Android icon for it and you can run
it from there in the future
86
Woes
• On your system you may discover that the
Google driver doesn’t install through the tool
• You may also discover that you follow the
directions for installation by hand, and MS
Windows overrides this installation and
replaces the driver with its preferred driver—
which is not the correct one for a Google
device
87
• This happened to me on my system
• I searched the Web for solutions and spent
many hours trying, but I couldn’t solve the
problem
• It seems that some versions of Windows (for
example, Windows 7 Professional) are locked
down in some way, and if there is a way to
stop the default “helpful, secure” behavior, I
have not found it
88
• If this happens to you:
• I can offer sympathy
• I can suggest that you look for a solution, and
if you find one, hope that you share it with me
for the benefit of future students
• In the case of no success, I can only
recommend that you get access to a machine
with a different version of Windows and see if
you can get it to work there
89
• I did get this to work on one machine with a
different version of Windows
• And for what it’s worth:
• Note that the special Google driver is only
needed in a Windows environment
• If, by chance, you’re developing in Linux
environment, for example, you are apparently
free of this curse.
90
Summary and Mission
• This is the end of the description of the first
project
• As noted, it was only about nuts and bolts
• The presentation had nothing to do with code
or the components of the app
• Those topics will be pursued in the following
sets of overheads
91
• You have two missions, neither of which are
graded homework:
• 1. Get “Hello World” to work on an emulated
virtual device in whatever development
environment you’re using
• 2. Get “Hello World” to work on a real
attached Android device
92
The End
93
Download