40
Android Mobile Phone Development

Android Mobile Phone Development

Embed Size (px)

Citation preview

Page 1: Android Mobile Phone Development

Android Mobile Phone Development

Page 2: Android Mobile Phone Development

Me First

dataStuff

ComputerStuff

http://stuff

x 10

neidetcher.com/android.html

Page 3: Android Mobile Phone Development

Why Android?

Page 4: Android Mobile Phone Development

Big Picture

Page 5: Android Mobile Phone Development

No Money | iPhone Dominates

simplyhired.com

dice.com

indeed.com

monster.com

careerbuilder.com

0 200 400 600 800 1000 1200 1400 1600

Job Listing Comparison

May 9, 2009

android javaiPhone

j2me

Page 6: Android Mobile Phone Development

No Money | Hackable & Pervasive?

● Canonical is creating framework to run Android apps on Ubuntu (netbook remix?) http://www.engadget.com/2009/05/26/canonical-giving-ubuntu-the-gift-of-android-apps/

● XML VM is porting Android apps to iPhone http://www.xmlvm.org/android/

● ASE to script for and on the phone (Lua, Python)

http://code.google.com/p/android-scripting/

● iPhone vs Android Dev Comparison http://greensopinion.blogspot.com/2009/07/android-versus-iphone-development.html

● Garmin Adopts Android http://techgeist.net/2009/06/garmin-officially-adopts-android-ditches-in-house-linux-os/

● Android on a Sony?

http://www.engadget.com/2009/06/08/sony-building-android-based-walkman-and-pnd-for-2010-launch/ http://phandroid.com/2009/07/04/sony-ericsson-making-xperia-android-holy-smokes/

Page 7: Android Mobile Phone Development

Android | Timeline● 2001: Google starts filing mobile patents

● 2005 July: Google Acquires Android Inc

● 2005 Oct: Dan Bornstein joins Google to work on Dalvik

● 2007 Nov: Open Handset Alliance is formed and Android SDK is released

● 2008 Oct: T-Mobile G1 is released

● 2009 Jan: Palm Pre announced

● 2009 Feb: Pay apps available on market

● 2009 Jun: Samsung Android phones in EU

● 2009 Nov: Motorola releasing Android phone

● 2009 Aug: new T-Mobile Android phone

● 2009: Possible Acer, Dell and HP net-books

Page 8: Android Mobile Phone Development

Android | Open Handset Alliance

47 Companies

Sprint, T-Mobile, Telecom Italia, Vodafone, Intel, Texas Instruments, NVIDIA, Asus, Garmin, HTC, LG, Motorola, Samsung, Sony, Toshiba...

Page 9: Android Mobile Phone Development

Android | MarketAndroid | Market

http://market.android.com/publish/Home

Page 10: Android Mobile Phone Development

Android

● Built on Linux● Not POSIX● Custom C library

(bionic)

http://source.android.com/download

Androidis a Linux

Dalvik VM

Android has many Dalvik VMs

Dalvik VMDalvik VM

Page 11: Android Mobile Phone Development

Android | Open Source

Linux: GPLv2

WebKit: LGPLv2SQLite: Public Domain

Eclipse: EPL

QEMU: LGPL

Bionic: BSDAndroid: Apache2

Apache Harmony: Apache2

Development StackRuntime Stack

JFree Chart: LGPL

JCommon: BSD

Page 12: Android Mobile Phone Development

Android | Architecture

Dalvik VM

Core Libraries

WiFi Driver PowerWiFi Camera Keypad

WebKit

WiFi

OpenGLWebKitSQLite OpenGL bionic

ActivityTelephony NotificationActivity Location Package

Contacts

Telephony

Home Phone Browser EmailMap

Display

Page 13: Android Mobile Phone Development

Android | Google IO

● 3 types of agreements● 3 Ways to code● 20 handsets in 2009● Oprah moment● Donut

● Universal search where universe is phone● Text to speech● Gestures/ handwriting recognition

http://www.flickr.com/photos/francois/3959737/

Page 14: Android Mobile Phone Development

Dalvik

●Different beast

●Not Java byte code

●Not JVM

Page 15: Android Mobile Phone Development

Dalvik | Background

● Dan Bornstein joined Google 2005

● Made for low end machines● low ram (no disk swap

space)● battery

● Register based (not stack based)

