KEMBAR78
Android Internals | PDF
Android	
  
 Internals	
  


Marko	
  Gargenta	
  
  Marakana	
  
Agenda	
  
•    Android	
  Stack	
  
•    Opera6ng	
  System	
  Features	
  
•    Working	
  with	
  Hardware	
  
•    Android	
  Startup	
  &	
  Run6me	
  
•    Na6ve	
  Development	
  Kit	
  
•    Debugging	
  
•    Summary	
  
ANDROID	
  STACK	
  
The	
  Stack	
  
Linux	
  Kernel	
  
Android runs on Linux.                                        Applications


                                    Home      Contacts          Phone             Browser              Other

Linux provides as well as:
    Hardware abstraction layer                            Application Framework
    Memory management              Activity        Window                    Content                   View

    Process management
                                   Manager         Manager                  Providers                 System

                                   Package    Telephony         Resource           Location            Notiication
    Networking                     Manager     Manager          Manager            Manager             Manager


                                                                Libraries
Users never see Linux sub system   Surface       Media
                                                                   SQLite                  Android Runtime
                                   Manager     Framework

                                                                                                 Core Libs

The adb shell command opens        OpenGL      FreeType            WebKit

                                                                                                  Delvik
Linux shell                          SGL          SSL               libc
                                                                                                   VM




                                    Display    Camera         Linux Kernel              Flash                Binder
                                    Driver      Driver                                  Driver               Driver

                                    Keypad      WiFi                                    Audio                Power
                                     Driver     Driver                                  Driver               Mgmt
Na6ve	
  Libraries	
  
Bionic, a super fast and small                                Applications


GPL-based libc library optimized    Home      Contacts          Phone             Browser              Other

for embedded use
                                                          Application Framework
Surface Manager for composing      Activity        Window                    Content                   View

window manager with off-screen     Manager         Manager                  Providers                 System


buffering
                                   Package    Telephony         Resource           Location            Notiication
                                   Manager     Manager          Manager            Manager             Manager


                                                                Libraries
2D and 3D graphics hardware        Surface       Media
                                                                   SQLite                  Android Runtime
support or software simulation
                                   Manager     Framework

                                                                                                 Core Libs
                                   OpenGL      FreeType            WebKit

                                                                                                  Delvik
Media codecs offer support for       SGL          SSL               libc
                                                                                                   VM

major audio/video codecs
                                    Display    Camera         Linux Kernel              Flash                Binder
                                                                                        Driver               Driver
SQLite database
                                    Driver      Driver

                                    Keypad      WiFi                                    Audio                Power
                                     Driver     Driver                                  Driver               Mgmt


WebKit library for fast HTML
rendering
Dalvik	
  
Dalvik VM is Google’s implementation of Java

Optimized for mobile devices




Key Dalvik differences:

    Register-based versus stack-based VM
    Dalvik runs .dex files
    More efficient and compact implementation
    Different set of Java libraries than SDK
Android	
  and	
  Java	
  
Applica6on	
  Framework	
  
Activation manager controls the life                              Applications


cycle of the app                        Home      Contacts          Phone             Browser              Other




Content providers encapsulate data                            Application Framework
that is shared (e.g. contacts)         Activity        Window                    Content                   View
                                       Manager         Manager                  Providers                 System

                                       Package    Telephony         Resource           Location            Notiication
Resource manager manages               Manager     Manager          Manager            Manager             Manager


everything that is not the code                                     Libraries
                                       Surface       Media
                                                                       SQLite                  Android Runtime
                                       Manager     Framework

Location manager figures out the                                                                     Core Libs

location of the phone (GPS, GSM,
                                       OpenGL      FreeType            WebKit

                                                                                                      Delvik

WiFi)                                    SGL          SSL               libc
                                                                                                       VM




Notification manager for events         Display
                                        Driver
                                                   Camera
                                                    Driver
                                                                  Linux Kernel              Flash
                                                                                            Driver
                                                                                                                 Binder
                                                                                                                 Driver

such as arriving messages,              Keypad      WiFi                                    Audio
                                                                                            Driver
                                                                                                                 Power
                                                                                                                 Mgmt
                                         Driver     Driver
appointments, etc
Applica6ons	
  
OPERATING	
  SYSTEM	
  FEATURES	
  	
  
File	
  System	
  
The file system has three main mount points. One
for system, one for the apps, and one for whatever.

Each app has its own sandbox easily accessible to
it. No one else can access its data. The sandbox is
in /data/data/com.marakana/

SDCard is expected to always be there. It’s a good
place for large files, such as movies and music.
Everyone can access it.
Security	
  
Each Android application            Android Application
runs inside its own Linux
process.                                     Linux Process
Additionally, each application
has its own sandbox file                                   File
system with its own set of                     Prefs
                                       DB                 System
