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
Reference books Tons of books on Android application development... Suggested: Learning Android, Marko Gargenta O'REILLY But we'll not follow it completely...
Alberto Panizzo
Course organisation 3 credits, 30 hours, frontal lessons + lab When: ○ Tuesday: 14:30-16:30 ○ Wednesday: 9:00-12:00 ● Where: ○ All in room 27A ● Web: http://retis.sssup.it/~panizzo/android ● Mail:
[email protected] ● Subscribe to the mailinglist! ● ●
Alberto Panizzo
Prerequisites • Object Oriented Programming • Autonomy with Java • Autonomy with Linux • ~ Personal laptop
Alberto Panizzo
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, etc.) • I want you to be trained, not educated on Android Details will be available in the website
Alberto Panizzo
Q&A? Any Questions?
Alberto Panizzo
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
lzo_do_compress(Android History) 2005: Google buys Andy Rubin's Android Inc Was working on location-aware mobile devices. [1] 2007: Announcement of the Open Handset Alliance Aimed to:"offer consumers a richer, less expensive, and better mobile experience" [2] 2008: Source code made available, Android SDK 1.0 is released, HTC G1 is sold 2009: Evolution of the platform (Cupcake, Donut, Eclair) and more than 20 devices running Android. 2011: 100 M of Activations world wide (Honeycomb, Icecream Sandwich)
Alberto Panizzo
Google's Motivations • Google's business is Advertisement • Before Android/iPhone there was WAP..... • 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. • 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
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
What is Android? It is a modern, comprehensive, portable and open mobile stack. • 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 .. • Highly connected: o 3G/3G/4G networks / Wifi / Bluetooth / NFC
Alberto Panizzo
Device ecosystem
Alberto Panizzo
Devices specs Minimum: • RAM: 128MB - 1GB (Run hardly on 64MB) • CPU: 500MHz - 1.2GHz • Screen / Touch panel (Can manage mouse input..) Then: • Audio I/O • Hardware accelerations (2D 3D Graphics/AV Codecs) • Connectivity (Wifi/Bluetooth/Modem) • Sensors (Camera/Proximity/Gyroscope/Accelerometer/GPS) • ... Alberto Panizzo
Open source licence • Most of the source code is released under Apache 2.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.
Alberto Panizzo
Android on the web Outstanding resources on the web: • http://developer.android.com All you need to develop applications for Android • http://source.android.com/ All needed to start develop Android
Alberto Panizzo
A note on versions
[4] http://developer.android.com/resources/dashboard/platform-versions.html Alberto Panizzo
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
Based on Linux Google says: "It offers consolidated model for managing Process / Memory / Security" But also: Open Source, Ported on all common architectures, Portable, Stable, Support all kind of peripheral, Big and spread community of developers, Lot of companies involved in its development ... More?
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 ...) • ..
Alberto Panizzo
Opportunistic Power Management "Rather than trying to put the various system components into a low-power state, opportunistic suspend works by simply suspending the entire device whenever it is determined that nothing interesting is going on." [5] Privileged userspace or kernel drivers can acquire wakelocks objects which controls the suspending/resume process.
Alberto Panizzo
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
Low memory management echo "0,1,2,4,7,15" > /sys/module/lowmemorykiller/parameters/adj echo "2048,3072,4096,6144,7168,8192">/sys/module/lowmemorykiller/parameters/minfree
Alberto Panizzo
Num Free Pages
=
8192
15
7168
7
6144
4
4096
2
3072
1
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. Process memory allocator PMEM: • Allocate large physical contiguous memory regions • Used mostly for driving 2D/3D hardware engines
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. • Remote interfaces are defined with AIDL [6]
Alberto Panizzo
New IPC model
Alberto Panizzo
Architecture
Alberto Panizzo
Android abstraction layer Set of stub drivers written to detach the Android framework from Linux driver interface. • Not all Linux drivers of the same class implements exactly the same behavior • Not all hardware devices of the same type (Camera, Audio codec..) 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
Userspace libraries Core libraries: • SurfaceFlinger: video compositor manager • AudioFlinger: audio compositor manager • ... External libraries: • Webkit: web render engine used by Safari, Chrome.. • SQLite: light, full-featured SQL engine • Media libraries: Packet Video OpenCore / Stagefright • OpenGL ESx: 2D/3D graphics library • ALSALib: Open audio library • ... Alberto Panizzo
Runtime enablement: Dalvik • The whole application environment is written in Java and runs on top of the Dalvik Vitrual Machine. • 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
Architecture
Alberto Panizzo
Runtime enablement: Dalvik The dalvik compiler takes the java bytecode (.class) and translates it to the .dex format. Advantages: • Java bytecode is far more stable than Java language • Developers use trusted tools (jdk) to compile their programs, no double toolkits on workstations
Alberto Panizzo
Architecture
Alberto Panizzo
Application Framework Set of Managers/Services which expose all the functionalities available to the applications. From core Managers as: ActivityManager, WindowManager, PackageManager, NotificationManager.. implementing the runtime ecosystem. To system Services as: TelephonyService, LocationService, SensorService, WifiService ... implementing functionalities available to the applications. Written primarily in Java, with the same rules as a normal application Alberto Panizzo
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
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 ...??) 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? • Application engines can be written in C/C++ (native code) • Use the JNI invocation method • Need to ship binaries for multiple architectures (ARMv5, ARMv7) • Used when high performance is needed o Physics simulations o Efficient/Fast load/analysis on large data o .. • 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
Interesting graph
[9] Alberto Panizzo
Links of interest [1] http://www.businessweek.com/technology/content/aug2005/tc20050817_0949_tc024.htm [2] http://www.openhandsetalliance.com/ [3] http://www.itbusinessedge.com/cm/blogs/bentley/%20more-on-g1-and-googles-goals-withandroid/?cs=15048 [4] http://developer.android.com/resources/dashboard/platform-versions.html [5] http://lwn.net/Articles/460644/ [6] http://elinux.org/Android_Binder [7]https://sites.google.com/site/io/dalvik-vm-internals [8] http://www.google.com/events/io/2009/sessions/HowToCodeThee.html [9] http://www.businessinsider.com/the-one-chart-you-need-to-see-to-understand-mobile2011-11?utm_source= %23frankguillen&utm_medium=twitter&utm_campaign=FrankGuillen+Buzz
Alberto Panizzo