● No JIT (it's on the road map)● most heavy lifting is in

native code

● Does no memory management or threading

http://www.flickr.com/photos/fremat/447395038/sizes/l/

Page 16: Android Mobile Phone Development

Dalvik | Pro/ Con of Approach

● All those Java All those Java coderscoders

● All that toolingAll that tooling● Don't have to deal Don't have to deal

with Sun and JCPwith Sun and JCP● Optimize byte-code Optimize byte-code

for hardwarefor hardware

● James Gosling James Gosling hates youhates you

● Sun (Oracle) hates Sun (Oracle) hates youyou

● Java fragmentationJava fragmentation● No more No more write once write once

run everywhererun everywhere

http://www.flickr.com/photos/catwommn/3374184512/http://www.flickr.com/photos/catwommn/3374184512/

Page 17: Android Mobile Phone Development

Dalvik | .dex vs .class

● Single constant pool for all classes

● Uncompressed● More pointing● More sharing

http://retrodev.com/android/dexformat.html

uncompressed jar compressed jar uncompressed dex

0

20

40

60

80

100

120

libsbrowser appalarm app

http://sites.google.com/site/io/dalvik-vm-internals (handouts)

Compression

Page 18: Android Mobile Phone Development

Dalvik | Other Languages?Dalvik | Other Languages?

● Java FX runs on Android (JavaOne 2008)

● Anything that dynamically reads .class files or dynamically manipulates Java binaries won't work without Android specific tweaks

● scala works

●http://www.youtube.com/watch?v=BtUX-GS36o8&feature=related●http://www.ibm.com/developerworks/opensource/library/os-eclipse-scala/

Page 19: Android Mobile Phone Development

Dalvik | SandboxDalvik | Sandbox

● Every app is a processEvery app is a process● process gets its own process gets its own

user iduser id● files is sandboxedfiles is sandboxed● SQLite DB is SQLite DB is

sandboxed sandboxed (just files (just files anyways)anyways)

http://www.flickr.com/photos/bauchidgw/2820627227/http://www.flickr.com/photos/bauchidgw/2820627227/

Page 20: Android Mobile Phone Development

Dalvik | Zygote

●Android creates Zygote at startup●Zygote loads up system libraries●Zygote finds out new app is starting●Zygote forks process, giving all Dalvik VMs (and apps) access to system libraries

Every new app isn't a VM cold startMinimize memory requirements by

sharing what we can

SystemLibraries

Zygote

Home

ApplicationCode

SystemLibrariesSystemLibraries

ApplicationCode

ApplicationCode

ApplicationCode

Email

Browser

Map

Page 21: Android Mobile Phone Development

Dalvik | Application Stack

Home

Email

Browser

Map

ActivityManager

Low memory killerwill look at LRU apps to destroy

Back buttondoes a popon the stack

Page 22: Android Mobile Phone Development

Development

● It's easy●Write code in Java●Lots of tooling●Lots of documentation

Page 23: Android Mobile Phone Development

Dev | Books

Amazon has 7 books total (Jun 14, 2009)

Page 24: Android Mobile Phone Development

Dev | Get Started

● Android SDKAndroid SDK● http://developer.android.com/sdkhttp://developer.android.com/sdk● local documentationlocal documentation● toolstools

● Eclipse pluginEclipse plugin● http://dl-ssl.google.com/android/eclipse/http://dl-ssl.google.com/android/eclipse/

Page 25: Android Mobile Phone Development

Dev | APIshttp://developer.android.com/reference/packages.html

java.awt.font java.beans java.io java.lang java.lang.annotation java.lang.ref java.lang.reflect java.math java.net java.nio java.security java.sql java.text java.util java.util.concurrent java.util.jar

junit.frameworkjunit.runnerorg.apache.httporg.jsonorg.w3c.domorg.xml.sax org.xmlpull.v1 org.xmlpull.v1.sax2

android.*dalvik.*

java.util.logging java.util.prefs java.util.regex java.util.zip javax.crypto javax.microedition.khronos.egl javax.microedition.khronos.opengles javax.net javax.security.auth javax.security.auth.callback javax.security.auth.login javax.security.auth.x500 javax.security.cert javax.sql javax.xml

...but

Page 26: Android Mobile Phone Development

Dev | 3rd Party & Workaround

● SCMXL (Apache State Chart XML project) has a good write-up http://commons.apache.org/scxml/usecases/scxml-stopwatch-on-android.html

●You can use 3rd Party APIs●If you need J2SE APIs that aren't there then include them from Apache Harmony

Page 27: Android Mobile Phone Development

Dev | Resource Class

mk19:~/code/scrumpoker>tree.|-- ...|-- res| |-- drawable| | `-- refres h.png| |-- layout| | |-- dis play.xml| | `-- main.xml| `-- values| `-- s tring s .xml`-- src `-- ...

mk19:~/code/scrumpoker>cat src/com/neidetcher/scrumpoker/R.java /* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */

package com.neidetcher.scrumpoker;

public final class R { public static final class attr { } public static final class drawable { public static final int refres h=0x7f020000; } public static final class id { public static final int displayPointsTextView=0x7f050000; public static final int pointsListView=0x7f050001; } public static final class layout { public static final int dis play=0x7f030000; public static final int main=0x7f030001; } public static final class s tring { public static final int app_name=0x7f040000; }}

aapt

Page 28: Android Mobile Phone Development

Dev | Activity

Page 29: Android Mobile Phone Development

Dev | View

public class DisplayActivity extends Activity{

TextView myTextView = null;

@Overridepublic void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);setContentView(R.layout.display);

// set value for the textmyTextView = (TextView) findViewById(

R.id.displayPointsTextView);String points = getIntent().getExtras().getString(

ScrumPokerActivity.SELECTED_POINTS);if (points.equals("100")){

myTextView.setTextSize(180);}