preferences and its own
database.

Other applications cannot
access any of its data,
unless it is explicitly shared.
WORKING	
  WITH	
  HARDWARE	
  
Camera	
  
Android SDK supports access to
built-in Camera and its preview.

You can access real-time frames,
or get a callback when shutter is
open. The photo data is passed
back in either raw or jpeg format.
WiFi
                                      	
  
WiFi API allows for managing your
connection, scanning for active WiFi
points and find out details about each.
Telephony
                                     	
  
With Telephony API, you can:

Make phone calls
Monitor phone state and activity
Access phone properties and status
Monitor data connectivity
Control the phone

It is a simple yet powerful API
ANDROID	
  	
  
STARTUP	
  &	
  	
  
RUNTIME	
  
Startup	
  Walkthrough	
  
Run6me	
  Overview	
  
Layer	
  Interac6ons
                                     	
  
There are three main
scenarios for your app to talk
to native library:

-  Directly
-  Via native service
-  Via native daemon

It will depend on the type of
app and type of native library
which method works best.
App	
  –	
  Run6me	
  Service	
  -­‐	
  Lib	
  
App	
  –	
  Run6me-­‐Na6ve	
  Service-­‐Lib	
  
App–Run6me–Na6ve	
  Daemon-­‐Lib	
  
Binder	
  IPC	
  




High-performance IPC: shared memory, per-process thread pool, synchronous
Java	
  Na6ve	
  Interface	
  

JNI defines naming and coding
convention so that Java VM can find
and call native code.

JNI is built into JVM to provide
access to OS I/O and others.
Building	
  and	
  Running	
  JNI	
  Code	
  
NATIVE	
  
DEVELOPMENT	
  
KIT	
  
What’s	
  in	
  NDK?	
  
Tools to build and compile your native code for the device
architecture (such as ARM)


A way to package your library into the APK file so you can
distribute your application easily


A set of native system headers that will be supported for
the future releases of Android platform (libc, libm, libz,
liblog, JNI headers, some C++ headers, and OpenGL)



(some) documentation, sample code and examples
Why	
  NDK?	
  

NDK allows you to develop parts of your Android
application in C/C++.

You cannot develop native-only apps in NDK –
your app is still subject to security sandboxing.

Main motivation for native code is performance.
Using	
  NDK	
  
DEBUGGING	
  	
  
ANDROID	
  APPS	
  
LogCat
                              	
  
The universal, most
versatile way to track
what is going on in
your app.

Can be viewed via
command line or
Eclipse.

Logs can be
generated both from
SDK Java code, or
low-level C code via
Bionic libc extension.
Debugger
                                    	
  




Your standard debugger is included in SDK, with all the usual bells & whistles.
TraceView	
  




TraceView helps you profile you application and find bottlenecks. It shows
execution of various calls through the entire stack. You can zoom into specific
calls.
Hierarchy	
  Viewer
                                          	
  
Hierarchy Viewer helps
you analyze your User
Interface.

Base UI tends to be the
most “expensive” part of
your application, this tool
is very useful.
Summary	
  
     For most applications, you will just
     need Android SDK to develop apps.

     Sometimes you may need NDK to
     make parts of your app run faster.

     Ultimately, you can do whatever you
     want by compiling Android platform
     from source.



     Marko Gargenta
     Marakana.com



     Licensed under Creative Commons
     License (cc-by-nc-nd). Please Share!

