You are on page 1of 45

Android Framework

How to use and extend it

Android Framework
Instructor: Alberto Panizzo, CTO at Amarula Solutions BV A company focused on OSS within Embedded solutions.

a.y. 2011/2012

Alberto Panizzo

Goal
Give a comprehensive introduction to Android, oriented towards application development. Not an Android porting course No rooting techniques Enable you to develop proficiently applications in Android: → → You'll know what to look for and where, and you'll learn the basic best practices Your code won't cause a headache to the next developer who takes a look at it

Alberto Panizzo

Main topics
• • • • • • Application's building blocks Multitasking and shared resources Data management Security Best practices X-ray view of the Camera stack

Alberto Panizzo

Alberto Panizzo ..Reference books Tons of books on Android application development. Marko Gargenta O'REILLY But we'll not follow it completely. Suggested:     Learning Android....

sssup.Course organisation 3 credits. frontal lessons + lab When: ○ Tuesday: 14:30-16:30 ○ Wednesday: 9:00-12:00 ● Where: ○ All in room 27A ● Web: http://retis. 30 hours.sssup.it ● Subscribe to the mailinglist! ● ● Alberto Panizzo .it/~panizzo/android ● Mail: panizzo@retis.

Prerequisites • Object Oriented Programming • Autonomy with Java • Autonomy with Linux • ~ Personal laptop Alberto Panizzo .

not educated on Android Details will be available in the website Alberto Panizzo . etc.Final test • Present a Project: o Develop an Application o Analysis of a Framework Subsystem o Analysis and proposal of a solution to open problems (?) (Boot time reduction.) • I want you to be trained.

Q&A? Any Questions? Alberto Panizzo .

What is Android and what devices it address Android Stack and key features Other ways to code in Android Alberto Panizzo .Lecture 1 • • • • A glance at the past.