myTextView.setText(points);}

}

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/displayPointsTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:height="400sp" android:width="300sp" android:textColor="#FFF" android:gravity="center_horizontal|center_vertical" android:textSize="210sp" /></LinearLayout>

Page 30: Android Mobile Phone Development

Dev | IntentsActivity1 Intent Activity2

create(Activity2)

putExtra(key, value)

startActivity(intent)

onCreategetIntent()

getExtras.get(key):value

●Open Intents http://www.openintents.org/en/

Page 31: Android Mobile Phone Development

Dev | Intents & Mashability

TzChangeReceiverActivity

YourApp

AndroidManifext.xml

receiver

classes.dex

intent:ACTION_TIMEZONE_CHANGED

LocationManager

NarcissisticProxy

InterTubes

wheels down in PSTFTW!!!

Page 32: Android Mobile Phone Development

Dev | Packaging

.java

.java

.java

javac

.class

.class

.class

dx

classes.dex

.xml

.xml

.png

aapt

.xml

.xml

.png

classes.dex

.apk

Page 33: Android Mobile Phone Development

Dev | apkshiv:~/play>jar -xvf "Scrum Poker.apk"shiv:~/play>tree.|-- AndroidManifest.xml|-- Scrum Poker.apk|-- classes.dex|-- res| |-- drawable| | `-- refresh.png| `-- layout| |-- display.xml| `-- main.xml`-- resources.arsc

3 directories, 7 files

Page 34: Android Mobile Phone Development

Dev | dexdump