Android Internals

  • 1.
    Android   Internals   Marko  Gargenta   Marakana  
  • 2.
    Agenda   •  Android  Stack   •  Opera6ng  System  Features   •  Working  with  Hardware   •  Android  Startup  &  Run6me   •  Na6ve  Development  Kit   •  Debugging   •  Summary  
  • 3.
  • 4.
  • 5.
    Linux  Kernel   Androidruns on Linux. Applications Home Contacts Phone Browser Other Linux provides as well as: Hardware abstraction layer Application Framework Memory management Activity Window Content View Process management Manager Manager Providers System Package Telephony Resource Location Notiication Networking Manager Manager Manager Manager Manager Libraries Users never see Linux sub system Surface Media SQLite Android Runtime Manager Framework Core Libs The adb shell command opens OpenGL FreeType WebKit Delvik Linux shell SGL SSL libc VM Display Camera Linux Kernel Flash Binder Driver Driver Driver Driver Keypad WiFi Audio Power Driver Driver Driver Mgmt
  • 6.
    Na6ve  Libraries   Bionic,a super fast and small Applications GPL-based libc library optimized Home Contacts Phone Browser Other for embedded use Application Framework Surface Manager for composing Activity Window Content View window manager with off-screen Manager Manager Providers System buffering Package Telephony Resource Location Notiication Manager Manager Manager Manager Manager Libraries 2D and 3D graphics hardware Surface Media SQLite Android Runtime support or software simulation Manager Framework Core Libs OpenGL FreeType WebKit Delvik Media codecs offer support for SGL SSL libc VM major audio/video codecs Display Camera Linux Kernel Flash Binder Driver Driver SQLite database Driver Driver Keypad WiFi Audio Power Driver Driver Driver Mgmt WebKit library for fast HTML rendering
  • 7.
    Dalvik   Dalvik VMis Google’s implementation of Java Optimized for mobile devices Key Dalvik differences: Register-based versus stack-based VM Dalvik runs .dex files More efficient and compact implementation Different set of Java libraries than SDK
  • 8.
  • 9.
    Applica6on  Framework   Activationmanager controls the life Applications cycle of the app Home Contacts Phone Browser Other Content providers encapsulate data Application Framework that is shared (e.g. contacts) Activity Window Content View Manager Manager Providers System Package Telephony Resource Location Notiication Resource manager manages Manager Manager Manager Manager Manager everything that is not the code Libraries Surface Media SQLite Android Runtime Manager Framework Location manager figures out the Core Libs location of the phone (GPS, GSM, OpenGL FreeType WebKit Delvik WiFi) SGL SSL libc VM Notification manager for events Display Driver Camera Driver Linux Kernel Flash Driver Binder Driver such as arriving messages, Keypad WiFi Audio Driver Power Mgmt Driver Driver appointments, etc
  • 10.
  • 11.
  • 12.
    File  System   Thefile system has three main mount points. One for system, one for the apps, and one for whatever. Each app has its own sandbox easily accessible to it. No one else can access its data. The sandbox is in /data/data/com.marakana/ SDCard is expected to always be there. It’s a good place for large files, such as movies and music. Everyone can access it.
  • 13.
    Security   Each Androidapplication Android Application runs inside its own Linux process. Linux Process Additionally, each application has its own sandbox file File system with its own set of Prefs DB System preferences and its own database. Other applications cannot access any of its data, unless it is explicitly shared.
  • 14.
  • 15.
    Camera   Android SDKsupports access to built-in Camera and its preview. You can access real-time frames, or get a callback when shutter is open. The photo data is passed back in either raw or jpeg format.
  • 16.
    WiFi   WiFi API allows for managing your connection, scanning for active WiFi points and find out details about each.
  • 17.
    Telephony   With Telephony API, you can: Make phone calls Monitor phone state and activity Access phone properties and status Monitor data connectivity Control the phone It is a simple yet powerful API
  • 18.
    ANDROID     STARTUP  &     RUNTIME  
  • 19.
  • 20.
  • 21.
    Layer  Interac6ons   There are three main scenarios for your app to talk to native library: -  Directly -  Via native service -  Via native daemon It will depend on the type of app and type of native library which method works best.
  • 22.
    App  –  Run6me  Service  -­‐  Lib  
  • 23.
    App  –  Run6me-­‐Na6ve  Service-­‐Lib  
  • 24.
  • 25.
    Binder  IPC   High-performanceIPC: shared memory, per-process thread pool, synchronous
  • 26.
    Java  Na6ve  Interface   JNI defines naming and coding convention so that Java VM can find and call native code. JNI is built into JVM to provide access to OS I/O and others.
  • 27.
  • 28.
  • 29.
    What’s  in  NDK?   Tools to build and compile your native code for the device architecture (such as ARM) A way to package your library into the APK file so you can distribute your application easily A set of native system headers that will be supported for the future releases of Android platform (libc, libm, libz, liblog, JNI headers, some C++ headers, and OpenGL) (some) documentation, sample code and examples
  • 30.
    Why  NDK?   NDKallows you to develop parts of your Android application in C/C++. You cannot develop native-only apps in NDK – your app is still subject to security sandboxing. Main motivation for native code is performance.
  • 31.
  • 32.
  • 33.
    LogCat   The universal, most versatile way to track what is going on in your app. Can be viewed via command line or Eclipse. Logs can be generated both from SDK Java code, or low-level C code via Bionic libc extension.
  • 34.
    Debugger   Your standard debugger is included in SDK, with all the usual bells & whistles.
  • 35.
    TraceView   TraceView helpsyou profile you application and find bottlenecks. It shows execution of various calls through the entire stack. You can zoom into specific calls.
  • 36.
    Hierarchy  Viewer   Hierarchy Viewer helps you analyze your User Interface. Base UI tends to be the most “expensive” part of your application, this tool is very useful.
  • 37.
    Summary   For most applications, you will just need Android SDK to develop apps. Sometimes you may need NDK to make parts of your app run faster. Ultimately, you can do whatever you want by compiling Android platform from source. Marko Gargenta Marakana.com Licensed under Creative Commons License (cc-by-nc-nd). Please Share!