Exploring the Android Build System: A Comprehensive Guide

Exploring the Android Build System: A Comprehensive Guide

Introduction to the Android Open Source Project (AOSP)

The Android Open Source Project (AOSP) serves as the foundation for Android development. By exploring its source code, developers can better understand the inner workings of Android and customize it according to their needs.

How to Download the AOSP Source Code

To get started with AOSP, follow these steps to download the source code:

Prerequisites:

  • Install tools like Git and Python.

  • Set up the required environment for AOSP development.

Clone the Repository:

mkdir aosp
cd aosp
repo init -u https://android.googlesource.com/platform/manifest
repo sync

Structure and Organization of AOSP

Key Directories:

  • frameworks/base/core: Contains core Android classes and libraries like Activity and Context.

  • frameworks/base/services: Manages system services such as the Notification Manager.

  • device folder: Holds device-specific configurations, drivers, and customization files.


Build Commands and Tools for AOSP

Key Build Commands:

  • lunch: Selects the target build.

      lunch aosp_x86-eng
    
  • m and mm: Build the entire source or specific modules.

      m framework
      mm apps/Settings
    
  • croot: Navigate to the root of the source code.

  • outmod: Navigate to the output directory after a successful build.

  • Want to explore more frequently used AOSP commands


Device-Specific Customizations in AOSP

To tailor AOSP to specific hardware, modifications can be made in the device folder, including changes to Wi-Fi or Bluetooth drivers.


Testing Your Build on Emulators and Physical Devices

Emulators:

  1. Cuttlefish: A virtualized Android emulator for running builds without physical hardware.

     launch_cvd
    
  2. Android Emulator: Use Android Studio to set up an emulator for testing custom builds.

Physical Devices:

Connect a physical device via USB and use ADB for debugging:

adb devices
adb install myapp.apk

Making Framework-Level Changes in AOSP

Modifying Frameworks:

  • Edit files in frameworks/base/core/java to change core behavior. Example: Modify PhoneWindowManager.java to alter the back button functionality.

Adding Custom Services:

  • Create new services in frameworks/base/services. Example: A custom logging service for tracking user actions.

Practical Workflow for Working with AOSP

  1. Download AOSP: Run the repo sync command to download the source code.

  2. Select a Target: Use the lunch command to choose the target device for your build.

  3. Build the Source: Run m to compile the system.

  4. Test on Emulator or Device: Deploy the build to Cuttlefish, Emulator, or a physical device.

  5. Iterate: Make changes, rebuild specific modules, and test again.


Exploring AOSP Security: DAC, MAC, and Permissions

DAC (Discretionary Access Control):

Set access permissions for files and processes:

chmod 755 myfile
chown user:group myfile

MAC (Mandatory Access Control):

Android uses SELinux for MAC. Example commands to check SELinux status:

getenforce
setenforce 1

Android Permissions:

Define app permissions in AndroidManifest.xml, such as camera access:

<uses-permission android:name="android.permission.CAMERA" />

Manage permissions via ADB:

adb shell pm grant com.example.app android.permission.CAMERA
adb shell pm list permissions

The Android Make Build System

Android.mk

Describes how to build native code. Basic example:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := my_native_lib
LOCAL_SRC_FILES := main.cpp
include $(BUILD_SHARED_LIBRARY)

Application.mk

Used for multi-architecture builds:

APP_ABI := all
APP_PLATFORM := android-29

Working with Generic System Images (GSIs)

Building and Flashing GSIs:

  1. Build a GSI with the following command:

     lunch aosp_arm64-userdebug
     make -j4
    
  2. Flash the GSI to a device:

     adb reboot bootloader
     fastboot flash system system.img
     fastboot reboot
    
  3. Verify the GSI:

     adb shell getprop ro.build.fingerprint
    

Installation and Setup of AOSP

Create Directory:

mkdir aosp
cd aosp

Install Repo:

sudo apt-get install repo
repo init --partial-clone -b main -u https://android.googlesource.com/platform/manifest

Set Git Configuration:

git config --global user.email "your_email@example.com"
git config --global user.name "Your Name"

Sync the Repository:

repo sync

Building the Emulator:

  1. Set Up the Build Environment:

     source build/envsetup.sh
    
  2. Choose the Target Build:

     lunch aosp_x86_64-eng
    
    • For ARM: lunch aosp_arm64-eng
  3. Build the Emulator Image:

     make -j8
    

    The -j8 flag specifies the number of threads to use (adjust based on CPU cores).