Android PlatformIntroduction
Hadipurnawan Satria, Ph.D
Source:Google Android PlatformZhahoui Wang, Angelous Stavrou, George Mason University
An Introduction to AndroidHuang Kuguang, Inha University
Android OS (CSE 2010)Xiao Ma
Internet users and Mobile phone users
Source: http://en.wikipedia.org/wiki/Digital_divide
Open Handset Alliance(OHA)
Mobile OperatorsHandset Manufacturers
Semiconductor Companies Software Companies Commercialization Companies
Source: http://www.openhandsetalliance.com
Android™ delivers a complete set of software for mobile devices: an operating system, middleware and key mobile applications.
Open
All application and Created equal
Breaking down application boundaries
Fast & easy application development
History of Android
• 2001 search service for wireless device
• 2005
– Acquire Android(Andy Rubin: Danger CEO, Development Sidekick of T-Mobile)
– Acquire Skia (2D Graphics for mobile device)
– Acquire RegWireless (Browser and Email for mobile device)
– Move Engineers from PlamSource (Dianne Hackborn, etc…)
• 2007 Nov 5: Android announced
• 2007 Nov 12: Android SDK released by OHA
• 2007 Dec 14: Bug-fix SDK released
• 2008 Jan 3: Android Developer Challenge I starts accepting submissions
• 2008 Feb 13: m5-rc15 SDK released
• 2008 Apr 14: 1788 total submissions for Challenge I
• 2008 May 12: Top 50 Applications in Challenge I announced
• 2008 Nov: Android Phone(G1 Phone by HTC/T-mobile)
• 2008 Nov: Full Source Open
• 2009 Apr: HTC Magic
• 2009 July: HTC Hero, Samsung i7500, Android Netbook, Set-top……
• 2009 Aug: Android Developer Challenge II
Industry Developer
Open
IndustrySoftware stack open-sourced under Apache 2.0 licenseSource available after first handsets shipAnyone will be able to build a system image
DeveloperDon not need permission to ship an applicationNo hidden or privileged framework APIsCan integrate, extendand replace existing components
UsersUsers have control of their experienceThey control what gets installedThey choose the defaults
Users
Integrate Extended Replace
Noteworthy Features Android uses Java:
• ... Everywhere, but only the mobile-appropriate bits!
• “Android is almost but not quite Java(tm)”
And so will you: • But nothing prevents native processes • Some native interfaces are available
Broad Java support: • java.io; • java.security;
• java.net; • java.sql;
Noteworthy Features Strong security: • Permissions-based • Applications sandboxed in separate VMs • Pervasive use of Linux process model Built-in SQL: • Property storage, retrieval • Utilized by nearly all standard components • Preferred, but not required Specialized APIs: • SurfaceFlinger • AudioFlinger
Noteworthy Features Highly-optimized Java implementation: • “Dalvik” VM implemented by Google • Custom bytecode format, processor model • Register-based, not stack-based Why? • “Didn’t want to pay Sun” (probably untrue) • Very memory- and performance-efficient • Highly tuned to limitations of small hardware Centralized object lifetime management: • Tied to component model • Tied to process model • Tied to user interface model • Tied to security model
Basic Terminology Activity : • A single visual user interface component • List of menu selections, icons, checkboxes, ... • A reusable component Service: • “Headless” activity component • Background processes Application: • Sequence of one or more Activities • Manifest tells which Activity to run first • Activities might come from other applications • Not the Linux concept of “application”!
Basic Terminology Task stack: • Sequences of application-centric Activity classes • Foreground is visible to user • BACK key returns to most-recent Activity Broadcast receiver : • Component that receives announcements • No user interface • May launch an Activity in response Content provider : • Provides application data to others • The only way to share data
Power Management Obviously important! • Can be a difficult problem to solve • Too much model exposure is bad • Too little is also bad Extends the Linux device model: • Introduces “wake locks” • See android.os.PowerManager In a nutshell: • Applications don’t control power at all • Applications hold “locks” on power states • If no locks are held, Android powers down
POWER SAVING
Picture is from Google I/O 09 talk - Coding for Life -- Battery Life, That Is
Audio and Video APIs MediaPlayer class: • Standard support for many data formats • URI invokes appropriate input method • Consistent API regardless of data source MediaRecorder class: • Support for audio recording only • Video recording is “planned” Surfaceflinger : • Centralized framebuffer management • Related to 2D h/w acceleration Audioflinger : • Centralized audio stream management You don’t work with these flingers directly!
Audio and Video APIs Example
1. MediaPlayer mp = new MediaPlayer(); 2. mp.setDataSource(PATH_TO_FILE); 3. mp.prepare(); 4. mp.start(); 5. mp.pause(); 6. mp.stop();
Android Architecture
Core LibrariesProvides the functionality of the JAVA Programming LanguageAndroid Application runs in its own process, with its own instance of the Dalvik virtual machineDalvik VM: Java based license free VM
•Register based VM, optimization for low memory requirements•Executes files in the Dalvik Executable (.dex) format•DX tool converts classes to .dex format
Details of Architecture(1/4)
Linux Version 2.6.x for core system servicesAndroid uses only “Kernel” portion in Linux
Details of Architecture(2/4)
• Libc: c standard lib.• SSL: Secure Socket Layer• SGL: 2D image engine• OpenGL|ES: 3D image engine• Media Framework: Core part of Android multi-media• SQLite: Embedded database• WebKit: Kernel of web browser• FreeType: Bitmap and Vector• Sufrace Manager: Manage difference windows for
different applications
Details of Architecture(3/4)
• No limited application• Equality of each apps.• Easy to embedded web browser• Parallel running
Details of Architecture(4/4)
• The Design goal of Android: Openness– Be as flexible as possible– How it handles access to
data: Mash up on the internet and everything else
– Rapid development(XML, Java)
• Develop Language– App: Java– Framework: Java– Libraries: C/C++– OS & Driver: C
Java
C/C++
C
The Hardware CPU: Qualcomm QSX8250 1Ghz
Mother board: Qualcomm Mobile Station Modem
(MSM) chipset , MSM7k series
RAM: 512 MB
ROM: 512 MB , partitioned as
boot/system/userdata/cache
External Storage: 4GB micro SD
Audio Processor: msm_qdsp6 onboard processor,
Firmware at /system/etc/vpimg
The Hardware Camera: Sensor_s5k3e2fx,5 MegaPixels Wifi+BlueTooth+FM: Boardcom BCM 4329,
802.11a/b/g/n, firmware at /system/etc/firmware/fw_bcm4329.bin
Touch Screen Input: msm_ts touchscreen controller, capella
Vibrator: Msm_vibrator on board vibrator Digital Compass: AK8973 More at
http://www.google.com/phone/static/en_US-nexusone_tech_specs.html
The Hardware
Application Architecture
• Dev. Language: Java• Virtual Machine:
– Dalvik VM, not JVM.– Open source
• Application: consists of one or more of the following classifications– Activities– Services– Content providers– Broadcast receivers
Dalvik Virtual Machine
APPLICATION LIBRARY
GNU libs (glibc) is too big and complicated for mobile phones, so Android implements its own special version of libc - Bionic libc:
Smaller size - 200K (glibc is more than 400K)
Strip out some complicated C++ features, the most significant one - no C++ exception!
Very special and small pthread implementation, heavily based on kernel futexes
Bionic libc does not fully support POSIX and is not compatible with glibc
which means ...?
PROCESS MANAGEMENT
What’s the difference between mobile apps cycle and desktop apps cycle?
Two key principles
Android usually do not kill an app, i.e. apps keep running even after you switch to other apps
Android kills apps when the memory usage goes too high, but it saves app state for quick restart later on
Do they make sense to mobile apps?
APPLICATION LIFE CYCLE
EXAMPLESystem
HomeHome
Home
At the “Home” screen
EXAMPLESystem
Home
Home
List
Home List
Start the “Mail” app and read the list
EXAMPLESystem
Home
Home
List Message
Home List Message
Click on one of the message and see its content
EXAMPLESystem
Home
Home
List Message
BrowserBrowser
Home List Message Browser
Click a link in the message
EXAMPLESystem
HomeHome
BrowserBrowser
Home List Message Browser
Now we have enough space to start the “Map” app
EXAMPLESystem
HomeHome
BrowserBrowser
Home List Message Browser
MapMap
Map
Start the “Map” app
EXAMPLESystem
HomeHome
BrowserBrowser
Home List Message Browser
Map
Go back to the browser
EXAMPLESystem
HomeHome
Browser
Home List Message
MailList Message
The “Mail” app is resumed and shows the previous message
EXAMPLESystem
HomeHome
Browser
Home List
MailList
Go back to the mail list
EXAMPLESystem
HomeHome
Browser
Home
Go back to the “Home” screen
DEBATE
Swapping modelVS.
Android’s life-cycle model
DISK I/O
Flash Hard Disk Drive
Random access ~0.1ms 5-10ms
File fragment impact No Greatly impacted
Total power 1/2 to 1/3 of HDD up to 15+ watts
Reliability Reliable Less reliable due to mechanical parts
Write longevity Limited number of writes Less of a problem
Capacity <= 512GB 2-3TB
Price $1.5-2 / GB $0.1-0.2 / GB
LIMITED WRITES?
Flash drives have the well-known problem of limited number of writes in the life time - 10,000~100,000 times. Solution?
What can applications do?
How about operating system?
Controllers?
Hardware?
MEMORY MANAGEMENT
Linux kernel does most of the job
Page-based memory management
Virtual address to physical address mapping
NO virtual memory
Why do we still need “virtual to physical” address mapping?
Why does Android not support virtual memory?
POWER MANAGEMENT
DALVIK VM
Why does Android let developers use Java?
DALVIK VM
A special Java virtual machine (VM) designed to run with limited system resource
Memory efficiency
Register machine vs. Stack machine (modern JVM)
fewer instructions, faster execution
why does the number of instructions matter?
Running multiple VMs more efficiently
DEX FILE
Java class files are converted into “.dex” files that Dalvik executes
Java byte-code is converted into Dalvik byte-code during this process
MEMORY EFFICIENCY
Shared constant string pool
Share clean (even some dirty) memory between processes as much as possible
“.dex” files are mapped as read-only by mmap()
Memory efficient JIT implementation
JIT itself is about 100K
Code cache and supporting data structure takes another 100K for each application
SHARED STRING POOLpublic interface Zapper { public String zap(String s, Object o);}
public class Blort implements Zapper { public String zap(String s, Object o) { .... }}
public class ZapUser { public void useZap(Zapper z) { z.zap(...); }}
SHARED STRING POOL
SHARED MEMORY
PROGRAMMING MODEL
Each application is running in its own process
An application can have one or more components:
activities, services, broadcast receivers and content providers
A task (an “application” from user’s point of view) consists of several activities from one or multiple applications
An application keeps running until the system kills it because of memory shortage
Android SDK
• android.jar Java archive file containing all of the Android SDK classes necessary to build your application.
• documention.html and docs directory The SDK documentation is provided locally and on the Web. It's largely in the form of JavaDocs, making it easy to navigate the many packages in the SDK. The documentation also includes a high-level Development Guide and links to the broader Android community.
• Samples directory The samples subdirectory contains full source code for a variety of applications, including Api Demo, which exercises many APIs. The sample application is a great place to explore when starting Android application development.
• Tools directory Contains all of the command-line tools to build Android applications. The most commonly employed and useful tool is the adb utility (Android Debug Bridge).
• Usb_driver Directory containing the necessary drivers to connect the development environment to an Android-enabled device, such as the G1 or the Android Dev 1 unlocked development phone. These files are only required for developers using the Windows platform.
Tools
• Emulator– Android applications may be
run on a real device or on the Android Emulator, which ships with the Android SDK.
• ADB (Android Debug Bridge)– The ADB utility lets you
connect to the phone itself and issue rudimentary shell commands, such as copying files to and from the device.
Development Environments
• Eclipse 3.2.3.3• Eclipse JDT Plugin• JDK 5 or 6• ADT:Android Development
Tools plug-in
Example: HelloWorldRunning on Emulator
Debugging your first Android App
Configure USB connection, if you are working with devices
Test adb and connect to device N:\android-sdk-windows\tools>adb devices * daemon not running. starting it now * * daemon started successfully * List of devices attached HT9CNP804091 device emulator-5556 device Launch a shell via adb: • The shell is actually on the target! N:\android-sdk-windows\tools>adb shell $
Debugging your first Android App
Summary
• Benefits– Open Platform/License Free– Robust OS Kernel, Innovative Library Packages– Ease App. Development– Rapid Improvement
• Challenges– Performance Consideration– Hard to Integrate for Vendors– Too Much Google Dependent
• Key Factor: Market Response