Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android OS Internals

Android OS Internals

In this talk, we will explore some important internal components of the Android operating system. Also, we will see how the Android framework and other cross-platform frameworks work under the hood.

Avatar for krupal shah

krupal shah

July 05, 2018
Tweet

More Decks by krupal shah

Other Decks in Programming

Transcript

  1. Why? ❖ Better understanding of • underlying operating system •

    stacktraces and errors • how Android framework works internally • how cross-platform frameworks work internally ❖ To know the limitations of extensibility of Android ❖ To know why of everything you write ❖ Just because you like getting bored
  2. Android ~ Linux (!=) ❖ Android kernel is based on

    Linux kernel. ❖ Highly modified version of Linux kernel. ❖ Modified kernel for the requirements of smartphones. ❖ Android uses kernel components from SE Linux(Security Enhanced Linux).
  3. Android Open Source Project (AOSP) ❖ Android is Open-source Operating

    System. ❖ Download source code from https://source.android.com/. ❖ Browse source code - https://github.com/aosp-mirror ❖ Total more than 90 repositories. ❖ ~30 GB in size. ~50 GB after build. ❖ Takes ~2 hours to build first time. Avg ~30-40 min. ❖ Report bugs/request features at http://issuetracker.google.com/
  4. AOSP : Bionic • Bionic is the standard C library

    developed for Android (similar to libc for ANSI C). • Small size. • Speed: Bionic was designed for CPUs at relatively low clock frequencies. • BSD Licence [patent free]
  5. AOSP : Dalvik and ART • Dalvik is a Virtual

    Machine (VM) for running Java bytecode. • ART stands for Android Runtime which is an extended VM that replaces Dalvik VM for 5.0 and above devices. • Unlike Dalvik, ART introduces the use of ahead-of-time (AOT) compilation by compiling entire applications into machine instructions upon their installation. • Dalvik only used Just In Time(JIT) compilation. • ART brings faster execution of applications, improved memory allocation and garbage collection (GC) mechanisms
  6. AOSP : CTS • Compatibility Test Suite • Google’s answer

    to device fragmentation on Android. • Automated tests to be passed by device manufacturer. • Checks unified behaviour of most framework components. • Customized operating systems must have to pass CTS tests before they put devices built with that in the market.
  7. AOSP : Packages • Standard Android application that are available

    as part of the AOSP - Camera, SMS, Dialer, Launcher, etc.
  8. AOSP : System • Source code files for the core

    Android OS. • A minimal Linux system that is started before the Dalvik VM and any java based services are enabled. • includes the source code for the init process.
  9. AOSP : Kernel • Definition : The kernel is a

    computer program that is the core of a computer's operating system, with complete control over everything in the system. • Talks to the hardware layer and operating system. • Handles CPU, RAM, I/O and Device Drivers. • Linux kernel modified for Android • Two major modifications: ▪ WakeLocks ▪ Out of memory killer (OOM Killer)
  10. Some Linux Terminology • Process : Process is an instance

    of a computer program that is being executed. Depending on the operating system, a process may be made up of multiple threads of execution that execute instructions concurrently. • Service : A service is a program which responds to requests from other programs over some inter-process communication mechanism. • Daemon : A daemon is a background, non-interactive program. It runs as a background process, rather than being under the direct control of an interactive user.
  11. Init Process - init is the first process started during

    booting of the os. - Init is a daemon process. - continues running until the system is shut down.
  12. UNIX Syscalls and fork() - A system call is the

    programmatic way in which a program requests something from the kernel of the operating system. - fork() is a syscall whereby a process creates a copy of itself. - Fork is the primary (and historically, only) method of process creation on Unix-like operating systems.
  13. Zygote Process • Zygote - “the first cell formed when

    a new organism is produced” . • a “Warmed-up” process, which means it’s a process that’s been initialized and has all the core libraries linked in. • Preloads all Android framework classes and resources for the application. • When you start an application, the Zygote is forked.
  14. System Server • A process that starts almost all of

    the Java system services. • Serves the entry point for framework, that’s why system server. • Includes are all the managers such as the Location Manager, Bluetooth Manager, Connectivity Manager, Activity Manager, Package Manager etc. • Once registered, clients in the application layer may request access to services in the System Server via the Service Manager. public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); return netInfo != null && netInfo.isConnectedOrConnecting(); }
  15. Manifest - Defines framework components and user permissions. - Mostly

    used by PackageManager. - While installing, it reads all the permissions needed. - Describes contents of the APK before extracting.
  16. Security • The Android system assigns a unique user ID

    (UID) to each Android application and runs it as that user in a separate process. • This sets up a kernel-level Application Sandbox. • Each user (Application) have some user privileges about what that can do with the rest of the system. • If application A tries to do something malicious like read application B's data or dial the phone without permission, then the operating system protects against this because application A does not have the appropriate user privileges.
  17. Cross platform frameworks - RN Bridge translates Javascript VM instructions

    to ART instructions using Just In Time (JIT) compiler. - JIT works better for dynamically typed languages (such as javascript or python) than Ahead of time compilation (AOT). - All javascript frameworks rely on JIT. They can’t take advantages of AOT. - Xamarin uses both as C# can be AOT/JIT compiled. - Flutter uses Dart AOT compiler which is bundled as runtime with the app.
  18. Conclusion 1. We have just explored 1% of Android open

    source project. Other notable components are - Skia - UI Rendering Engine - Binder IPC - Power Management - Garbage Collector - ASHMEM (Android Shared Memory)
  19. Conclusion 3. Android framework is not really designed for application

    developers but instead designed for the operating system. “Should you use MVC? Or MVP? Or MVVM? I have no idea. Heck, I only know about MVC from school and had to do a Google search to find other options to put here. This may be surprising, because Android could feel like it has strong opinions on how apps should be written. With its Java language APIs and fairly high-level concepts, it can look like a typical application framework that is there to say how applications should be doing their work. But for the most part, it is not. It is probably better to call the core Android APIs a "system framework." For the most part, the platform APIs we provide are there to define how an application interacts with the operating system; but for anything going on purely within the app, these APIs are often just not relevant.” - Dianne Hackborn, Android Framework Engineer at Google. [https://plus.google.com/+DianneHackborn/posts/FXCCYxepsDU ]
  20. Conclusion 4. Android is hard. - Open-sourcing makes it vulnerable

    for security exploits, at the same time makes it robust over the time as bugs get fixed. - In closed-source systems, components can’t be seen and so bugs can’t be found by the community. At the end, software remains fragile. - Device fragmentation is still an issue. - After all, openness is hard.