+
Inside
Android
CS2002S: Mobile
Development and Design
Melissa Densmore
University of Cape Town
CSC2002S Melissa Densmore, UCT
1
+ Android is a software stack for
mobile devices that includes an
operating system, middleware
and key applications.
The Android SDK provides
the tools and APIs
necessary to begin
developing applications on
the Android platform using
the Java programming
language
Melissa Densmore, UCT CSC2002S
Stats from developer.android.com
Image from https://www.phonearena.com/news/final‐version‐of‐android‐11‐has‐
dropped_id127027
Screenshots of the Augmented Reality representation of the 11 statue available with the 11
update.
If you have 11, share a screenshot of the 11 statue with Table Mountain!
2
+
The Android Open Source Project
Check out source.android.com
Melissa Densmore, UCT CSC2002S
This video is a bit old, but it gives an introduction to what an open source project is.
It’s not just the software, the but it’s the ecosystem of people working to support and
advance the software. One of those advancements includes the move from Dalvik to the
Android Runtime Environment
What are your main takeaways from this video?
• Layered system
• Open platform
• Easy for hardware manufacturers to adopt – improves innovation on hardware
* source.android.com
3
+
Key AOSP Features
Layered Architecture
Apache License
Open platform, easy to adopt
Tied to the Google ecosystem (…or not?!)
Developers can branch code for customization
For example, Amazon Fire, Google TV, Huawei
Not just the code – also the people, and the principles for
working together
Melissa Densmore, UCT CSC2002S
4
+
Open Handset Alliance
Mobile
Operators
14
Commercial-
Handset
ization
Manufacturers
Companies
23
12
84
Semi-
Software
conductor
Companies
Companies
17
21
Melissa Densmore, UCT CSC2002S
5
+
Melissa Densmore, UCT CSC2002S
6
+
11B Android Architecture
CSC2002S Melissa Densmore, UCT
7
+
Architecture
Diagram
Linux Kernel: manages
threading and low-level
memory management, well-
known kernel
Hardware Abstraction Layer
(HAL): api allows apps to be
written independently of
specific hardware models
Android Runtime: this is where
apps run
Native Libraries
Java API Framework
System Apps
Melissa Densmore, UCT CSC2002S
Recall what I taught earlier about software architectures. You can see at a glance that
Android uses a layered architecture, with each layer exposing an API to the next, but also
shielding the next layer from the implementation specifics required for that layer. This
allows an android app built and tested on one phone to effectively work on most other
phones.
Diagram from https://developer.android.com/guide/platform?hl=en
Built on top of a linux kernel, with drivers for the standard devices on android phones
Most aren’t aware of the linux kernel – but it is important to know for developers – you will
be using the adb command in the shell
Libraries are writtent in C or C++, compiled for the hardware and installed by the vendor
• Surface Manager: off screen bitmaps combined for rendering
• Graphics: openGL
• Media codecs: supports aac, avc, h.263, mp3, mpeg‐4
• Browser: webkit
App Framework
• Activity Manager: life cycle of applications
• Content providers: shared data between apps – eg. Contacts
• Resource Manager: images, files, R class names all your resources
• Location Manager: gps and wifi – where is your phone?
• Notification Manager: events and notifications – in a uniform and unobtrusive manner
8
Architecture Diagram
Applications
Home Contacts Browser Widgets Apps
and Widgets
Application Activity Window Content View Notification
Framework Manager Manager Providers System Manager
Package Telephony Location Sensor
Resource
Manager Manager Manager Manager
Surface Media Android Core
Libraries Manager Framework
SQLite Runtime Libraries
Dalvik
OpenGL | Virtual
WebKit …
ES Machine
Hardware
Abstraction Audio Bluetooth Camera Sensors …
Layer
Linux Flash
Display Bluetooth Camera Binder (IPC)
Memory
Kernel Driver Driver Driver
Driver
Driver
Keypad Audio Power
USB Driver WiFi Driver
Driver Drivers Management
Melissa Densmore, UCT CSC2002S
Built on top of a linux kernel, with drivers for the standard devices on android phones
Most aren’t aware of the linux kernel – but it is important to know for developers – you will
be using the adb command in the shell
Libraries are writtent in C or C++, compiled for the hardware and installed by the vendor
• Surface Manager: off screen bitmaps combined for rendering
• Graphics:
• Media codecs: supports aac, avc, h.263, mp3, mpeg‐4
• SQL – same as firefox and iPhone
• Browser: chrome, safari, iphone and s60
App Framework
• Activity Manager: life cycle of applications
• Content providers: shared data between apps – eg. Contacts
• Resource Manager: images, files, R class names all your resources
• Location Manager: gps and wifi – where is your phone?
• Notification Manager: events and notifications – in a uniform and unobtrusive manner
9
Browser: Integrated, based on the open source
+
WebKit engine
For the developer
Graphics: Powered by custom 2D graphics library,
with 3D graphics based on the OpenGL ES 1.0 spec
Data Storage: SQLite for structured data
Media: Support for common audio, video, and still
image formats (Mpeg-4, H.264, mp3, aac, amr, jpg,
png, gif)
Radio: multiple frequencies, and GSM or CDMA
Wireless: bluetooth, EDGE, 3G, 4G, 5G, and WiFi
Sensors and Devices: camera, gps, compass,
accelerometer
SDK: Rich development environment, including a
device emulator, debugging tools, memory and
performance profiling
Melissa Densmore, UCT CSC2002S
10
+
Android Runtime Environment
.. vs Dalvik Virtual Machine
• Introduced with KitKat, official with
Lollipop
• Nougat couples a JIT compiler with AoT
compilation
ARE DVM
• Ahead of Time • Just in Time (JIT)
(AoT) Compilation
Compilation • Faster to boot
• Apps compiled at • Lower space
install or boot requirements
• Faster to run
• Independent VMs
• Takes more space
Melissa Densmore, UCT CSC2002S
What’s the point of a runtime environment?
‐ interpretation vs compilation
11
+
Nuts and Bolts of Android Apps
Applications Application
and Widgets Camera WhatsApp 22seven Framework
APK APK APK
Activity Window
WhatsApp 22seven Manager Manager
Camera
Process Process Process
Content View
Providers System
Android
Runtime ARE ARE ARE Notification Package
Manager Manager
• Written in Java Telephony
Resource
• Compiled into Android Packages (apk) Manager
• Allocated individual process & user id
• Executed on own virtual machine Location Sensor
Manager Manager
• User IDs can be shared between apps
• To share files/content
• To share virtual machine
Melissa Densmore, UCT CSC2002S
12
+
Melissa Densmore, UCT CSC2002S
13
+
11C Android App
Development
CSC2002S Melissa Densmore, UCT
14
+
Download Android Studio
https://developer.android.com/studio/index.html
Melissa Densmore, UCT CSC2002S
Run on OSX, Linux, and Windows
You need JDK 6 – latest is 8 – but I often go with one version down – Java 8
Optional packages
Warning – it’s a ton of data, not for the bandwidth constrained!
15
+
Android Emulator
Emulation at the CPU level | Android Runtime Environment | “touch” with
mouse or touchscreen | device profiles for different phone models | fake
locations, calls, SMS, connection speeds | can’t fake sensors – but can use
webcam
Melissa Densmore, UCT CSC2002S
16
+
Kotlin vs Java
As of 2019, Android development is increasingly Kotlin-first
Examples now provided on developer.android.com
Interchangeable with Java - also runs on JVM
More succinct code
Fixes some issues with Java, but not always better
Null safe
No raw types
Arrays are invariant
Function Types
No checked Exceptions
Structured concurrency
https://kotlinlang.org/docs/reference/comparison-to-java.html
Melissa Densmore, UCT CSC2002S
Migrating new feature development to Kotlin helped the Google Home team reduce the
size of their codebase by 33% and decrease their number of Null Pointer Exception‐caused
crashes by 30% .
https://developer.android.com/kotlin/first
17
+
Melissa Densmore, UCT CSC2002S
Available through Android Studio Create New Project wizard.
This is an incredibly important decision – picking what API level you will support.
https://gs.statcounter.com/android‐version‐market‐share/mobile‐tablet/africa
18
+
99% (+8%)
92% (+7%)
85% (+6%)
79%(+17%)
62% (+24%)
28%
Melissa Densmore, UCT CSC2002S
But that chart is the global stats from Android. Global stats shares Market share in Africa –
noting that most of the market is at Marshmallow or above
However, this isn’t reflective what I see in the field ‐ I suspect this is data only from android
users that are connected to the play store and have a google account.
In a study I did a couple of years back, we found several phones out of a group of 25 people
that were using Kit Kat or earlier, which meant they couldn’t use our app, which was based
strongly on the notifications framework introduced in 5.0.
https://gs.statcounter.com/android‐version‐market‐share/mobile‐tablet/africa
19
Sizes
+
-
- - + ++ + +
https://developer.android.com/about/dashboards/index.html
xxhdpi
Normal
89.6%
Small ldpi
xhdpi
XL mdpi
tvdpi
Large
hdpi
Melissa Densmore, UCT CSC2002S
+ and ‐ indicate change from 2018
20
+
Melissa Densmore, UCT CSC2002S
21
+
11D Android Architecture
Components
CSC2002S Melissa Densmore, UCT
22
+
Architecture Components
Dividing apps into
components saves
Your App Camera
resources
Each component of an
app is a point of entry
for the operating
system
Your app can call parts Operating System
of other apps (e.g. use
the camera)
Melissa Densmore, UCT CSC2002S
23
+
Types of Components
Activities Services
User interface Background tasks
Application Screens Long-running operations
example: mail list, view example: checking for new
message, compose message mail, sending mail
Content Providers Broadcast Receivers
Manages shared app data Responds to system-wide
Private data too announcements (e.g. battery
low, screen off)
File system, SQLite, cloud
Status bar notifications
Query or modify data
Melissa Densmore, UCT CSC2002S
https://developer.android.com/guide/components/fundamentals#Components
24
+
Activities
1:1 correlation with
screens
Must be declared in
the manifest (xml)
Use <intent-filter> to
denote initial
activity
Activities are called
by other Activities
Melissa Densmore, UCT CSC2002S
Example in Kotlin
25
+
Services
A service can be bound, started,
or both
onBind(): allows IPC, provides
an API for interaction with app
onStartCommand(): long-
running background service
Multi-threaded!
Make sure to start a thread for
your work, or it will slow down
the main activity!
Use IntentService to handle
multiple calls at once
Can send toast and statusbar
notifications to the user
Ongoing services (foreground)
won’t be killed if system is
running low on memory
Melissa Densmore, UCT CSC2002S
26
+
Content Providers
Shareable data stores
Local or cloud: database, files, dropbox
Store your own data
Share your data
Access other ContentProviders via ContentResolver
contacts
calendar media
settings phone
alarm
browser
dictionary sms
camera
Melissa Densmore, UCT CSC2002S
27
+
Broadcast Receivers
Receives system
and application
messages (Intents)
and invokes
response
No UI – but can
update status bar
Battery low,
message received,
picture taken,
incoming call
Melissa Densmore, UCT CSC2002S
28
+
Intents
Activates services,
activities, and
receivers
Can be targeted or
broadcast
Explicit or implicit
(app chooser)
Receive implicit
intents using
<intent-filter>s
Melissa Densmore, UCT CSC2002S
29
+
Resource Management
Responsiveness is maintained at all costs!
Content providers automatically sleep
Activities and services can be shut down anytime
Process priority determines what gets killed (like *nix)
active, visible, service, background, empty
Lower priority processes are killed when resources are tight
Melissa Densmore, UCT CSC2002S
30
Resources
+• Stored externally – don’t declare
Strings in code
• Good for tailoring language,
devices, and for re-use
• Can also include arbitrary xml,
binary data
• Accessed in code through R
instance
Melissa Densmore, UCT CSC2002S
31
+
Layouts and Views
Declare all user interface elements in XML
Melissa Densmore, UCT CSC2002S
32
+
Material Components
Each widget is the basis for a unit
of action with the user.
Custom widgets are derived from
View
Melissa Densmore, UCT CSC2002S
33
+
Application Manifest
Every .apk contains resources, code, and a manifest file
Specifies the Java package for the app – a unique id
All components are declared in AndroidManifest.xml
Permissions (e.g. internet access, read/write content)
Minimum API level (e.g. Eclair, 8)
Hardware and software features (e.g. camera, Bluetooth)
External APIs (e.g. Google Maps)
and more...
Melissa Densmore, UCT CSC2002S
34
+
AndroidManifest.xml
<activity> activities
<service> services
<receiver> broadcast receivers
<provider> content providers
Melissa Densmore, UCT CSC2002S
35
+
Resources
Android Developer API Guides
http://developer.android.com/guide/index.html
StackOverflow
Assigned Reading/Coding (Self-Tutorial):
Android Training: Build Your First App
https://developer.android.com/training/basics/firstapp/inde
x.html
Melissa Densmore, UCT CSC2002S
36