Android Programming: Installation, Setup, and Getting Started

bravesnailsSoftware and s/w Development

Jun 7, 2012 (5 years and 3 months ago)

677 views

© 2011 Marty Hall
Android Programming: Installation,
Android Programming: Installation,
Setup, and Getting Started
Originals of Slides and Source Code for Examples:
http://www.coreservlets.com/android-tutorial/
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
© 2011 Marty Hall
For live Android training, please see courses
t htt//l t/
a
t htt
p:
//
courses.coreserv
l
e
t
s.com
/
.
Taught by the author of Core Servlets and JSP, More
Servlets and JSP
and this Android tutorial Available at
Servlets and JSP
,
and this Android tutorial
.
Available at
public venues, or customized versions can be held
on-site at your
organization.
C d l d d t ht b M t H ll
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.

C
ourses
d
eve
l
ope
d
an
d

t
aug
ht

b
y
M
ar
t
y
H
a
ll
– Android development, JSF 2, servlets/JSP, Ajax, jQuery, Java 6 programming, custom mix of topics
– Ajax courses can concentrate on 1 library (jQuery, Prototype/Scriptaculous, Ext-JS, Dojo, etc.) or survey several
• Courses developed and taught by coreservlets.com experts (edited by Marty)
– Spring, Hibernate/JPA, EJB3, GWT, RESTful and SOAP-based Web Services
Contact hall@coreservlets.com for details
Topics in This Section
• Installing the Software and Documentation

Java 6
– Eclipse

Android SDK base
Android

SDK

base
– Eclipse ADT Plugin
– Updated SDK components
AVD (A d id Vi l D i )

AVD

(A
n
d
ro
id

Vi
rtua
l

D
ev
i
ce
)
• Running Apps

Import and test an existing app
Import

and

test

an

existing

app
• Run on emulator
– Create and test a new app

Run on emulator

Run

on

emulator
– Seeing standard output in the DDMS
– Deploy app to USB-connected Android device
5
© 2011 Marty Hall
Installing the Software
Installing

the

Software

and Documentation
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
JDK for Java SE 6
• Overview
– Java 6 is newest, best, and especially fastest version
• Java 5 supported by Android but not recommended
F PC Li S l i f ll di i

F
or
PC
,
Li
nux,
S
o
l
ar
i
s,
f
o
ll
ow
di
rect
i
ons at
http://www.oracle.com/technetwork/java/javase/downloads/

Get JDK,not just JRE
Get

JDK,

not

just

JRE
• Get SE (Standard Edition), not EE or Micro Edition
• Don’t get version with the NetBeans IDE
– For MacOS, Java is preinstalled & updated automatically
7
Installing Java SE 6
• Install Java 6

http://www.oracle.com/technetwork/java/javase/downloads/
Use this version. The “JDK – Java
Development Kit” includes compiler for
.java files, whereas the “JRE – Java
Runtime Environment” is only for
executing prebuilt .class files.
This tutorial uses Eclipse,
so do not use this link.
As of summer 2011, there
is no NetBeans plugin for
Android development So
Android development
.
So
,

