What is Android? - Micrel Home Page
Transcription
What is Android? - Micrel Home Page
Alessio Bonfietti Giuseppe Tagliavini What is Android? 1. A complete software stack for mobile devices – Joined project of the Open Handset Alliance (OHA) • Open-sourced under Apache 2.0 licence 2. Android Platform – Operating system – Middleware – Applications 3. Android development environment – SDK – NDK 1 Handset Manufacturers Software Mobile Operators Semiconductor Commercialization Android Diffusion 2 Android Diffusion Android Diffusion http://developer.android.com/resources/dashboard/platform-versions.html 3 Why Android? • Multi Platform • Open-source Multi Platform System Application Layer Services Layer Native Libraries Layer VM Layer Kernel Layer Display Driver Audio Drivers Wifi Driver … HARDWARE 4 Multi Platform System Application Layer Application Layer Application Layer Services Layer Services Layer Services Layer Native Libraries Layer VM Layer Kernel Layer Native Libraries Layer VM Layer Kernel Layer Native Libraries Layer VM Layer Kernel Layer Display Driver Audio Drivers Display Driver Audio Drivers Display Driver Audio Drivers Wifi Driver … Wifi Driver … Wifi Driver … HARDWARE HARDWARE HARDWARE Multi Platform System Application Layer Application Layer Application Layer Services Layer Services Layer Services Layer Native Libraries Layer VM Layer Kernel Layer Native Libraries Layer VM Layer Kernel Layer Native Libraries Layer VM Layer Kernel Layer Display Driver Audio Drivers Display Driver Audio Drivers Display Driver Audio Drivers Wifi Driver … Wifi Driver … Wifi Driver … HARDWARE HARDWARE HARDWARE 5 Android Anatomy • • • • Linux Kernel Native Libraries Android Runtime Application Framework Linux Kernel Android is built on the Linux kernel No native windowing system No glibc support Does not include the full set of standard Linux utilities Android relies on Linux version 2.6 for core system services such as security, memory management, process management, network stack, and driver model. The kernel also acts as an abstraction layer between the hardware and the rest of the software stack. Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management 6 Linux Kernel Why Linux kernel: • • • • • Great memory and process management Permission-based security model Proven driver model Support for shared library Open source! Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management Linux Kernel Kernel Enhancements: • • • • • • • Alarm Ashmem Low Memory Killer Kernel Debugger Logger Binder (IPC) Power Management: wakelock Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management 7 Linux Kernel Kernel Enhancements: Binder (-) Applications and Services may run in separate processes bus must communicate and share data (-) IPC can introduce significant processing overhead and security holes Solution: Driver to facilitate inter-process communication (IPC) • • • A pool of threads is associated to each application to process incoming IPC The driver performs mapping of objects between two processes ―Binder‖ uses an object reference as an address in a process’s memory space (+) High performance through shared memory (+) Synchronous calls between processes (+) pre-process thread pool for processing requests Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management Linux Kernel Kernel Enhancements: Binder in Action Process A Process B App A Service B Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management 8 Linux Kernel Kernel Enhancements: Binder in Action Process A Process B Context App A Service B Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management Linux Kernel Kernel Enhancements: Binder in Action Process A Process B Context App A Service B getService() Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management 9 Linux Kernel Kernel Enhancements: Binder in Action Process A Process B Context App A Service B getService() service Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management Linux Kernel Kernel Enhancements: Binder in Action Process A Process B Context App A Service B getService() service call func(par) Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management 10 Linux Kernel Kernel Enhancements: Binder in Action Process A Process B Binder (IPC) Driver Context App A Service B getService() service Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management Linux Kernel Kernel Enhancements: Binder in Action Process A Process B Binder (IPC) Driver Context App A Service B getService() service call func(par) Marshal Proxy object Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management 11 Linux Kernel Kernel Enhancements: Binder in Action Process A Process B Binder (IPC) Driver Context App A Service B getService() service Marshal Proxy object call func(par) Reply to IPC threads Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management Linux Kernel Kernel Enhancements: Binder in Action Process A Process B Binder (IPC) Driver Context App A Service B getService() service call func(par) Marshal Proxy object Reply to IPC threads call return Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management 12 Linux Kernel Kernel Enhancements: Power Management (-) Mobile devices run on battery power (-) Battery have limited capacity Solution: Based on the standard Linux Power Management (PM), Android has its own component. (+) More aggressive power management policy (+) Based on the wakelocks policy • • Application uses user space library to inform the framework about its constraints Constraints are implemented using lock mechanism Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management Linux Kernel Kernel Enhancements: Power Management in Action Process A App A PM Driver Power Management Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management 13 Linux Kernel Kernel Enhancements: Power Management in Action Process A PM Driver Power Management App A new wake lock PARTIAL Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management Linux Kernel Kernel Enhancements: Power Management in Action Process A PM Driver Power Management App A new wake lock Create wake lock PARTIAL Turn Off LCD Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management 14 Linux Kernel Kernel Enhancements: Power Management in Action Process A PM Driver Power Management App A new wake lock Create wake lock PARTIAL Turn Off LCD release release Turn Off CPU Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management Native Libraries • System library (Bionic) - a BSD-derived implementation of the standard C system library (libc), tuned for embedded Linux-based devices • Media Libraries - based on PacketVideo's OpenCORE; the libraries support playback and recording of many popular audio and video formats, as well as static image files, including MPEG4, H.264, MP3, AAC, AMR, JPG, and PNG • Surface Manager - manages access to the display subsystem and seamlessly composites 2D and 3D graphic layers from multiple applications • Audio Manager – processes multiple audio streams into PCM audio paths • LibWebCore - a modern web browser engine which powers both the Android browser and an embeddable web view • SGL - the underlying 2D graphics engine • 3D libraries - an implementation based on OpenGL ES APIs; the libraries use either hardware 3D acceleration (where available) or the included, highly optimized 3D software rasterizer • FreeType - bitmap and vector font rendering • SQLite - a powerful and lightweight relational database engine available to all applications Libraries Audio Manager FreeType Media Framework Surface Manager SQLite SGL SSL libc OpenGL|ES WebKit 15 Native Libraries • System library (Bionic) - a BSD-derived implementation of the standard C system library (libc), tuned for embedded Linux-based devices • Media Libraries - based on PacketVideo's OpenCORE; the libraries support playback and recording of many popular audio and video formats, as well as static image files, including MPEG4, H.264, MP3, AAC, AMR, JPG, and PNG • Surface Manager - manages access to the display subsystem and seamlessly composites 2D and 3D graphic layers from multiple applications • Audio Manager – processes multiple audio streams into PCM audio paths • LibWebCore - a modern web browser engine which powers both the Android browser and an embeddable web view • SGL - the underlying 2D graphics engine • 3D libraries - an implementation based on OpenGL ES APIs; the libraries use either hardware 3D acceleration (where available) or the included, highly optimized 3D software rasterizer • FreeType - bitmap and vector font rendering • SQLite - a powerful and lightweight relational database engine available to all applications System Multimedia Rendering Storage Libraries Audio Manager FreeType Media Framework Surface Manager SQLite SGL SSL libc OpenGL|ES WebKit Native Libraries: system System library (Bionic) - a BSD-derived implementation of the standard C system library (libc) (+) Optimized for embedded use (+) Size: will load in each process, so it needs to be small (+) Fast: limited CPU power, so it need to be fast • Custom pthread implementation Built-in support for important Android-specific services: • system properties • log capabilities getprop(“my.system.property”, LOG(“Logging a message buff, with default); priority „Info‟”); Libraries Audio Manager FreeType Media Framework Surface Manager SQLite SGL SSL libc OpenGL|ES WebKit 16 Native Libraries Audio Manager Processes multiple audio streams into PCM audio out paths. • Handle several types of devices (headphone, earpiece, …) • Redirects audio stream to the specified output App Tone Audio App Media Player Earpeace Speaker Audio Manager Bluetooth Game Audiio Libraries Audio Manager FreeType Media Framework Surface Manager SQLite SGL SSL libc OpenGL|ES WebKit Native Libraries Surface Manager Surface Manager - manages access to the display subsystem and seamlessly composites 2D and 3D graphic layers from multiple applications •Provides system-wide surface ―composer‖, handling all surface rendering to frame buffer device •Can combine 2D and 3D surfaces and surfaces from multiple applications •Surfaces passed as buffers via Binder IPC calls •Can use OpenGL ES and 2D hardware accelerator for its compositions •Double-buffering using page-flip Surface App Surface Surface Manager App Frame buffer Surface Libraries Audio Manager FreeType Media Framework Surface Manager SQLite SGL SSL libc OpenGL|ES WebKit 17 Native Libraries Hardware Abstraction Libraries Defines the interfaces that Android requires hardware ―drivers‖ to implement. • • • • Set of standardized APIs the developer will have to implement User space C/C++ library layer Separates the Android platform logic from the hardware interface Available for all the components a manufacturer can integrate on its Android platform Graphics Camera Audio GPS … Hardware Abstraction Libraries Android Stack Native Libraries Audio Manager FreeType Media Framework Surface Manager SQLite SGL SSL libc OpenGL|ES WebKit Graphics Audio Camera GPS … Hardware Abstraction Libraries Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management 18 Android Runtime Every Android application runs in its own process, with its own instance of the Dalvik virtual machine. (+) Designed to allow multiple VM instances to run at one • The Dalvik VM executes files in the Dalvik Executable (.dex) format which is optimized for minimal memory footprint. (+) An interpreter-only virtual machine (no JIT), register based. • Dalvik runs classes compiled by a Java language compiler that have been transformed into the .dex format by the included "dx" tool. (+) Optimized for low memory requirements • The Dalvik VM relies on the Linux kernel for underlying functionality such as threading and low-level memory management. Dalvik Virual Machine Core Libraries Android runtime Android Runtime Android includes a set of core libraries that provides most of the functionality available in the core libraries of the Java programming language. • Data structures • Utilities • File access • Network Access • Graphics • … Dalvik Virual Machine Core Libraries Android runtime 19 Android Stack Native Libraries Android runtime Surface Manager Media Framework SQLite OpenGL|ES FreeType WebKit SGL Audio Manager libc Core Libraries Graphics Camera Audio Dalvik Virual Machine … GPS Hardware Abstraction Libraries Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management Application Framework Underlying all applications is a set of services and systems. Developers have full access to the same framework APIs used by the core applications. (+) The application architecture is designed to simplify the reuse of components: • any application can publish its capabilities and any other application may then make use of those capabilities (subject to security constraints enforced by the framework). This same mechanism allows components to be replaced by the user. Services: • • Core Platform Services Hardware Services Application Framework Acivity Manager Window Manager Content Providers View System Notification Manager Service Manager Telephony Manager Resource Manager Location Manager … 20 Application Framework Core Platform Services • Activity Manager • Service Manager • Window Manager • Resource Manager • Content Providers • View System Application Application Framework Framework Acivity Manager Package Service Manager Manager Acivity ManagerWindow Manager Window ManagerContent Providers Content Providers Telephony Telephony Manager Manager Resource Resource Manager Manager View System View System Location Location Manager Manager Notification Manager Notification … Manager Application Framework Core Platform Services • Activity Manager • Handles Application lifecycle OnCreate() The Activity is created Start Up Initialization of the resources (i.e. create a thread, connection to database) OnStart() Shutdown OnResume() OnPause() OnStop() OnDestroy() The system put the application on top of the execution stack The user cannot interact with. The Activity is on focus and visible to the user The focus is lost Save data (persistence) The Activity is removed from the execution stack The Activity is killed Resources can be freed 21 Application Framework Core Platform Services A Service is an application component that can perform long-running operations in the background and does not provide a user interface. • • Another application component can start a service and it will continue to run in the background even if the user switches to another application. A component can bind to a service to interact with it and even perform interprocess communication (IPC). • • For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background. When a Service is using IPC, an Android Interface Definition Language (AIDL) is used to generate code to allow communication between two processes through IPC (Bound Service). Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). Application Framework Core Platform Services : Content Providers Content providers provide a level of abstraction for any data stored on the device that is accessible by multiple applications. The Android development model encourages you to make your own data available to other applications, as well as your own ‚ building a content provider lets you do that, while maintaining complete control over how your data gets accessed. • The only way to share data between Android applications. • Any form of data storage can be used like SQLite database, files, or memory hash map. • A Content Providers exposes a unique URI used to query, add, update and delete Data Application Application Framework Framework Acivity Manager Package Service Manager Manager Acivity ManagerWindow Manager Window ManagerContent Providers Content Providers Telephony Telephony Manager Manager Resource Resource Manager Manager View System Location Location Manager Manager View System Notification Manager Notification … Manager 22 Application Framework Hardware Services Provide access to lower-level hardware APIs • Telephony Service • Location Service • Bluetooth Service • Wifi Service • Sensor Service • USB Service Application Application Framework Framework Acivity Manager Acivity ManagerWindow Manager Window ManagerContent Providers Content Providers Telephony Telephony Manager Manager Package Service Manager Manager Resource Resource Manager Manager View System View System Notification Manager Notification … Manager Location Location Manager Manager Android Stack Application Framework Acivity Manager Window Manager Content Providers View System Notification Manager Service Manager Telephony Manager Resource Manager Location Manager … Native Libraries Android runtime Surface Manager Media Framework SQLite OpenGL|ES FreeType WebKit SGL Audio Manager libc Core Libraries Graphics Audio Camera Dalvik Virual Machine GPS … Hardware Abstraction Libraries Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management 23 Android Stack Applications Home Dialer Contacts Alarm SMS Email Browser … Medial Player Clock Application Framework Acivity Manager Window Manager Content Providers View System Notification Manager Service Manager Telephony Manager Resource Manager Location Manager … Native Libraries Android runtime Surface Manager Media Framework SQLite OpenGL|ES FreeType WebKit SGL Audio Manager libc Core Libraries Graphics Audio Camera Dalvik Virual Machine GPS … Hardware Abstraction Libraries Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management Platform Initialitazion Kernel Init Similar to most Linux-based systems at startup, the bootloader loads the Linux kernel and starts the init process. 24 Platform Initialitazion Kernel Init Init starts Linux daemons: usbd adbd debuggerd rild • USB Daemon (usbd) to manage USB connections • Android Debug Bridge (adbd) to manage ADB connections • Debugger Daemon (debuggerd) to manage debug processes requests (dump memory, etc.) … • Radio Interface Layer Daemon (rild) to manage communication with the radio • … Platform Initialitazion Kernel Init usbd adbd rild Daemons Zygote Init process starts the zygote process : • • A nascent process which initializes a Dalvik VM instance Loads classes and listens on socket for requests to spawn • VMs Forks on request to create VM instances for managed • processes Copy-on-write to maximize re-use and minimize footprint 25 Platform Initialitazion Kernel Init Init starts runtime process: usbd adbd rild Daemons Zygote Runtime • Initializes Service Manager – the context manager for Binder that handles service registration and lookup Service Manager • Registers Service Manager as default context manager for Binder services Platform Initialitazion Kernel Init Init starts runtime process: usbd adbd rild Daemons Zygote Runtime • Initializes Service Manager – the context manager for Binder that handles service registration and lookup Service Manager • Registers Service Manager as default context manager for Binder services Runtime process sends request for Zygote to start System Service 26 Platform Initialitazion Kernel Init usbd adbd rild Daemons Zygote Runtime Service Manager Dalvik Zygote forks a new VM instance for the System Service process and starts the service Virual Machine System Server Runtime process sends request for Zygote to start System Service Platform Initialitazion Kernel Init usbd adbd rild Daemons Zygote Runtime Service Manager Dalvik Virual Machine System Service starts the native system servers, including System Server Surface Manager Audio Manager … • Surface Flinger • Audio Flinger 27 Platform Initialitazion Kernel Init usbd adbd rild Daemons Zygote Runtime Service Manager Native system servers register with Service Manager as IPC service targets Dalvik Virual Machine System Service starts the native system servers, including System Server Audio Manager Surface Manager • Surface Flinger … • Audio Flinger Platform Initialitazion Kernel Init usbd adbd rild Daemons Zygote Runtime Service Manager Dalvik Virual Machine System Service starts the Android managed services System Server Surface Manager Manager Surface Surface Manager Acivity Manager Window Manager Content Providers Service Manager Resource Manager Location Manager View System Telephony Manager Notification Manager … 28 Platform Initialitazion Kernel Init usbd adbd rild Daemons Zygote Runtime Service Manager System Service starts the Android managed services Dalvik Virual Machine Service Manager System Server Android managed Services register with Service Manager Surface Manager Manager Surface Surface Manager Acivity Manager Window Manager Content Providers Service Manager Resource Manager Location Manager View System Notification Manager Telephony Manager … Platform Initialitazion Kernel Init usbd adbd rild Daemons Zygote Runtime Service Manager Dalvik Virual Machine System Server Surface Manager Manager Surface Surface Manager Window Manager Resource Manager Acivity Manager 29 Platform Initialitazion After system server loads all services, the system is ready… Window Manager Resource Manager Acivity Manager Dalvik Virual Machine Init usbd adbd rild Daemons Runtime Zygote Surface Manager Manager Surface Surface Manager Platform Initialitazion After system server loads all services, the system is ready… Home Dalvik Window Manager Resource Manager Acivity Manager Dalvik Virual Machine Init usbd adbd rild Daemons Runtime Zygote Surface Manager Manager Surface Surface Manager 30 Platform Initialitazion After system server loads all services, the system is ready… Home Dalvik Window Manager Resource Manager Acivity Manager Dalvik Virual Machine Init libc usbd adbd rild Daemons libc Runtime libc Zygote libc Surface Manager Manager Surface Surface Manager libc libc Platform Initialitazion Each subsequent application is launched in itʼs own process Home App Dalvik Dalvik libc libc Window Manager Resource Manager Acivity Manager Dalvik Virual Machine Init libc usbd adbd rild Daemons libc Runtime libc Zygote libc Surface Manager Manager Surface Surface Manager libc 31 Application Stack Media Recorder Function Call Application Native Libraries Audio Manager Media Framework SGL Surface Manager libc OpenGL|ES Graphics GPS Camera Audio … Hardware Abstraction Libraries Linux Kernel Camera Driver Display Driver Audio Drivers Binder (IPC) Driver Application Stack Media Recorder Function Call Application setCamera iSurface selectAudio Native Libraries Audio Manager Media Framework SGL Surface Manager libc OpenGL|ES Graphics GPS Camera Audio … Hardware Abstraction Libraries Linux Kernel Camera Driver Display Driver Audio Drivers Binder (IPC) Driver 32 Application Stack Media Recorder Function Call Application setCamera iSurface selectAudio Native Libraries Audio Manager Media Framework SGL Surface Manager libc OpenGL|ES Graphics GPS Camera Audio … Hardware Abstraction Libraries Linux Kernel Camera Driver Display Driver Audio Drivers Binder (IPC) Driver Application Stack Media Recorder Function Call Application setCamera iSurface selectAudio Native Libraries Audio Manager Media Framework SGL Surface Manager libc OpenGL|ES Graphics GPS Camera Audio … Hardware Abstraction Libraries Linux Kernel Camera Driver Display Driver Audio Drivers Binder (IPC) Driver 33 Application Stack Media Recorder Function Call and Playback Application setCamera iSurface selectAudio Native Libraries SurfaceManager Media Framework AudioManager SGL libc OpenGL|ES Graphics GPS Camera Audio … Hardware Abstraction Libraries Linux Kernel Camera Driver Display Driver Audio Drivers Binder (IPC) Driver Alessio Bonfietti Giuseppe Tagliavini 34 Android layer cake Applications App0 Home Alarm App1 App2 Application Framework Acivity Manager Window Manager Content Providers Service Manager Media Manager … Native Libraries Android runtime Surface Manager Media Framework OpenGL|ES Core Libraries Audio Manager SQLlite … Dalvik Virual Machine Graphics Camera Audio … SD/MMC Hardware Abstraction Libraries Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management App featuring audio contents Applications App0 Home Alarm App1 App2 Application Framework Acivity Manager Window Manager Content Providers Service Manager Media Manager … Native Libraries Android runtime Surface Manager Media Framework OpenGL|ES Core Libraries Audio Manager SQLlite … Dalvik Virual Machine Graphics Audio Camera SD/MMC … Hardware Abstraction Libraries Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management 35 App featuring audio contents Applications App0 Home Alarm App1 App2 Application Framework Acivity Manager Window Manager Content Providers Service Manager Media Manager … Native Libraries Android runtime JNI Surface Manager Media Framework OpenGL|ES Core Libraries Audio Manager SQLlite … Dalvik Virual Machine Graphics Camera Audio … SD/MMC Hardware Abstraction Libraries Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management App featuring audio contents Applications App0 Home Alarm App1 App2 Application Framework Acivity Manager Window Manager Content Providers Service Manager Media Manager … Native Libraries Surface Manager Media Framework Android runtime JNI OpenGL|ES Core Libraries … Dalvik Virual Machine Binder IPC SQLlite Audio Manager Graphics Audio Camera SD/MMC … Hardware Abstraction Libraries Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management 36 App featuring audio contents Applications App0 Home Alarm App1 App2 Application Framework Acivity Manager Window Manager Content Providers Service Manager Media Manager … Native Libraries Surface Manager Android runtime JNI Media Framework OpenGL|ES Core Libraries … Dalvik Virual Machine Binder IPC SQLlite Audio Manager Dynamic load Graphics Camera Audio … SD/MMC Hardware Abstraction Libraries Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management App featuring audio contents Applications App0 Home Alarm App1 App2 Application Framework Acivity Manager Window Manager Content Providers Service Manager Media Manager … Native Libraries Surface Manager Media Framework Android runtime JNI OpenGL|ES Core Libraries … Dalvik Virual Machine Binder IPC SQLlite Audio Manager Dynamic load Graphics Audio Camera SD/MMC … Hardware Abstraction Libraries Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management 37 Android applications • • • Android applications are written in the Java programming language, compiled for the Dalvik VM. The Android SDK tools build the code into an Android package (.apk archive). Android package (.apk) Android manifest Dalvik classes Resources A single .apk file is considered to be one application. Each application has its own virtual machine (VM), and runs in isolation from other applications. – Principle of least privilege each application has access only to the components that it requires to do its work and no more SECURE EXECUTION ENVIRONMENT Application components • Application components are the building blocks of an Android application. • There are four types of components: – Activities – An activity represents a single screen with a user interface. – Services – A service is a component that runs in the background to perform long-running operations or to perform work for remote processes. – Content providers - A content provider manages a shared set of application data. – Broadcast receivers – A broadcast receiver is a component that responds to system-wide broadcast announcements. 38 Processes and Threads • When an application starts, the Android system starts a new Linux process for the application with a single thread of execution. • By default, all components of the same application run in the same process and thread (called the main thread). – The main thread is in charge of dispatching events to the appropriate UI widgets (including drawing events). • When an app performs intensive work in response to user interaction, this single thread model can yield poor performance. – It is important to guarantee the responsiveness of application’s UI that programmers do not block the UI thread: operations that are not instantaneous should be managed dy separate threads (worker threads). Processes, Threads and Tasks APK Activity Activity APK Activity Activity Content Provider Content Provider Service Service All activities, content providers and services run in separate threads 39 Processes, Threads and Tasks APK Activity APK Activity Activity Activity Content Provider Content Provider Service Service P1 P2 P0 •By default,, Android binds the content of an APK to a Linux process •A custom configuration can be used to tune this relation with a lower granularity (actiivity,, service,,…) Processes, Threads and Tasks APK APK T0 Activity Activity T1 Activity Activity Content Provider Content Provider Service Service •A task is a collection of related activities •Tasks are usually interpreted as single applications. 40 Alessio Bonfietti Giuseppe Tagliavini Android SDK tools • Android SDK consists of a set of tools programmers can use through the Eclipse ADT plugin or from the command line. • Important tools: – android – Creates and updates Android projects, configures AVDs – emulator – Launches the emulator – adb – Interface with running emulator or connected device – bmgr – shell tool to interact with the backup manager on Android devices – logcat – shell tool to view log messages – … 41 New project with Eclipse Step 1 New project with Eclipse Step 2 42 New project with Eclipse Step 3 Project structure Android project Android manifest Java sources src/ directory Resources AndroidManifest.xml file res/ directory 43 Eclipse auto-generated activity • The onCreate() method is called by the Android system when this Activity starts • An Android user interface is composed of hierarchies of objects called views: setContentView() set the activity content to an explicit view R.layout.main references to a XML-based layout file • XML layout file Similar to Microsoft XAML 44 XML layout file (visual editor) Manifest file • • • Before the Android system can start an application component, the system must know that the component exists by reading the application's AndroidManifest.xml file. This file must be at the root of the project directory, and it contains the declaration of all components used by the application. Additional features: – Identify any user permissions the application requires, such as Internet access or read-access to the user's contacts. – Declare the minimum API Level required by the application, based on which APIs the application uses. – Declare hardware and software features used or required by the application, such as a camera, bluetooth services, or a multitouch screen. – API libraries the application needs to be linked against (other than the Android framework APIs), such as the Google Maps library. – … 45 Manifest file (visual editor) Thread analysis with DDMS 46 Profiling with DDMS This profiling method does not exceed the VM barrier Debugging with DDMS • If we introduce a null reference error… 47 Debugging with DDMS • … we can use logcat tool to trace the software exceptions. Alessio Bonfietti Giuseppe Tagliavini 48 Android NDK • The Android NDK is a companion tool to the Android SDK. WHY THE NDK? • Developers can directly access native libraries (C and C++) • It enables the use of native-code languages (C and C++) Android NDK • The Android NDK can provide benefits to certain classes of applications, in the form of reuse of existing code and (in some cases) increased speed. – Using native code does not result in an automatic performance increase. • The NDK provides: – A set of tools and build files used to generate native code libraries from C and C++ sources. – A set of native system headers and libraries. – A methodology to embed the corresponding native libraries into an application package file (.apk) that can be deployed on Android devices. 49 Project structure Android project Android manifest Java sources Resources Native code jni/ directory includes the native source files as well as the Android.mk file, which is a GNU makefile that will be parsed by the NDK build system Android package Android package (.apk) Android manifest Dalvik classes Resources Native libraries The JNI protocol enables to call back and forth between Dalvik VM and native code. 50 Steps to create a project 1. Place native sources under <project>/jni/ 2. Create <project>/jni/Android.mk to describe all native sources to the NDK build system 3. [optional] Create <project>/jni/Application.mk (it describes all native modules needed by the application and some other features). 4. Build native code by running the ndk-build script (located in the top-level NDK directory). 5. Compile your application using the SDK tools in the usual way (Eclipse or ant) – The SDK build tools will package the shared libraries in the application's deployable .apk file. Native code example • Java code (src/org/testjni/android/ MyClass.java): package org.testjni.android; class MyClass { static { System.loadLibrary("example"); } public native void someFunction(); … } • Native code (example.c) #include <jni.h> JNIEXPORT void JNICALL Java_org_testjni_android_MyClass_someFunction (JNIEnv * env, jobject obj) { … } 51 Native code example • Android.mk file: LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE LOCAL_CFLAGS LOCAL_SRC_FILES LOCAL_LDLIBS := := := := example -Werror example.c -llog include $(BUILD_SHARED_LIBRARY) Shared library Static and shared libraries • NDK build system can generate both static and shared libraries. • Static libraries are not directly copied into application packages, but can be used to build shared libraries. 52 Alessio Bonfietti Giuseppe Tagliavini OpenCV • • OpenCV is an open-source library aimed at real time computer vision. Application areas: – – – – Segmentation and recognition Motion tracking … Augmented reality OpenCV core objdetect calib3b highgui features2d gpu ffmpeg Giuseppe Tagliavini April 13th 2011 53 Demo structure Applications App Application Framework Acivity Manager … Window Manager Native Libraries Android runtime Surface Manager Media Framework Dynamic library Audio Manager … Static OpenCV library Graphics Audio Camera Core Libraries Dalvik Virual Machine … SD/MMC Hardware Abstraction Libraries Linux Kernel Camera Driver Display Driver Flash Memoruy Driver Binder (IPC) Driver Keypad Driver Wifi Driver Audio Drivers Power Management Alessio Bonfietti Giuseppe Tagliavini 54 Android Graphics Acceleration http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter45.html Android Graphics Acceleration 55 OpenGL ES Application Layer Application SDK: Java NDK: C/C++ Native Libraries Layer OpenGL ES OpenGL ES Application Layer Application Renderer Thread SDK: Java NDK: C/C++ OpenGL ES Native Librares Layer 56 OpenGL ES Application Layer Application Renderer Thread SDK: Java NDK: C/C++ JNI wrapper Native Libraries Layer OpenGL ES NDK Calls are NOT necessarily faster, but enable the use of all Native Libraries OpenGL ES Application SDK: Java Application Layer NDK: C/C++ OpenGL ES Native Libraries Layer HAL User Space Kernel Space Kernel Layer 57 OpenGL ES Application SDK: Java Application Layer NDK: C/C++ Native Libraries Layer OpenGL ES HAL libHGL User Space Kernel Space Kernel Layer OpenGL ES Application SDK: Java Application Layer NDK: C/C++ OpenGL ES libHGL Native Libraries Layer HAL User Space Kernel Space GPU Driver Kernel Layer 58 OpenGL ES Application SDK: Java Application Layer NDK: C/C++ Native Libraries Layer OpenGL ES HAL libHGL User Space Kernel Space Kernel Layer GPU Driver OpenGL ES Application SDK: Java Application Layer NDK: C/C++ OpenGL ES libagl libHGL Native Libraries Layer HAL User Space Kernel Space GPU Driver Kernel Layer 59 OpenGL ES Application SDK: Java Application Layer NDK: C/C++ OpenGL ES libagl Native Libraries Layer copyBLIT HAL libHGL User Space Kernel Space FB Driver GPU Driver Kernel Layer OpenGL ES Application SDK: Java Application Layer NDK: C/C++ OpenGL ES libagl Native Libraries Layer copyBLIT HAL libHGL User Space Kernel Space GPU Driver FB Driver Kernel Layer 60 Alessio Bonfietti Giuseppe Tagliavini Android roadmap 61 Android roadmap JIT NFC RenderScript Wi-Fi Direct Linux kernel 3.0.1 Linux kernel 2.6.36 Linux kernel 2.6.35 ??? Linux kernel 2.6.32 4.X Ice Cream Sandwich Linux kernel 2.6.29 Linux kernel 2.6.29 3.x Honeycomb Linux kernel 2.6.27 2.3 Gingerbread 2.2 Froyo 2.0/2.1 Eclair 1.6 Donut 1.5 Cupcake April 2009 Sep 2009 Oct 2009 May 2010 Dec 2010 Feb 2011 Oct 2011 2012 … http://www.android.com/ http://developer.android.com/index.html SDK, NDK, Tutorials http://source.android.com Source Code http://www.google.com/events/io/2011/index-live.html Google IO Developer Conference (tutorials, slides, presentations) 62