Programming with Android: System Architecture
Luca BedogniDipartimento di Scienze dell’Informazione
Università di Bologna
Luca Bedogni - Programming with Android – System Architecture 2
Outline
Android Components: Content Providers
Android Components: Services
Android Components: Intents
Android Components: Activities
Android Java Virtual Machine
Android Architecture: An Overview
Android Application Distribution and Markets
Luca Bedogni - Programming with Android – System Architecture 3
Android … What?
vAndroid is a Linux-based platform for mobile devices …
§ Operating System§ Middleware§ Applications§ Software Development Kit (SDK)
v Which kind of mobile devices … (examples)
SMARTPHONES TABLETS EREADERS ANDROID TV GOOGLE GLASSES
?
Luca Bedogni - Programming with Android – System Architecture 4
Android … What?
SMARTPHONES TABLETS EREADERS ANDROID TV GOOGLE GLASSES
?SMART FRIDGE
ANDROID MICROWAVE
Luca Bedogni - Programming with Android – System Architecture 5
Android … When?
ØGoogle buys Android from the Android Inch
ØOpen Handset Alliance (OHA) created for open standards for mobile devices. Partners of OHA: Google, Motorola, Samsung, Vodafone, T-Mobile, etc
ØAndroid 1.0 Released
ØThe first Android smartphone: G1 HTC-Dream
ØAndroid 1.1 ReleasedØAndroid 1.5 (CupCake) Released
2005
2006
2007
2008
2009
Time
Luca Bedogni - Programming with Android – System Architecture 6
Android … When?
ØAndroid 1.6 (Donut) Released
ØAndroid 2.0 (Eclair) Released
ØAndroid 2.2 (Froyo) Released
ØAndroid 2.3 (Gingerbread) Released
ØAndroid 3.0 (Honeycomb) Released(First version for devices with larger screens such as tablets)
ØAndroid 4.0 (Ice-Cream Sandwich) Released. (It merges the 3.x tab centric design and the v2.x phone based design into a single version.)
2008
2009
2010
2011
2012
Time
Luca Bedogni - Programming with Android – System Architecture 7
Android … When?
ØAndroid 4.4 (Kitkat) Released
ØWireless printing capabilityØAbility for applications to use "immersive mode”ØPerformance optimization ØNew experimental runtime virtual machine, ART…
API Level 19 (Android 4.4):
Ø Support to new embedded sensors (e.g. STEP_DETECTOR)
Ø Adaptive video playback functionalitiesØ Read and write SMS and MMS messages
(managing default text messaging client)
2012
2013
Time
ANDROID 5.0
2014
Luca Bedogni - Programming with Android – System Architecture 8
Android … market share
Luca Bedogni - Programming with Android – System Architecture 9
Android … heterogeneityht
tp://
open
sign
al.c
om/re
ports
/frag
men
tatio
n-20
13/
11,868 different devices in 2013!
Luca Bedogni - Programming with Android – System Architecture 10
Android … heterogeneity
24,093 different devices in 2015!
http
://op
ensi
gnal
.com
/repo
rts/2
015/
08/a
ndro
id-fr
agm
enta
tion/
Luca Bedogni - Programming with Android – System Architecture 11
Android … heterogeneity
24,093 different devices in 2015!
http
://op
ensi
gnal
.com
/repo
rts/2
015/
08/a
ndro
id-fr
agm
enta
tion/
Luca Bedogni - Programming with Android – System Architecture 12
Android … heterogeneity
Luca Bedogni - Programming with Android – System Architecture 13
Android … heterogeneity
2015
2013
Luca Bedogni - Programming with Android – System Architecture 14
Android … heterogeneity
Luca Bedogni - Programming with Android – System Architecture 15
Android … heterogeneity
Luca Bedogni - Programming with Android – System Architecture 16
Android … heterogeneity
http://developer.android.com/about/dashboards/index.html
Luca Bedogni - Programming with Android – System Architecture 17
Android … heterogeneity
Luca Bedogni - Programming with Android – System Architecture 18
Android … heterogeneity
Luca Bedogni - Programming with Android – System Architecture 19
Android … heterogeneity
Luca Bedogni - Programming with Android – System Architecture 20
Android … When?
http://www.appbrain.com/stats/android-market-app-categories
ANDROID APP CATEGORIES
http://www.onlinemarketing-trends.com/2011/07/android-marketplace-top-5-statistics.html
ANDROID APP PRICE
Luca Bedogni - Programming with Android – System Architecture 21
The Android Architecture
Luca Bedogni - Programming with Android – System Architecture 22
The Android Architecture
Built on top of Linux kernelAdvantages:
Ø Portability (i.e. easy to compile on different hardware architectures)
Ø Security (e.g. secure multi-process environment)
Ø Power Management
Ø ART relies on the kernel for threads and memory management
Ø Manufacturers build on top of a reliable kernel
Luca Bedogni - Programming with Android – System Architecture
Kernel Security
vUser based permission modelvProcesses are isolatedvInter-process communication (IPC)vResources are protected from other processesvEach application has its own User ID (UID)vSandboxvVerified boot
23
Luca Bedogni - Programming with Android – System Architecture 24
The Android Architecture
HAL
Advantages:
Ø Shadows the real device
Ø Manages different devices of the same type
Ø Standard interfaces to expose lower level capabilities to higher level APIs
Luca Bedogni - Programming with Android – System Architecture 25
HAL
Ø Standard interface that manufacturers have to implement – Android is agnostic about lower level driver implementations
Ø Application developers rely on common APIsØ Depending on the hardware, appropriate libraries are loaded
Luca Bedogni - Programming with Android – System Architecture 26
The Android Architecture
Native Libraries (C/C++ code)
ØGraphics (Surface Manager)
ØMultimedia (Media Framework)
ØDatabase DBMS (SQLite)
ØFont Management (FreeType)
Ø WebKit
ØC libraries (Bionic)
Ø….
Luca Bedogni - Programming with Android – System Architecture 27
Android NDK
ØEnables C/C++ coding
ØUseful if you want to interact/extend with some native librariesØ PerformanceØ Reuse your C/C++ libraries
ØJAVA APIs are provided for most used libraries
ØNDK can be installed as an Android Studio plugin
public class myNDKActivity extends Activity {public native void doNothing():
}
Luca Bedogni - Programming with Android – System Architecture 28
The Android Architecture
APIs (Core Components of Android)
ØActivity Manager
ØPacket Manager
ØTelephony Manager
ØLocation Manager
ØContents Provider
ØNotification Manager
Ø….
Luca Bedogni - Programming with Android – System Architecture 29
Java APIs
ØView SystemØ Through which you build the APP UI
ØResource ManagerØ Through which you handle resources
ØNotification ManagerØ Through which you can access to different kind of notifications
ØActivity ManagerØ Which handles the Activity lifecycle
ØContent ProvidersØ To share data among APPs
Luca Bedogni - Programming with Android – System Architecture 30
The Android Architecture
Applications(Written in Java code)
ØAndroid Play Store
ØEntertainment
ØProductivity
ØPersonalization
ØEducation
ØGeo-communication
Ø….
Luca Bedogni - Programming with Android – System Architecture 31
The Android Architecture
ART (VM)
ØNovel Java Virtual Machine implementation (not using the Oracle JVM)
ØOptimized for memory-constrained devices
ØFaster than Oracle JVM
ØART optional from 4.4, mandatory from 5.0
Luca Bedogni - Programming with Android – System Architecture 32
ART
ØStarting from Android 5.0, ART is used instead of Dalvik
ØDesigned to run multiple VM on low end devices
ØRuns DEX bytecode
ØAhead-of-time (AOT) and Just-in-time (JIT) compilationØ AOT: At install time, ART compiles APPs using an on-device tool called dex2oatØ JIT: code profiling
ØOptimized Garbage collection
Luca Bedogni - Programming with Android – System Architecture 33
ART
ØStarting from Android 5.0, ART is used instead of Dalvik
ØDesigned to run multiple VM on low end devices
ØRuns DEX bytecode
ØAhead-of-time (AOT) and Just-in-time (JIT) compilationØ AOT: At install time, ART compiles APPs using an on-device tool called dex2oatØ JIT: code profiling
ØOptimized Garbage collection
Luca Bedogni - Programming with Android – System Architecture 34
Android Applications Design
ØGUI Definition
ØEvents Management
ØApplication Data Management
ØBackground Operations
ØUser Notifications
APPLICATION DESIGN:
Luca Bedogni - Programming with Android – System Architecture 35
Android Applications Design
ØActivities & Fragments
Ø Intents
ØServices
ØContent Providers
Ø Broadcast Receivers
APPLICATION COMPONENTS
Luca Bedogni - Programming with Android – System Architecture 36
Android Components: Activities
ØAn Activity corresponds to a single screen of the Application.
ØAn Application can be composed of multiples screens (Activities).
ØThe Home Activity is shown when the user launches an application.
ØDifferent activities can exhange information one with each other.
Hello World!
Android HelloWorld
Button1
Luca Bedogni - Programming with Android – System Architecture 37
Android Components: Activities
ØEach activity is composed by a list of graphics components.ØSome of these components (also called Views) can interact
with the user by handling events (e.g. Buttons). ØTwo ways to build the graphic interface:
Example:
Button button=new Button (this);TextView text= new TextView();text.setText(“Hello world”);
PROGRAMMATIC APPROACH
Luca Bedogni - Programming with Android – System Architecture 38
Android Components: Activities
ØEach activity is composed by a list of graphics components.ØSome of these components (also called Views) can interact
with the user by handling events (e.g. Buttons). ØTwo ways to build the graphic interface:
DECLARATIVE APPROACH
Example:
< TextView android.text=@string/hello” android:textcolor=@color/blue android:layout_width=“fill_parent” android:layout_height=“wrap_content” />< Button android.id=“@+id/Button01” android:textcolor=“@color/blue”android:layout_width=“fill_parent” android:layout_height=“wrap_content” />
Luca Bedogni - Programming with Android – System Architecture 39
- Build the application layout through XML files (like HTML)
- Define two different XML layoutsfor two different devices
- At runtime, Android detects the current device configuration and loads the appropriate resources for the application
- No need to recompile!- Just add a new XML file if you
need to support a new device
EXAMPLE
Device 1HIGH screen pixel density
Device 2LOW screen pixel density
XML Layout FileDevice 1
XML Layout FileDevice 2
Java App Code
Android Components: Activities
Luca Bedogni - Programming with Android – System Architecture 40
Android Components: Activities
ØAndroid applications typically use both the approaches!
DECLARATIVE APPROACH
PROGRAMMATIC APPROACH
Define the Application layouts and resources used by the Application (e.g. labels).
Manages the events, and handles the interaction with the user.
XML Code
Java Code
Luca Bedogni - Programming with Android – System Architecture 41
Android Components: Activities
ØViews can generate events (caused by human interactions) that must be managed by the Android-developer.
public void onClick(View arg0) {if (arg0 == Button) {
// Manage Button events}
}
ESEMPIO
Butto
n
Text
Edit
Luca Bedogni - Programming with Android – System Architecture 42
Android Components: Activities
ØThe Activity Manager is responsible for creating, destroying, managing activities.
ØActivities can be on different states: starting, running, stopped, destroyed, paused.
ØOnly one activity can be on the runningstate at a time.
ØActivities are organized on a stack, and have an event-driven life cycle (details later …)
Luca Bedogni - Programming with Android – System Architecture 43
Android Components: Activities
ØMain difference between Android-programming and Java (Oracle) -programming:
ØMobile devices have constrained resource capabilities!
ØActivity lifetime depends on users’ choice (i.e. change of visibility) as well as on system contraints (i.e. memory shortage).
ØDeveloper must implement lifecycle methods to account for state changes of each Activity …
Luca Bedogni - Programming with Android – System Architecture 44
Android Components: Activities
public class MyApp extends Activity {
public void onCreate() { ... }public void onPause() { ... }public void onStop() { ... }public void onDestroy(){ ... }….
}
Called when the Activityis created the first time.
Called when the Activityis partially visible.
Called when the Activityis no longer visible.
Called when the Activityis dismissed.
Luca Bedogni - Programming with Android – System Architecture 45
Android Components: Intents
Ø Intents: asynchronous messages to activate core Android components (e.g. Activities).
ØExplicit Intent à The component (e.g. Activity1) specifies the destination of the intent (e.g. Activity 2).
LOGIN
PASSWORD
Login
luca
**********
Welcome Luca!
Login Intent
Activ
ity1
Activ
ity2
Luca Bedogni - Programming with Android – System Architecture 46
Android Components: Intents
Ø Intents: asynchronous messages to activate core Android components (e.g. Activities).
Ø Implicit Intent à The component (e.g. Activity1) specifies the type of the intent (e.g. “View a video”).
View
Implicit Intent
Activ
ity1
Activ
ity2
Activ
ity2
Multiple choicesmight be available
to the user! } Intent-Filters
Luca Bedogni - Programming with Android – System Architecture 47
Android Components: Services
ØServices: like Activities, but run in background and do not provide an user interface.
ØUsed for non-interactive tasks (e.g. networking).Ø Service life-time composed of 3 states:
Starting Destroyed
Running
(on background)
onCreate()onStart() onDestroy()
Luca Bedogni - Programming with Android – System Architecture 48
Android Components: Content Providers
ØEach Android application has its own private set of data (managed through files or through SQLite database).
ØContent Providers: Standard interface to access and share data among different applications.
DB
APPinsert()
update()
delete()
query()
ContentProvider
e.g. Photo Gallery
Luca Bedogni - Programming with Android – System Architecture 49
Android Components: Broadcast Receivers
ØPublish/Subscribeparadigm
ØBroadcast Receivers: An application can be signaled of external events.
ØNotification types: Call incoming, SMS delivery, Wifi network detected, etc
Luca Bedogni - Programming with Android – System Architecture 50
Android Components: Broadcast Receivers
class WifiReceiver extends BroadcastReceiver {public void onReceive(Context c, Intent intent) {
String s = new StringBuilder();wifiList = mainWifi.getScanResults();for(int i = 0; i < wifiList.size(); i++){
s.append(new Integer(i+1).toString() + ".");s.append((wifiList.get(i)).toString());s.append("\\n");
}mainText.setText(sb);
}}
BROADCAST RECEIVER example
Luca Bedogni - Programming with Android – System Architecture 51
Android Components: System API
ØUsing the components described so far, Android applications can then leverage the system API …
Ø Telephony Manager data access (call, SMS, etc)Ø Sensor management (GPS, accelerometer, etc)Ø Network connectivity (Wifi, bluetooth, NFC, etc)Ø Web surfing (HTTP client, WebView, etc)Ø Storage management (files, SQLite db, etc)Ø ….
SOME EXAMPLEs …
Luca Bedogni - Programming with Android – System Architecture 52
Android Components: Google API
Ø… or easily interface with other Google services:
Luca Bedogni - Programming with Android – System Architecture 53
ØEach Android application iscontained on a single APK file.
Ø Java Byte-code
Ø Resources (e.g. images. videos, XML layout files)
ØLibraries (optimal native C/C++ code)
APKFILE
XMLFiles
C
Android Application Distribution
Luca Bedogni - Programming with Android – System Architecture 54
Android Application Distribution
ØEach application must be signed through a key before being distributed.
ØApplications can be distributedvia Web or via Stores.
ØAndroid Play Store: application store run by Google … but several other application stores are available (they are just normal applications).
Luca Bedogni - Programming with Android – System Architecture 55
Android Application Security
ØAndroid applications run with a distinct system identity (Linux user ID and group ID), in an isolated way.
ØApplications must explicitly share resources and data. They do this by declaring the permissions they need for additional capabilities. ØApplications statically declare the permissions they require.ØUser must give his/her consensus during the installation.ØEverything changes starting from 6.0
<uses-permission android:name=“android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name=“android.permission.INTERNET" />
ANDROIDMANIFEST.XML
Luca Bedogni - Programming with Android – System Architecture 56
Permissions from 6.0
Ø Android Marshmallow (6.0) introduces runtime permissionsØ Permissions are not requested at install-time, but when they are used
Ø Bad behavior: request everything on the first screen of the first launch of the app
Ø Good behavior: request permission only when neededØAPP should work (with limited functions) even if some
permissions are not granted