Eclipse is strongly
recommended even if you
normally use NetBeans for
Java development.
However, IntelliJ IDEA has
A
ndroid
s
u
pp
ort:
s
ee
s pp s
http://www.jetbrains.com/
idea/features/
google_android.html
After downloading, run installer and accept all defaults.
Eclipse
• Overview
E li i f IDE (I d

E
c
li
pse
i
s a
f
ree open source
IDE

(I
ntegrate
d

Development Environment). Support for Java, HTML,
CSS, JavaScript, C++, PHP, and more.
– Google has free Eclipse plugin to integrate with the
Android SDK.
• Features
– General
• Checks your syntax as you type

Automatically compiles every time you save file

Automatically

compiles

every

time

you

save

file
• Refactoring, debugging, templates for common tasks, etc.
– Android-specific
D l t A d id l t

D
ep
l
oy apps
t
o
A
n
d
ro
id
emu
l
a
t
o
r
• Configure virtual environments
• Drag-and-drop GUI builder
9
Installing Eclipse
• Go to eclipse.org, click on “Downloads”
– Results in
http://eclipse.org/downloads/
– Can use either “for Java” or
“for Java EE”
“for

Java

EE”
.
• I use EE version since I also use Eclipse for Web apps.
– Latest version
(
3.6 – Helios
)
recommended.
(
)
• Previous version (3.5 – Ganymede) still supported
• Older versions (3.4 and earlier) not supported
10
Running Eclipse
• Unzip the downloaded file (no installer!)
– Call the folder you unzip into “installDir”
• Double click eclipse.exe
F
i llDi
/bi

F
rom
i
nsta
llDir
/bi
n
• Click on

Workbench

icon
Workbench

icon
– Next time you bring
up Eclipse, it will
come up in workbench automatically
• Shortcut
M d l t E li li k th i d kt

M
any
d
eve
l
opers pu
t

E
c
li
pse
li
n
k
on
th
e
i
r
d
es
kt
op
• R-click eclipse.exe, Copy, then go to desktop, R-click, and
Paste Shortcut (not just Paste!)
The Android SDK
• Overview
– Androi
d
-specific libraries
– Dalvik (Android virtual machine) compiler

Android emulator (to run without physical device)
Android

emulator

(to

run

without

physical

device)
– DDMS debugging environment
• Documentation
– Installation
• http://developer.android.com/sdk/installing.html

Developer

s Guide
Bookmark these URLs!
Developer s

Guide
• http://developer.android.com/guide/developing/index.html
– JavaDoc (API Reference)
http
://developer android com/reference/classes html

http
://developer
.
android
.
com/reference/classes
.
html
– Tutorials and articles
• http://developer.android.com/resources/index.html
12
Installing the Android SDK
• Download and run installer
– From http://developer.android.com/sdk/
• I install in C:\android-sdk-windows
S t b i SDK b t it t

S
e
t
s up
b
as
i
c
SDK
,
b
u
t
om
it
s many componen
t
s
• Detailed instructions
http://developer android com/sdk/installing html

http://developer
.
android
.
com/sdk/installing
.
html
• Postponed step

After installing Eclipse plugin we will run the SDK
After

installing

Eclipse

plugin
,
we

will

run

the

SDK

updater to get the important missing components
• Easier to do it from Eclipse than from command-line tool
d ib d i li k b S i lid ft E li
d
escr
ib
e
d

i
n
li
n
k
a
b
ove.
S
ee upcom
i
ng s
lid
e a
ft
er
E
c
li
pse
ADT installation.
13
Eclipse ADT Plugin
• Overview
– ADT (Android Development Tools) provides many
useful features accessible directly in Eclipse

Integration between Eclipse & Android command
-
line tools
Integration

between

Eclipse

&

Android

command
line

tools
• Drag-and-drop GUI builder
• Many development and debugging aids
D t il d i t ti

D
e
t
a
il
e
d

i
ns
t
ruc
ti
ons
– http://developer.android.com/sdk/eclipse-adt.html
14
Installing Eclipse ADT
• Steps
– Start Eclipse
– Help Install New Software …
Click

Add

in upper
right

Click

Add

in

upper
-
right
– In Add Repository, for Name enter “ADT Plugin” and for
Location enter htt
p
s://dl-ssl.
g
oo
g
le.com/android/ecli
p
se/
p
g g p
– Click OK, select checkbox
next to Developer Tools, Next,
see packages to be installed
see

packages

to

be

installed
,
accept license, Finish
– Update the ADT plugin.
• Help Check for Updates
15
Configuring Eclipse ADT
• Set SDK Location
– Window Preferences Androi
d
– Click Browse and point at place you installed the SDK

Optional:disable sending stats to Google

Optional:

disable

sending

stats

to

Google
– Window Preferences Android Usage Stats
16
Updating SDK
Components
Components
• Run SDK manager
– Window Android SDK and AVD Manager
– Click on “Available packages” on left
Select all non
obsolete

Android Repository

entries

Select

all

non
-
obsolete

Android

Repository

entries
• Select checkbox next to “Android Repository”, optionally
unselect obsolete entries, click “Install Selected”
R f
l
ti

R
uns
f
or a
l
ong
ti
me
• Detailed instructions

http://developer android com/

http://developer
.
android
.
com/
sdk/adding-components.html
17
Android Virtual Devices (AVDs)
• Overview

An AVD (Android Virtual Device) is an Android Emulator
configuration that lets you model an actual device by defining
hardware and software options
• Idea
– Define several AVDs at different Android API levels to test
against At least:
against
.
At

least:
• Recent version (e.g., 3.x or 2.3.3)
• Most common version (2.2 as of 2011)
To see statistics for versions of currently used Android devices see

To

see

statistics

for

versions

of

currently

used

Android

devices
,
see

http://developer.android.com/resources/
dashboard/platform-versions.html
• Detailed instructions
– http://developer.android.com/guide/developing/devices/
managing-avds.html
18
Defining an AVD
• Defining
– Window Android SDK and AVD Manager
– Click on “Virtual devices” on left. Click Add, choose
options You can (should!) create several AVDs
options
.
You

can

(should!)

create

several

AVDs
.
• Options
– Tar
g
et
(
i.e.
,
tar
g
et API version
)
g (,g )
• 3.x to test tablet features
• 2.3.3 to test new phone features

2 2 to test on most common phone

2
.
2

to

test

on

most

common

phone
– SD Card size
• Can be omitted. Or, choose middle
f th d l
4 GB
o
f

th
e roa
d
va
l
ue, e.g.,
4

GB
– Skin
• Use default for the tar
g
et
y
ou chose
19
© 2011 Marty Hall
Running Apps on
Running

Apps

on

Emulator
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Big Ideas
• Running apps

Soon, you want to learn how to write your own apps. First,
however, we will practice running existing apps.

Ways to run
Ways

to

run
– Covered here
• On the Android Emulator. Deploy directly from Eclipse.

During development, do your normal testing here
• On an Android device. Deploy from your PC via USB.
– Covered in later tutorial sections
• On an Android device. Deploy from a Web site.
• On an Android device. Deploy via email.
• On and Android device. Deploy from the Android Market
• Writing apps
– Covered in later tutorial sections. That is the fun part!
21
Running the HelloAndroid App
in Emulator
in

Emulator
• HelloAndroid
S
i l d l d i

S
uper-s
i
mp
l
e app to test
d
ep
l
oyment an
d
execut
i
on steps.
• For students in live classes, this project is already in your
Eclipse workspace.
F li d d l d j t f th G tti St t d

F
or on
li
ne rea
d
ers,
d
own
l
oa
d
pro
j
ec
t

f
rom
th
e
G
e
tti
ng
St
ar
t
e
d

section of Android tutorial and import into Eclipse.
– http://www.coreservlets.com/android-tutorial/
St t it

St
eps
t
o run
it
– Import it if necessary

File

䝥湥牡G

䕸楳瑩湧 偲潪散瑳P
䙩汥


䝥湥牡G


䕸楳瑩湧

偲潪散瑳P
– R-click on project on L side of Eclipse
– Run As Android Application
L
it hil l t i iti li

L
on
g
wa
it
w
hil
e emu
l
a
t
or
i
n
iti
a
li
zes
• Do not close emulator when done
– Next time, app will come up much faster
22
HelloAndroid: Result
23
Code will be discussed in next tutorial section.
© 2011 Marty Hall
Making Your Own
Making

Your

Own

Android App
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Making Your Own Android App:
Basics
Basics
• Idea
– When you create a new app, it has simple “Hello World”
functionality built in.

So you can create and test an app without knowing
So
,
you

can

create

and

test

an

app

without

knowing

syntax (which is not discussed until next tutorial section)
• Steps




– File

N
敷e

偲潪散琠

䅮摲潩搠

䅮摲潩搠偲潪散A
• Once you do this once, next time you
can do File New Android Project
– Fill in options as shown on next page
– Run new project as shown previously

R
click

Run As


R
-
click


創R




䅮摲潩搠䅰灬楣慴楯A

Making Your Own Android App:
Setting Project Options
Setting

Project

Options
• New Android Project Settings
j
– Pro
j
ect Name
• Eclipse project name. Follow naming convention you use for Eclipse.
– Build Target
Th A d id i th t t t F t h h

Th
e
A
n
d
ro
id
vers
i
on
th
a
t
you wan
t

t
o use.
F
or mos
t
p
h
one apps, c
h
oose
2.2, since that is the most common version in use worldwide.
– Application name

Human
-
readable app name

title will be shown on Android title bar

Human
-
readable

app

name


title

will

be

shown

on

Android

title

bar
.
– Package name
• Apps on a particular Android device must have unique packages, so use
com.
y
ourCom
p
an
y
.
p
ro
j
ect
y p y p j
– Create Activity
• The name of the top-level Java class
– Min SDK Version
• Number to match the Build Target. Summarized in the Eclipse dialog, but
for details, see http://developer.android.com/guide/appendix/api-levels.html
26
Making Your Own Android App:
Setting Project Options
Setting

Project

Options
Eclipse project name
Android version that you want to run on
Human-readable app name
Package. Use naming convention to ensure uniqueness
Java class name
27
Java class name
Number corresponding to build target
Running New App on Emulator
• Builtin functionality

N
ewly created projects automatically have simple “Hello
World” behavior

Execution steps

Execution

steps
– Same as with any project
• R-click Run As 
Android Applicaton
– Reminder: do not close
emulator after testing.
Emulator takes a long time
to start initially, but it is
relatively fast to deploy
a new or a changed
a

new

or

a

changed
project to the emulator.
28
© 2011 Marty Hall
Seeing Standard Output
Seeing

Standard

Output

in DDMS
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
DDMS Basics
• Idea
S (
l ik
b i S i ) i l h hi
– DDM
S

(
Da
l
v
ik
De
b
ug Mon
i
tor
S
erv
i
ce
)

i
s a too
l
t
h
at supports many t
hi
ngs
• Simulate incoming calls in emulator
• Set GPS locations in emulator

See print statements and runtime errors

See

print

statements

and

runtime

errors
• Set locations and take screenshots of actual Android device
• Simple usage now
St t DDMS

St
ar
t

DDMS
• Window 
Open Perspective DDMS

Once you do this once,you
Once

you

do

this

once,

you
can click on “DDMS” at
top right of Eclipse
• Click on “Java” to return to code
– See print statements
• Look in LogCat window at bottom
• Type part of output into Filter field to see specific output
30
DDMS Output (HelloAndroid)
• Code
– Put System.out.println
in main onCreate method

See code in screenshot
See

code

in

screenshot

on previous page
• onCreate and other syntax
discussed in next section
discussed

in

next

section
Output of System.out.println
31
Entered so it is easier to find specific output among
the many informational messages that emulator prints
© 2011 Marty Hall
Running Apps on
Running

Apps

on

Physical Android Device
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Running Apps on Android
Device
Device
• Idea
Th j i f i ill b A d id l B

Th
e vast ma
j
or
i
t
y
o
f

y
our test
i
n
g
w
ill

b
e on
A
n
d
ro
id
emu
l
ator.
B
ut
sometimes you want to test on a physical phone or other Android
device to test compatibility and to use camera, GPS, contact list, etc.
• You first make a signed application package (YourApp.apk), then
you have various options for sending it to the phone
• O
p
tions
p
– Covered here
• Connect phone via USB, use adb to deploy
Covered in later tutorial section

Covered

in

later

tutorial

section
• Submit app to Android marketplace
– Can learn on your own
E il
k
fil il dd f h

E
ma
il
ap
k
fil
e to ema
il
a
dd
ress o
f
p
h
one
• Deploy apk file to a Web site, then connect there from phone. Must
set MIME type to application/vnd.android.package-archive.
33
Deploying via USB Connection
• Prereq: install drivers for Android device
– Plug phone (or other Android device) into computer.
• Recent OS’s will find drivers automatically. If not,
download from device manufacturer. See list at
http://developer.android.com/sdk/oem-usb.html
• Steps
A d id d i

A
n
d
ro
id

d
ev
i
ce
• Enable USB debugging
• Allow unknown sources
– Eclipse
• Export signed application package (YourApp.apk)
adb

adb
• Go to sdk-install-dir\tools (or put that folder in PATH)
• Run “adb install C:\...\YourApp.apk”
34
Configuring Android Device
• Enable USB debugging
– Settings Applications 
Development

Required:USB debugging
Required:

USB

debugging
– Allows PC to send commands
via USB
• O
p
tional: Sta
y
awake
p y
– Phone/device won’t sleep when
connected via USB
• Optional: Allow mock locations
– Let PC send fake GPS locations
• Allow unknown sources
S tti

A li ti


S
e
tti
ngs

A




潮猠

啮歮潷渠獯畲捥U
35
Exporting Application Package
• Every time: export
– R-click Eclipse project
– Android Tools Export
Signed Application Package
Signed

Application

Package
• First time: set up
ke
y
store
y
– You will be prompted for
location for keystore and
asked for password Key will be
asked

for

password
.
Key

will

be
created and app will be signed
with this key, which is in
valid format but is not counter-
signed by trusted third party.
36
Installing with adb
• Connect device/phone via USB
– With drivers installed and settings from previous slide
• Open a command window
Wi d S M

R

d

Wi
n
d
ows
S
tart
M
enu

R
畮u


d
• Go to “platform-tools” folder of SDK
DOS>
cd
C:
\
android
sdk
windows
\
platform
tools

DOS>

cd
C:
\
android
-
sdk
-
windows
\
platform
-
tools
• A good trick is to navigate to folder in Windows Explorer,
then copy address, then type “cd ” and paste address
R “
db
i t ll C
\
th
t
\
Y A k”

R
un

a
db
i
ns
t
a
ll

C
:
\
pa
th
-
t
o
\
Y
our
A
pp.ap
k”

– DOS> adb install C:\Users\Marty\...\HelloAndroid.apk
1439 KB/s (13262 bytes in 0 009s)
1439

KB/s

(13262

bytes

in

0
.
009s)
pkg: /data/local/tmp/HelloAndroid.apk
Success
37
Running App on Device
• Go to installed apps
• Tap new app
– Remember that the human
readable name (Application
readable

name

(Application

Name from new Android
Project wizard) is shown,
not the Java class name
• If you want to install
updated version
updated

version
– Uninstall old version first

Sett
in
gs
A
pp
li
cat
i
o
n
s

Sett gs
pp cat o s
YourApp Uninstall
38
© 2011 Marty Hall
Wrap-Up
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Summary
• Install software
J 6 E li A d id SDK E li ADT l i

J
ava
6
,
E
c
li
pse,
A
n
d
ro
id

SDK
,
E
c
li
pse
ADT
p
l
ug
i
n
• Bookmark documentation
– Developer’s Guide and more at developer.android.com
• Update/configure software
– Set SDK location in Eclipse
– Get updated components via SDK & AVD Manager
– Define at least one AVD to run apps on emulator
• Run apps

On emulator (usually).R
-
click project,Run As

䅮摲潩A A灰汩捡瑩潮p


敭畬慴潲

⡵獵慬汹⤮

R
捬楣c

灲潪散琬

創R




䅮摲潩A

䅰灬楣慴楯渮
– 佮O灨祳楣慬p摥癩捥
潮捥⁩渠愠睨楬攩⸠䉵楬搠獩杮敤d慰a a湤⁵獥⁡摢n
• Make new app
File

New

Project

Android

Android Project

File


乥N


偲潪散P


䅮摲潩A


䅮摲潩A

偲潪散P
• See output of print statements
– In LogCat window of DDMS
40
© 2011 Marty Hall
Questions?
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.