# Introduction to Android ![img](img/g.png) **Linux Kernel**: threading, low-level memory management, driver **Hardware Abstraction Layer:** Libraries for hardware modules, because different devices have different hardware **Android Runtime:** A virtual machine **Native C/C++ Libraries:** Libraries for rendering & fundamental core functionalities **Java API Framework:** Programming interface **System Apps:** Normal apps that can be customised. #### Android-Specific Mods Android is based on Linux with some key differences: * Wake locks - keep the phone awake * Binder - inter-process communication; this is so no app can communicate with others, otherwise banking apps could be compromised * `ashmem` - shared memory * oom - kills processes when memory is low * alarm manager - wakes up the phone when necessary (raise to wake) #### Android Apps * Applications are sandboxed * A security mechanism for separately running applications and data * Android application sandbox * Linux is a multi-user system - this isn't needed * Makes use of Linux permissions and security * No root access #### System Boot-up Process 1. **Boot ROM/Bootloader**: Load bootloader into RAM, detect external RAM, set up network, memory, etc. 2. **Kernel**: Set up cache, protected memory, scheduling and load drivers 3. **Init**: Mount directories like `/sys`, `/dev` and run `rc` scripts. 4. **Zygote & VM**: Enables code sharing across the Android VM for quick startup of separate VMs for different apps 5. **System service**: Power manager, activity manager, telephony registry, package manager. ##### Zygote * Initialise a process that has all core libraries linked in. * Load all `*.java` and `*.android` classes at boot. * Initially create a single Android VM process * When the user runs an application * Creates a copy of itself in a separate address space * Does not copy memory; instead, refers to original memory until modified ![img](img/h.png) #### Android Compilation * Applications are written in Java and run on Google's own VM - Dalvik/Android Runtime ![img](img/i.png) * `.apk` is analogous to `.exe` ##### Dalvik * Dalvik architecture is register-based rather than stack-based. * This makes it optimised to use less space * Executes its own Dalvik bytecode rather than Java bytecode ![img](img/j.png) ##### Android Programming Model * Traditional OS applications * Single entry point - `main()` * OS loads the program into a process and executes * Java Applications * A Java VM is instantiated * Loads all classes used by the application * Executes `main()` * Component-based model * Multiple entry points - think sharing a photo through WhatsApp; WhatsApp loads in a different way. * Not all entry points are for the user ###### 4 Main Components Communicating via specific interfaces * Inter-process - between * Intra-process - within * Bound at runtime * Each with a specific life cycle * Dynamically loaded and unloaded as necessary 1. **Activities** * UI components 2. **Services** * Mechanism for doing something long-running in the background (front-end / back-end) 3. **Broadcast Receivers** * Respond to broadcast messages from the OS / other apps 4. **Content Providers** * Make data available for use by other apps