Deep Inside Android
OpenExpo 2008 - Zurich September 25th, 2008
Copyright 2007 Esmertec AG
Gilles Printemps - Senior Jan 2007 Architect
Agenda
What is Android? The Android platform Anatomy of an Android application Creating and deploying an application
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 2
Jan 2007
What is Android?
A complete software stack for mobile devices.
Introducing Android
A first joined project of the Open Handset Alliance (OHA).
First open, complete and free platform Software stack open-sourced under Apache 2.0 license Source code will be available and everyone will have the capability to built an image
The Android platform
It includes an operating system, a middleware and some applications.
Lightweight and full featured Developers can extend and replace existing components
A generous development environment
A SDK is available to build, compile, test and debug user applications.
Applications are developed using Java programming language No difference between the built-in applications and the user ones
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 3
Jan 2007
Agenda
What is Android?
The Android platform
Linux Kernel Native Libraries Android Runtime Application Framework
Anatomy of an Android application Creating and deploying an application
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 4
Jan 2007
Linux Kernel
Linux Kernel
Display Driver USB Driver
Camera Driver Keypad Driver
Bluetooth Driver WiFi Driver
Shared Memory Driver Audio Drivers
Binder (IPC) Driver Power Management
A Linux 2.6.24 fit for Android
Some common features have been removed
No GBLIC support No native windowing system Does not include the full set of Linux utilities
New Android specific components have been added
Alarm, Android Shared Memory Kernel Memory Killer, Kernel Debugger, Logger
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 5
Jan 2007
Linux Kernel (cont.)
Power Management
Based on the standard Linux Power Management, Android has its own component.
Application uses user space library to inform the framework about its constrains. Constraints are implemented using lock mechanism.
Partial Wake Lock
CPU
Screen
Keyboard
Full Wake Lock
Binder
Driver to facilitate inter-process communication between applications and services.
A pool of threads is associated to each application to process incoming IPC The driver performs mapping of object between two processes Binder uses an object reference as an address in a processs memory space
Application A
Function(Object)
Binder
- Marshaling - Relaying
Application B
Thread
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 6
Jan 2007
Native Libraries
Native Libraries Linux Kernel
Surface Manager WebKit
Media Framework SGL Libc
SQLite SSL
Open GL/ES FreeType
Android Libc implementation
A custom implementation, optimized for embedded use.
BSD license Small size and fast code paths Very fast custom pthread implementation Built-in support for android-specific services (system properties, log capabilities) Doesnt support some POSIX features
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 7
Jan 2007
Native Libraries (cont.)
Storage, rendering, multimedia,
Provides the main features on the Android platform:
SQLite, a simple relational database management system (No IPC, single file,) WebKit, an application framework that provides foundation for building a web browser Media Framework, based on PacketVideo openCORE platform (codec) Optimized 2D/3D graphic library based on OpenGL ES
Surface Manager
Provides a system-wide surface composer to render all the surfaces in a frame buffer.
Can combined 2D and 3D surfaces Can use OpenGL ES and 2D hardware accelerator for its compositions
7:56 PM
7:56 PM
App 1 Surface Flinger Manager App 2
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 8
Jan 2007
Native Libraries (cont.)
Audio Manager
Processes multiple audio streams into PCM audio out paths.
Handle several types of devices (headphone, ear piece,) Redirects audio stream to the specified output
App 1
Media Player ear piece
Audio Manager Manager 2 App 1
Tone Audio headphone
Hardware Abstraction Libraries
Defines the interface that Android requires hardware drivers to implement.
Set of standardized APIs the developer will have to implement Available for all the components a manufacturer can integrate on its Android platform
Graphics Audio Camera Bluetooth GPS Radio
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 9
Jan 2007
Android Runtime
Android Runtime Native Libraries Linux Kernel
Surface Manager WebKit
Media Framework Audio Manager Core Libraries Libc
SQLite SSL Virtual Machine
Open GL/ES FreeType
Dalvik Virtual Machine
An interpreter-only virtual machine (no JIT), register based.
Optimized for low memory requirements Designed to allow multiple VM instances to run at one Relying on underlying OS for process isolation, memory management and threading support Executes Dalvik Executables (DEX) files which are zipped into an Android Package (APK)
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 10
Jan 2007
Application Framework
Application Framework Android Runtime Native Libraries Linux Kernel
Activity Manager Package Manager
Window Manager Telephony Manager
Content Providers Resource Manager
View System Location Manager
Notification Manager
Some essential services
However, home made applications dont interact directly with them.
Activity Manager handles application lifecycle Package Manager holds information about applications loaded in the system Windows Manager handles all the application related windows View system provides all the standard widgets to build an application Hardware services provide access to lower-level hardware APIs: Bluetooth, telephony, location,
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 11
Jan 2007
Platform initialization
Kernel
The bootloader loads the kernel and starts the init process
Init
daemons
Zygote
Runtime Service Manager
Daemons for handling low level hardware interfaces are started up (usb, adb, debugger, radio) Zygote, the initial Dalvik VM process is created Runtime process initiates the Service Manager, a key element for Binders and IPC communication
Dalvik VM System Server Surface Manager Audio Manager
Registration
Runtime process requests Zygote to start a new instance of Dalvik for running the System Server
The two first processes are able to handle graphic and audio outputs
Telephony Activity Manager
Bluetooth Package Manager
All the others android components are then started
Service Manager
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 12
Jan 2007
Agenda
What is Android? The Android platform
Anatomy of an Android application
Activity Service Content Provider Processes and Tasks
Creating and deploying an application
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 13
Jan 2007
Activity
One single screen in an Android application
What is really an Activity?
Displays a user interface component and responds to system/user initiated.
When an application has a user interface, it contains one or more Activities One activity is then considered as the main entry point by the system An existing Activity can be replaced with a new one that fulfill the same contract Each Activity can be invoked from others applications
Adding a new Activity in an Android project
The new Java class must extend the framework Activity class Created Activity must be defined into the applications Manifest
<activity android:name=.sampleActivity android:label=@string/app_name> </activity>
Class name
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 14
Jan 2007
Activity Lifecycle
Android maintains an history stack of all the activities which are spawned in an application
A Classic scenario
An Activity is started and explicitly finalized (i.e. finish() is called).
OnCreate()
First method called during lifetime; the Activity is created Initialize all the resources (i.e. create a thread, connection to a database) Execution of the Activity can start at anytime; system put it on top of the stack Activity is on-screen (may be not in foreground); user cannot interact with.
Start up
OnStart()
OnResume()
Activity is visible to the user and has the focus.
OnPause()
Focus is lost and potentially the Activity will shutdown Save data (persistence)
Shutdown
OnStop()
Activity is no more on the top of the stack
Service Manager OnDestroy()
Activity does not exist anymore in the system Resources can be freed
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 15
Jan 2007
Activity Lifecycle (cont.)
Switching between Activities
Android will notify the running Activity through two new callbacks.
OnResume() OnFreeze() OnPause() OnStop() OnStart() OnRestart()
On Restart(): Signal the Activity will be restarted On Freeze(): All the Activity to save UI state Save the value belonging to the UI (i.e. field in a form)
Dropping an Activity
An Activity can be dropped from memory only in the following specific states:
When it is paused, even if it is completely alive When it is completely obscured by another Activity (stopped)
Normal Execution OnStop() OnDestroy() OnCreate() Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 16 The system can: - Ask the user to finish it - Directly kill the process it belongs to
Jan 2007
Navigation and Triggering
Intents / Intent Filters
Simple message objects that represent:
An intention to do something A declaration of capacity and interest in offering assistance to those in need
Activity
MP3
Player Activity Activity Manager
- Edit / Create a play list - View a play list
Intents
- Build a play list - Listen one play list
Service
- Play a MP3 file
IntentFilters
An intent is made up a number of pieces of information describing the action or the service:
action attribute is typically a verb (VIEW, EDIT, DIAL,) The data to operate on is expressed in the form of an Universal Resource Identifier (URI) category attribute gives additional information about the action to execute
<intent-filter> <action android:value=android.intent.action.VIEW /> <data android:mimeType=com.esmertec.player/media/12 /> </intent-filter>
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 17
Jan 2007
Broadcast Intent Receiver
Broadcasted notifications from the system
When using these Receivers?
When an application desires to receive and respond to a global event.
In order to be triggered when an event occurs, application does not have to be running By default, Android includes some built-in Intents Receiver
Alerter
Activity
- Get incoming calls - Get incoming SMS
Activity Manager
How to receive these specific Intents?
The user class must extend the framework IntentReceiver class To process incoming Intents, onReceiveIntent() method is implemented receiver element must be described into the applications Manifest
<receiver android:name=.Alerter> Class name <intent-filter> <action android:name=android.provider.Telephony.SMS_RECEIVED /> </intent-filter> </receiver>
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 18
Jan 2007
Service
A long-running background task
Defining and invoking a Service
Adding a Service with Android is quite similar than for an Activity.
Framework Service class must be extended The new Service must be defined into the applications Manifest
<service class=".AdderServiceImpl"/>
Class name
When a Service is using IPC, an AIDL description of its features is also needed.
Android Interface Definition Language (AIDL) is used to generated code to allow communication between two processes through IPC This mechanism is interface-based, similar to Corba (Unix) or COM (windows) but lighter weight With this specific mode, the Service is started through the binder and not with startService()
Activity Activity Manager
Start
Service
Notification Communication
Binder
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 19
Jan 2007
Content Provider
Handle data and expose them to other applications
Why using a Content Provider?
The only way to share data between Android applications.
It implements a standard set of methods to allow access to a data store Any form of data storage can be used like SQLite database, files, or memory hash map
Activity
APK
Activity
APK
Activity
Content Provider
Service
Data
SQLite
XML
Remote Store
A Content Providers exposes a unique URI used to query, add, update and delete data:
A standard prefix (content://) The authority part (fully-qualified to ensure uniqueness) The path to determine what kind of data is being requested A specific record being requested, if any
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 20
Jan 2007
Processes and Tasks
A relation between the kernel and Android
APK Task APK
Activity P1
Activity
Activity
Activity P2 P3
Content Provider Service
Content Provider Service
Processes
A low-level kernel process in which an applications code is running.
By default, Android binds the content of an APK to a Linux process Process tag can be used to tune this relation with a lower granularity (activity, service,)
Tasks
A notion that users know on other platform as application.
A collection of related Activities Capable of spanning multiple processes Interaction with Activities can be controlled through Activitys launchMode attribute
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 21
Jan 2007
Agenda
What is Android? The Android platform Anatomy of an Android application
Creating and deploying an application
Eclipse plug-in Building the package Running the application Debugging environment
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 22
Jan 2007
Creating an Application
Using the Eclipse plug-in
After some clicks, a basic application can be created, compiled and executed.
Only some properties are required to create an Android skeleton The plug-in creates all the file/directories useful to generate the final APK It can be freely downloaded by following the instructions at http://code.google.com/android/intro/installing.html
OpenExpo
XML
AndroidManifest
res
com.esmertec. openexpo
Resources
Java
SampleActivity
An element of a complete development environment containing:
An Android emulator which mimics the hardware and the software of a mobile phone Some tools for debugging, tracing, logging
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 23
Jan 2007
Creating an Application (cont.)
AndroidManifest.xml file
Describes the application components that the package exposes.
This file is mandatory and is located at the root of the application directory A top-level application presented to the user must include at least one Activity that supports the MAIN action and LAUNCHER category
<?xml version=1.0 encoding=utf-8?> <manifest xmlns:android=http://schemas.android.com/apk/res/android package=com.esmertec.openexpo android:versionCode=1 android:versionName=1.0.0> <application android:icon=@drawable/icon android:label=@string/app_name> <activity android:name=.SampleApp android:label=@string/act_name> <intent-filter> <action android:name=android.intent.action.MAIN /> <category android:name=android.intent.category.LAUNCHER /> </intent-filter> </activity> </application> </manifest>
XML
AndroidManifest
res
<?xml version=1.0 encoding=utf-8?> <resources> <string name=act_name> SampleActivity </string> <string name=app_name> Android Application </string> </resources>
XML
values
string
drawable
icon
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 24
Jan 2007
Building an APK
Dalvik Executable (DEX files)
A optimized format for efficient storage and memory mappable definition.
All classes are compiled with a standard Java language compiler Migration between a standard class file and the Android bytecode is done with the dx tool (For debugging purpose, it can also present a people-friendly readable format)
AIDL *. AIDL
*.
Java
*.
XML JAR Java
Java
*. Class
DEX
Application Manifest res Android R
Generating the Android specific java files:
When an application contains IPC services, AIDL files must be preprocessed to get Java classes Interface based AIDL tool saves developer the time used to write marshalling code All Android projects have a resource ID file called R.java Its basically a series of integers that can be used to reference the actual resources This file is generated automatically by aapt and should not be edited
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 25
Jan 2007
Building an APK (cont.)
Packaging the resources
During the build process, an archive containing application resources is also created.
This file is automatically generated by the aapt utility Among other things, its made up of an index resource file called resources.arsc
resources res
XML
ARSC XML JAR ap_
Manifest
res
Android
resources
Manifest
Generating the Android PacKage (APK)
Typically, an APK includes all the files related to a single Android application:
A compressed collection of AndroidManifest.xml, application code, resources Additionally, META_INF directory can integrate certificates used to sign application
resources
DEX
ARSC DEX ap_
res
META-INF
Resources
Package
XML
Manifest
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 26
Jan 2007
Running the application
Installing / Removing a package
All Android applications are handled by the Package Manager.
An APK must just be uploaded to a specific directory Two locations are monitored: /data/app (user) and /system/app (system)
data
app
Package
Package Manager
Android Debug Bridge (ADB)
A client/server application used as a gateway to communicate with the device. It allows:
Copying files to/from the device or the emulator (push, pull, install) Running shell binary commands
adb
Commands
adbd
Commands
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 27
Jan 2007
Debugging environment
Dalvik Debug Monitor Service (DDMS)
A GUI application providing debugging information to Android developers:
Processes and threads examination Memory heap statistics and allocation tracker Emulator controls to simulate specific device events File explorer to perform basic management on the file system Dump of the printed out messages,
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 28
Jan 2007
Copyright 2007 Esmertec AG
Esmertec Proprietary OpenExpo 2008 Page 29
Jan 2007