HTC G1 is sold 2009: Evolution of the platform (Cupcake. 2011: 100 M of Activations world wide (Honeycomb. less expensive.lzo_do_compress(Android History) 2005: Google buys Andy Rubin's Android Inc Was working on location-aware mobile devices.0 is released. Donut. [1] 2007: Announcement of the Open Handset Alliance Aimed to:"offer consumers a richer. Android SDK 1. Eclair) and more than 20 devices running Android. and better mobile experience" [2] 2008: Source code made available. Icecream Sandwich) Alberto Panizzo .

• Whatever was Android/iPhone/Blackberry Google was pointing at standard web browsing all over the world [3] • Now Google makes money through Google API fees + Android Market Alberto Panizzo . • If every cellphone owners can go on the Internet and use google....com in a standard way.. Google income increases exponentially without changing anything in its product wallet...Google's Motivations • Google's business is Advertisement • Before Android/iPhone there was WAP.

Lecture 1 • • • • A glance at the past. What is Android and what devices it address Android Stack and key features Other ways to code in Android Alberto Panizzo .

comprehensive. • Fit modern mobile device resources in terms of: o Memory / CPU power / hardware helpers • Support all the modern mobile devices peripherals: o Camera / Sensors / GPS / Mass storage .What is Android? It is a modern.. portable and open mobile stack. • Highly connected: o 3G/3G/4G networks / Wifi / Bluetooth / NFC Alberto Panizzo .

Device ecosystem Alberto Panizzo .

Alberto Panizzo .1.1GB (Run hardly on 64MB) • CPU: 500MHz .) Then: • Audio I/O • Hardware accelerations (2D 3D Graphics/AV Codecs) • Connectivity (Wifi/Bluetooth/Modem) • Sensors (Camera/Proximity/Gyroscope/Accelerometer/GPS) • ..Devices specs Minimum: • RAM: 128MB ...2GHz • Screen / Touch panel (Can manage mouse input.

Alberto Panizzo .0 Open Source / Not "persistent" / Not "propagative" o Derivative works can be re-licensed o Can use software modules with a different licence • Linux kernel is GPLv2 Open Source / Persistent / Propagative • Google APIs are closed source • Manufacturer private stub drivers are typically closed source.Open source licence • Most of the source code is released under Apache 2.

Android on the web Outstanding resources on the web: • http://developer.android.android.com All you need to develop applications for Android • http://source.com/ All needed to start develop Android Alberto Panizzo .

com/resources/dashboard/platform-versions.android.html Alberto Panizzo .A note on versions [4] http://developer.

Lecture 1 • • • • A glance at the past. What is Android and what devices it address Android Stack and key features Other ways to code in Android Alberto Panizzo .

Architecture Alberto Panizzo .

Lot of companies involved in its development .Based on Linux Google says: "It offers consolidated model for managing Process / Memory / Security" But also: Open Source. Portable. Ported on all common architectures. More? Alberto Panizzo .. Big and spread community of developers.. Support all kind of peripheral. Stable.

Alberto Panizzo .Based on Linux How to fit more the mobile devices lack of resources? • New Power management layer • New Low Memory Killer • New Shared memory manager (ASHMem) • New Physical contiguous memory allocator (pmem) New behaviors: • New IPC mechanism: Binder • Increased the tethering capability • New usb device drivers (adb / mass_storage .) • ....

Opportunistic Power Management "Rather than trying to put the various system components into a  low-power state. Alberto Panizzo ." [5] Privileged userspace or kernel drivers can acquire wakelocks objects which controls the suspending/resume process. opportunistic suspend works by simply  suspending the entire device whenever it is determined that  nothing interesting is going on.

Low memory management • Based on the fact that Mobile devices have different processes set than normal desktop workstations. • Android caches application's processes as much as possible to achieve fast user interaction • Uses the standard oom_adj process property to prioritize the pool of running processes and choose which to kill to free system memory • Enables user-space policies to control which process should be killed on low memory Alberto Panizzo .

3072.15" > /sys/module/lowmemorykiller/parameters/adj echo "2048.7168.8192">/sys/module/lowmemorykiller/parameters/minfree Num Free Pages < 8192 7168 6144 4096 3072 Class to kill >= 15 7 4 2 1 Alberto Panizzo .1.Low memory management echo "0.6144.2.4.7.4096.

Process memory allocator PMEM: • Allocate large physical contiguous memory regions • Used mostly for driving 2D/3D hardware engines Alberto Panizzo .Memory management Android Shared Memory ASHMem: • Allocate shared memory between processes as SHM • Maintain a user ref count • If all users free the shared object. the shared memory will be automatically released.

• Remote interfaces are defined with AIDL [6] Alberto Panizzo .New IPC model Android Binder derived from the PalmSource OpenBinder • Kernel driver • Implements inside the machine a distributed model of Remote Method Invocation.

New IPC model Alberto Panizzo .

Architecture Alberto Panizzo .

Audio codec. • Not all Linux drivers of the same class implements exactly the same behavior • Not all hardware devices of the same type (Camera.Android abstraction layer Set of stub drivers written to detach the Android framework from Linux driver interface.) are supported through the same driver class. • Android HAL define an interface for every type of supported hardware device. which the driver stub must implement Alberto Panizzo ..

Architecture Alberto Panizzo .

Private libc Bionic is a custom implementation of C libraries focusing on: size and speed • Does not support C++ exception model • Support only a subset of pthread API • Implements Android specific functionalities: o log API o give programmatic access to system properties Released under BSD license: not persistent and not propagative Alberto Panizzo .

Architecture Alberto Panizzo .

.. • SQLite: light. Alberto Panizzo . full-featured SQL engine • Media libraries: Packet Video OpenCore / Stagefright • OpenGL ESx: 2D/3D graphics library • ALSALib: Open audio library • .. Chrome..Userspace libraries Core libraries: • SurfaceFlinger: video compositor manager • AudioFlinger: audio compositor manager • . External libraries: • Webkit: web render engine used by Safari..

• Dalvik is not a standard Java Virtual machine but runs its own executable: dex. • Designed to be small and FAST o Hard optimization at compile time o Register based arithmetic (instead of stack based) • Every applications starts as a fork from another dalvik instance: o Share the same libraries memory space o Enable the uid based Android security [7] Alberto Panizzo .Runtime enablement: Dalvik • The whole application environment is written in Java and runs on top of the Dalvik Vitrual Machine.

Architecture Alberto Panizzo .

class) and translates it to the .dex format. no double toolkits on workstations Alberto Panizzo . Advantages: • Java bytecode is far more stable than Java language • Developers use trusted tools (jdk) to compile their programs.Runtime enablement: Dalvik The dalvik compiler takes the java bytecode (.

Architecture Alberto Panizzo .

From core Managers as: ActivityManager. with the same rules as a normal  application Alberto Panizzo . implementing the runtime ecosystem. LocationService. implementing functionalities available to the applications. WindowManager. Written primarily in Java.. To system Services as: TelephonyService.. NotificationManager.Application Framework Set of Managers/Services which expose all  the functionalities available to the applications. WifiService . PackageManager.. SensorService.

What is Android and what devices it address Android Stack and key features Other ways to code in Android Alberto Panizzo .Lecture 1 • • • • A glance at the past.

??) o Ajax o Flash (where supported) • Custom devices can have a webserver running inside the device with "applications" provided by it Alberto Panizzo .Other way to code in Android? • All software modules which need to deal with UI must have a frontend in Dalvik • But you can use the browser as your frontend! o HTML (1 / 2 / 3 / 4 / 5 ...

. • Used when you don't want GC breathing on your back o Audio/video stream management • System share only libc (bionic) and libm [8] Alberto Panizzo . ARMv7) • Used when high performance is needed o Physics simulations o Efficient/Fast load/analysis on large data o .Other way to code in Android? • Application engines can be written in C/C++ (native code) • Use the JNI invocation method • Need to ship binaries for multiple architectures (ARMv5.

Interesting graph [9] Alberto Panizzo .

android.html [5] http://lwn.businessweek.com/cm/blogs/bentley/%20more-on-g1-and-googles-goals-withandroid/?cs=15048 [4] http://developer.itbusinessedge.com/technology/content/aug2005/tc20050817_0949_tc024.com/ [3] http://www.openhandsetalliance.org/Android_Binder [7]https://sites.businessinsider.google.htm [2] http://www.Links of interest [1] http://www.com/resources/dashboard/platform-versions.com/events/io/2009/sessions/HowToCodeThee.google.html [9] http://www.com/the-one-chart-you-need-to-see-to-understand-mobile2011-11?utm_source= %23frankguillen&utm_medium=twitter&utm_campaign=FrankGuillen+Buzz Alberto Panizzo .net/Articles/460644/ [6] http://elinux.com/site/io/dalvik-vm-internals [8] http://www.