Virtual methods - #0 : (in Lcom/neidetcher/scrumpoker/ScrumPoker;) name : 'onCreate' type : '(Landroid/os/Bundle;)V' access : 0x0001 (PUBLIC) code - registers : 5 ins : 2 outs : 4 insns size : 47 16-bit code units000ad8: |[000ad8] com.neidetcher.scrumpoker.ScrumPoker.onCreate:(Landroid/os/Bundle;)V000ae8: 6f20 0100 4300 |0000: invoke-super {v3, v4}, Landroid/app/Activity;.onCreate:(Landroid/os/Bundle;)V // method@0001000aee: 1401 0100 037f |0003: const v1, #float 174128887730233583002069148843976425472.000000 // #7f030001000af4: 6e20 1e00 1300 |0006: invoke-virtual {v3, v1}, Lcom/neidetcher/scrumpoker/ScrumPoker;.setContentView:(I)V // method@001e000afa: 1401 0100 057f |0009: const v1, #float 176787343721803414747876762964537114624.000000 // #7f050001000b00: 6e20 1c00 1300 |000c: invoke-virtual {v3, v1}, Lcom/neidetcher/scrumpoker/ScrumPoker;.findViewById:(I)Landroid/view/View; // method@001c000b06: 0c01 |000f: move-result-object v1000b08: 1f01 0d00 |0010: check-cast v1, Landroid/widget/ListView; // class@000d000b0c: 5b31 0d00 |0012: iput-object v1, v3, Lcom/neidetcher/scrumpoker/ScrumPoker;.myListView:Landroid/widget/ListView; // field@000d000b10: 1400 0300 0901 |0014: const v0, #float 0.000000 // #01090003000b16: 2201 0b00 |0017: new-instance v1, Landroid/widget/ArrayAdapter; // class@000b000b1a: 5432 0800 |0019: iget-object v2, v3, Lcom/neidetcher/scrumpoker/ScrumPoker;.POINTS:[Ljava/lang/String; // field@0008000b1e: 7040 0700 3120 |001b: invoke-direct {v1, v3, v0, v2}, Landroid/widget/ArrayAdapter;.<init>:(Landroid/content/Context;I[Ljava/lang/Object;)V // method@0007000b24: 5b31 0c00 |001e: iput-object v1, v3, Lcom/neidetcher/scrumpoker/ScrumPoker;.myArrayAdapter:Landroid/widget/ArrayAdapter; // field@000c000b28: 5431 0d00 |0020: iget-object v1, v3, Lcom/neidetcher/scrumpoker/ScrumPoker;.myListView:Landroid/widget/ListView; // field@000d000b2c: 5432 0c00 |0022: iget-object v2, v3, Lcom/neidetcher/scrumpoker/ScrumPoker;.myArrayAdapter:Landroid/widget/ArrayAdapter; // field@000c000b30: 6e20 0800 2100 |0024: invoke-virtual {v1, v2}, Landroid/widget/ListView;.setAdapter:(Landroid/widget/ListAdapter;)V // method@0008000b36: 5431 0d00 |0027: iget-object v1, v3, Lcom/neidetcher/scrumpoker/ScrumPoker;.myListView:Landroid/widget/ListView; // field@000d000b3a: 5432 0b00 |0029: iget-object v2, v3, Lcom/neidetcher/scrumpoker/ScrumPoker;.mMessageClickedHandler:Landroid/widget/AdapterView$OnItemClickListener; // field@000b000b3e: 6e20 0900 2100 |002b: invoke-virtual {v1, v2}, Landroid/widget/ListView;.setOnItemClickListener:(Landroid/widget/AdapterView$OnItemClickListener;)V // method@0009000b44: 0e00 |002e: return-void catches : (none) positions :

/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);setContentView(R.layout.main);

displayPointsListView = (ListView) findViewById(R.id.pointsListView);int layoutId = android.R.layout.simple_list_item_1;

// populate the list view with an arrayArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,

layoutId, POINTS);displayPointsListView.setAdapter(arrayAdapter);displayPointsListView.setOnItemClickListener(mMessageClickedHandler);

}

shiv:~/play>~/opt/android-sdk-linux_x86-1.0_r2/tools/dexdump -d classes.dex

Page 35: Android Mobile Phone Development

Dev | Install Time

● What happens when you install the software...

● there is no JIT

● security

● verification of dex files

● Optimization

● specific to hardware● inlining● pruning● static linking

http://www.netmite.com/android/mydroid/dalvik/docs/dexopt.html

.odex

.dex

dexopt

Page 36: Android Mobile Phone Development

Dev | Good Android Code● sleep, react quickly, sleep● avoid allocation

Address[] addressArray = mgr.findAddresses()for(int ii = 0; ii < addressArray.length; ii++)

Address[] addressArray = mgr.findAddresses()for(Address currAddress : addressArray)

List<Address> addresses = mgr.findAddresses();for(Address currAddress : addresses)

http://kohlerm.blogspot.com/2009/04/analyzing-memory-usage-off-your-android.html

Page 37: Android Mobile Phone Development

Dev | Gotchas

● I accidentally imported the "super R" instead of my locally generated R. (2 hours)

● View layouts can't be camel cased. (30 minutes)● The tools directory in the Android SDK needs be

set to executable to run and for the Eclipse plug-in to work. (10 minutes)

● If your install didn't work on the phone you see a deceptive green check-mark. (10 minutes)

● Don't put heavy object graphs in putExtra()

● Development upgrade to Cupcake was kludgy for me

Page 38: Android Mobile Phone Development

One More Thing | NDK

● Native Dev Kit● C/ C++● Consistent libs● Docs, tutorials, code● Transparent to user

http://www.flickr.com/photos/sin_agua/2208439930/

http://android-developers.blogspot.com/2009/06/introducing-android-15-ndk-release-1.html

Page 39: Android Mobile Phone Development

Dev | Eclipse Plugin

●Logging●Emulator●Hierarchy Viewer●ADB

Page 40: Android Mobile Phone Development

Questions?http://neidetcher.com/android.htmlhttp://neidetcher.com/android.html