Framework Mmi
Framework Mmi
1. What is AAPT?
The Android Asset Packaging Tool (AAPT) is a command-line utility used in the Android build
system to compile, process, and package app resources. It plays a crucial role in converting raw
assets and XML-based resources into a format that the Android system can understand and use at
runtime.
AAPT is part of the Android SDK (build-tools) and is automatically used by the Android
Gradle Plugin during the build process.
a) Compiling Resources
• It compiles XML resource files (.xml), such as:
Example Output:
c) Compiling Resources
e) Extracting AndroidManifest.xml
4. AAPT vs AAPT2
Google introduced AAPT2 (AAPT version 2) to improve efficiency in the Android build process.
<resources
xmlns:android="http://schemas.android.com/apk/res/android">
<color name="primary_color">#FF0000</color> <!-- Overrides the
original color -->
</resources>
• Set AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.overlay">
<overlay android:targetPackage="com.example.targetapp"
android:priority="1"
android:isStatic="true" />
</manifest>
Android Architecture
1. Linux Kernel Layer
• Android is built on Linux Kernel, which provides:
• Process Management (Scheduling, Threading)
• Memory Management
• Security (SELinux, Permissions)
• Driver Support (Display, Camera, Audio, Bluetooth, Wi-Fi)
• Power Management (Wake Locks, Battery Management)
• Networking (TCP/IP, VPN, Wi-Fi, Mobile Data)
• Important Kernel Subsystems:
• Binder IPC Mechanism (for inter-process communication)
• Low Memory Killer (LMK) (for handling OOM scenarios)
A. Native Libraries
• Written in C/C++, these libraries provide core features like:
• SurfaceFlinger (Graphics rendering using OpenGL)
• Media Framework (Playback and Recording)
• WebKit (Web rendering)
• SQLite (Database)
• Libc (Standard C library)
• SSL (Security libraries)
4. Framework Layer
The Framework Layer in Android acts as a bridge between the Android Runtime and the Native
System components. It includes:
1. Java APIs – Provide interfaces for app developers to interact with system services (e.g.,
ActivityManager, WindowManager, PowerManager).
2. JNI (Java Native Interface) – Allows Java code to communicate with native C/C++ libraries
(used for performance-critical operations and hardware interaction).
3. Core Platform Services (System Services) – Essential services running in SystemServer that
manage app lifecycle, power, input, display, telephony, etc.
5. Application Layer
• Topmost layer, contains:
• System Apps: Phone, Messages, Settings, Contacts.
• User Apps: Installed by users from Play Store.
• OEM specific Apps: Pre-installed, provided by the vendors.
• These apps interact with Framework APIs to access system resources.
Example:
When you power on a device, Boot ROM ensures that the bootloader is authentic before
proceeding.
Example:
Holding Volume Down + Power boots into Fastboot Mode, where firmware can be flashed via
fastboot commands.
Example:
If the kernel fails to initialize correctly, the device may get stuck in a boot loop or show a kernel
panic error.
2.5 Zygote, System Server & User Interface (Fifth Stage) - Starting Android
Framework
What is Zygote?
• Zygote is the first Dalvik/ART VM process, responsible for launching app processes.
• Every app is forked from Zygote, reducing memory usage and improving performance.
Building an android syatem is a very complex process that involves compiling and linking of
thousands of files.
Make is the build automation tool that is used to build android systems. Make uses a file make file
to the build process. Makefile specifies how to build the target system, its dependencies and the
required commands.
include $(CLEAR_VARS)
LOCAL_MODULE := libexample
LOCAL_SRC_FILES := example.cpp
LOCAL_CFLAGS := -Wall
LOCAL_SHARED_LIBRARIES := liblog libc
LOCAL_STATIC_LIBRARIES := libbase
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_MODULE_TAGS := optional
include $(BUILD_SHARED_LIBRARY)
The soong build system parces through the .bp files and creates a single build.ninja file that contains
all build instructions for building the target system. This build.ninja file is given to ninja.
Ninja i s build execution tool that is used to process build instructions for building a system. It
compiles the source code, links the dependencies and generates the binaries.
cc_library_shared {
name: "libexample",
srcs: ["example.cpp"],
cflags: ["-Wall"],
shared_libs: ["liblog", "libc"],
static_libs: ["libbase"],
include_dirs: ["include"],
host_supported: true,
}
What is Logcat?
Logcat is a command-line tool that collects and displays logs from an Android device or emulator. It
provides insights into:
• Application behavior
• System processes and services
• Kernel logs
• Hardware-related messages (e.g., radio, Wi-Fi, Bluetooth)
2. Issue Analysis
Fixing ANRs
• Use AsyncTask, Handlers, Executors for background tasks.
• Optimize database queries (use Room, pagination).
• Minimize work in BroadcastReceivers.
• Use WorkManager for background tasks.
This provides details about the crash, including signal type and fault address.
ps | grep <package_name>
This finds the process running the crashed code.
break <function_name>
bt
info registers
x/10x <fault_address>
Look for:
E Watchdog: SystemServer is not responding, rebooting...
Key Components
Component Description
Messenger A wrapper around Handler that facilitates IPC.
Handler Processes messages in the service thread.
Message Encapsulates data sent between processes.
MessageQueue Stores messages and delivers them sequentially.
Scenario:
Suppose we are building a music player app, where a client (Activity) sends commands (play,
pause, stop) to a background service that plays music. The service should process requests one at a
time and respond accordingly.
Component Description
Client (Caller Process) The process that wants to communicate with another process.
Service (Callee Process) The process providing the functionality (e.g., Location Service).
Binder The underlying mechanism that enables IPC.
IBinder An interface that facilitates communication between client and service.
ServiceManager Manages system services and their registration.
What is AIDL?
AIDL (Android Interface Definition Language) is a mechanism in Android used for Inter-Process
Communication (IPC). It allows processes running in different memory spaces to communicate by
defining an interface that clients and services can use to send data across processes.
In simpler terms, if one Android app (or a service running in the background) needs to send data to
another process, AIDL provides a structured way to do this.
AIDL (Android Interface Definition Language) is required when different applications or processes
need to communicate efficiently in real-time. Since Android runs apps in separate processes for
security and stability, direct method calls don’t work across processes. AIDL helps by enabling fast,
structured, two-way communication between different processes using IPC (Inter-Process
Communication).
AIDL Workflow
Unlike services that use AIDL-generated interfaces (Stub.asInterface()), PackageManager does not
directly implement Stub.asInterface() because it follows a different approach using Binder
proxies and delegates.
Inside ActivityManager.java:
java
CopyEdit
private static final IActivityManager getService() {
return IActivityManagerSingleton.get();
}
Role:
Manages activity lifecycle (start, stop, destroy).
Handles app switching and background processes.
Manages process crashes and ANRs (Application Not Responding).
How AMS Works?
Whenever you start an app, ActivityManagerService creates a new process for it.
It tracks all running activities and maintains their state.
frameworks/base/core/java/com/example/customservice/ICustomService
.aidl
package com.example.customservice;
frameworks/base/services/core/java/com/example/customservice/Custo
mService.java
java
package com.example.customservice;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Slog;
import com.android.server.SystemService;
@Override
public int getValue() {
return storedValue;
}
};
}
<permission android:name="com.example.customservice.PERMISSION"
android:protectionLevel="signature" />
Keyword Meaning
service my_service Defines a service named my_service that runs
/system/bin/my_service /system/bin/my_service.
Assigns the service to the main class (ensuring it can
class main
be started).
Runs the service as the root user (needed for
user root
privileged operations).
group root Assigns the process to the root group.
oneshot Ensures the service runs only once and doesn’t restart
automatically.
seclabel Sets the SELinux security label (required for security
Keyword Meaning
u:r:my_service_label:s0 enforcement).
SELinux in Android
Security-Enhanced Linux (SELinux) is a mandatory access control (MAC) mechanism integrated
into the Linux kernel. In Android, SELinux enforces security policies that restrict how processes
interact with each other and with system resources.
1. What is SELinux?
SELinux is a security feature in Linux that controls access to files, processes, and system resources
based on security policies.
Enforces strict security policies that prevent unauthorized access.
Works in two modes: Permissive (logs violations) and Enforcing (blocks violations).
📍 Example:
sh
u:r:system_server:s0
Field Meaning Example
User SELinux user u (unprivileged), r (root)
Role SELinux role system_r (system process)
Type Defines what the process/file can do system_server
MLS Level Multi-Level Security (MLS) s0 (default)
1. Product Makefile
What is a Product Makefile?
A Product Makefile (<product_name>.mk) defines the components and configuration for a
particular Android build product. It specifies:
• Applications to be included
• System properties
• Build flags
• Libraries and binaries to include
• Partition layout configurations
Where is it Located?
• Located in the device/ directory or under vendor/ if it's for a specific vendor.
include $(CLEAR_VARS)
LOCAL_MODULE := MySpecialApp
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_PACKAGE_NAME := MySpecialApp
include $(BUILD_PACKAGE)
1. BoardConfig.mk
This file defines board-specific configurations. The primary focus here is to define the CPU
architecture and WLAN settings for the emulator.
2. device.mk
Located at: target/board/sasken_phone/device.mk
This file includes product-specific device configurations and ensures compatibility with the
goldfish emulator.
3. system_ext.prop
Located at: target/board/sasken_phone/system_ext.prop
This file contains additional system properties for the Sasken phone product.
4. sasken_phone.mk
Located at: target/product/sasken_phone.mk
This is the main Product Makefile defining the Sasken phone product. It inherits common product
configurations and specifies the build details for the sasken_phone product.
Important Points
• Inherits configurations from various system images:
• core_64_bit.mk
• generic_system.mk
• handheld_system_ext.mk
• telephony_system_ext.mk
• aosp_product.mk
• Emulator-specific configurations like emulator_vendor.mk and
emulator_x86_64/device.mk.
5. Changes in AndroidProducts.mk
Located at: target/product/AndroidProducts.mk
Important Points
• Adds $(LOCAL_DIR)/sasken_phone.mk to the list of available product makefiles.
Git Commands
SETUP
TEMPORARY COMMITS
Temporarily store modified, tracked files to change branches.
• git stash
Save modified and staged changes.
• git stash list
List stack-order of stashed file changes.
• git stash pop
Apply changes from the top of stash stack.
• git stash drop
Discard the changes from the top of stash stack.
REWRITE HISTORY
Rewriting branches, updating commits, and clearing history.
• git rebase [branch]
Apply any commits of the current branch ahead of the specified one.
• git reset --hard [commit]
Clear staging area, rewrite working tree from the specified commit.
1. ls
Description: Lists files and directories in the current directory.
Example: ls
2. pwd
Description: Prints the current working directory.
Example: pwd
3. cd
Description: Changes the current directory.
Example: cd /home/user/Downloads
4. cp
Description: Copies files or directories.
Example: cp file1.txt file2.txt
5. rm
Description: Removes files or directories.
Example: rm file1.txt
6. mv
Description: Moves or renames files or directories.
Example: mv oldfile.txt newfile.txt
7. mkdir
Description: Creates a new directory.
Example: mkdir newdir
8. man
Description: Displays the manual or help documentation for a command.
Example: man ls
9. touch
Description: Creates an empty file or updates the timestamp of an existing file.
Example: touch newfile.txt
10. chmod
Description: Changes file permissions.
Example: chmod u+rwx script.sh
11. sudo
Description: Executes a command as a superuser (administrator).
Example: sudo apt-get install vim
12. exit
Description: Exits the current shell session.
Example: exit
13. shutdown
Description: Shuts down or reboots the system.
Example: sudo shutdown now
14. top
Description: Displays running processes and system resource usage.
Example: top
16. grep
Description: Searches for patterns in files.
Example: grep "error" logfile.txt
17. zip/unzip
Description: Compresses or extracts compressed files.
Example: zip -r archive.zip myfolder
unzip archive.zip
18. echo
Description: Displays a line of text or a variable value.
Example: echo "Hello World"
19. cat
Description: Displays the contents of a file.
Example: cat file.txt
20. ps
Description: Displays information about running processes.
Example: ps aux
21. kill
Description: Sends a signal to a process, often used to terminate it.
Example: kill 1234
23. history
Description: Displays a list of previously executed commands.
Example: history 10
24. which
Description: Displays the path of a command.
Example: which lunch
25. whereis
Description: Locates the binary, source, and manual pages for a command.
Example: whereis gcc
26. type
Description: Displays information about a command (whether it is a built-in shell command, alias,
or external command).
Example: type ls
27. more/less
Description: Views file contents one page at a time.
Example: more largefile.txt
less largefile.txt
28. head/tail
Description: Displays the beginning or the end of a file.
Example: head file.txt
tail file.txt
29. id/whoami
Description: Displays the user ID or current logged-in user.
Example: id
whoami
30. wc
Description: Counts lines, words, and characters in a file.
Example: wc -l file.txt
31. uname
Description: Displays system information like kernel version and machine hardware.
Example: uname -r
32. find
Description: Searches for files and directories.
Example: find . -name "*.txt"
35. alias
Description: Creates a shortcut or alias for a command.
Example: alias ll="ls -l"
36. date
Description: Displays or sets the system date and time.
Example: date
37. insmod
Description: Inserts a module into the Linux kernel.
Example: sudo insmod module.ko
38. rmmod
Description: Removes a module from the Linux kernel.
Example: sudo rmmod module
39. lsmod
Description: Displays the list of loaded kernel modules.
Example: lsmod
40. gcc/make
Description: Compiler for C programs and builds executables.
Example: gcc hello.c -o hello
make