# termux/android-build-tools architecture

> Architecture diagram of the GitHub repository termux/android-build-tools, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/termux/android-build-tools
- Repository: https://github.com/termux/android-build-tools

## Overview

##### Project Type and Purpose
- The project is a build system toolkit for Android tools. Its main purpose is to facilitate building key Android build tools by integrating submodules (sourced from AOSP) and applying a series of patches.
- It is not a full-stack application but a specialized build system that wraps around components like aidl (AIDL compiler), aapt, aapt2, and zipalign.

##### Main Components
a. Build System & Toolchain:
- CMake build scripts (CMakeLists.txt) as the driver for the entire building process.
- Build dependencies (C and C++ toolchain, bison, flex, ninja, pkgconf, etc.) and runtime libraries (libfmt, googletest, expat, libpng, protobuf).
b. Source Components:
- Git submodules which incorporate AOSP repositories (like “base” as referenced in the README).
- The Android build tool sources (integrated from upstream) that are patched.
c. Patching Mechanism:
- Several “patches” directories (for aidl, base, build, core, incremental_delivery, libbase, libziparchive, native). These represent modifications on top of the upstream source code.
d. Vendor and Versioning:
- The vendor directory and version.h.in file, which might be used to manage versions and third-party libraries.
e. Development and Deployment Workflow:
- .github/workflows which indicate automated builds/tests through CI (using build.yml).
- The clone.sh script, indicating the process to clone the repository with updated submodules.

##### Relationships and Data Flow
- Data Flow/Process: Start with a clone operation that recursively fetches submodules and runs clone.sh to set everything up. Then, CMake is used to generate build files (likely for ninja) which integrate the source, patches, and third-party dependencies.
- The patched components flow into the respective build targets (aidl, aapt, aapt2, and zipalign) which are then compiled into final tools.
- The CI/CD pipeline (GitHub workflows) initiates the build process and likely runs tests; this should be shown as an external process triggering builds.

##### Architectural Patterns and Technologies
- The design follows a modular approach by separating build logic (CMake), patch management, and dependency management (git submodules).
- Integration with system toolchains and the layered approach to patching upstream code.
- Incorporate external dependencies (build and runtime) as supporting libraries used by the tools.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Source Acquisition
    subgraph "Source Acquisition"
        clone["clone.sh"]:::source
        gitmods[".gitmodules"]:::source
        aosp["Git Submodules (AOSP Sources)"]:::source
    end

    %% Build Infrastructure
    subgraph "Build Infrastructure"
        cmakeBuild["CMake Build System"]:::build
        cmakeTxt["CMakeLists.txt"]:::build
    end

    %% Patch Application Module
    subgraph "Patch Application"
        patches["patches"]:::patch
        patchAidl["patches/aidl"]:::patch
        patchBase["patches/base"]:::patch
        patchBuild["patches/build"]:::patch
        patchCore["patches/core"]:::patch
        patchIncremental["patches/incremental_delivery"]:::patch
        patchLibbase["patches/libbase"]:::patch
        patchLibziparchive["patches/libziparchive"]:::patch
        patchNative["patches/native"]:::patch
    end

    %% Tool Components
    subgraph "Tool Components"
        aidl["AIDL Compiler"]:::tools
        aapt["AAPT/AAPT2"]:::tools
        zipalign["Zipalign Utility"]:::tools
    end

    %% Vendor & Versioning
    subgraph "Vendor & Versioning"
        vendor["Vendor Directory"]:::vendor
        version["version.h.in"]:::vendor
    end

    %% CI/CD Pipeline
    subgraph "CI/CD Pipeline"
        ci["GitHub Workflows"]:::ci
    end

    %% External Dependencies
    subgraph "External Dependencies"
        extDep["Build & Runtime Libraries"]:::build
    end

    %% Connections
    clone --> aosp
    gitmods --> aosp
    aosp -->|"patched"| patches
    patchAidl --> patches
    patchBase --> patches
    patchBuild --> patches
    patchCore --> patches
    patchIncremental --> patches
    patchLibbase --> patches
    patchLibziparchive --> patches
    patchNative --> patches
    patches -->|"integrated"| cmakeBuild
    cmakeTxt -->|"configures"| cmakeBuild
    extDep -->|"uses"| cmakeBuild
    vendor -->|"versions"| cmakeBuild
    version -->|"versions"| cmakeBuild
    ci -->|"triggers"| cmakeBuild
    cmakeBuild -->|"compiles"| aidl
    cmakeBuild -->|"compiles"| aapt
    cmakeBuild -->|"compiles"| zipalign

    %% Click Events
    click clone "https://github.com/termux/android-build-tools/blob/master/clone.sh"
    click cmakeTxt "https://github.com/termux/android-build-tools/blob/master/CMakeLists.txt"
    click gitmods "https://github.com/termux/android-build-tools/blob/master/.gitmodules"
    click patches "https://github.com/termux/android-build-tools/tree/master/patches"
    click patchAidl "https://github.com/termux/android-build-tools/tree/master/patches/aidl"
    click patchBase "https://github.com/termux/android-build-tools/tree/master/patches/base"
    click patchBuild "https://github.com/termux/android-build-tools/tree/master/patches/build"
    click patchCore "https://github.com/termux/android-build-tools/tree/master/patches/core"
    click patchIncremental "https://github.com/termux/android-build-tools/tree/master/patches/incremental_delivery"
    click patchLibbase "https://github.com/termux/android-build-tools/tree/master/patches/libbase"
    click patchLibziparchive "https://github.com/termux/android-build-tools/tree/master/patches/libziparchive"
    click patchNative "https://github.com/termux/android-build-tools/tree/master/patches/native"
    click vendor "https://github.com/termux/android-build-tools/tree/master/vendor"
    click version "https://github.com/termux/android-build-tools/blob/master/version.h.in"
    click ci "https://github.com/termux/android-build-tools/blob/master/.github/workflows/build.yml"

    %% Styles
    classDef source fill:#FFECB3,stroke:#333,stroke-width:1px;
    classDef build fill:#E0F7FA,stroke:#333,stroke-width:1px;
    classDef patch fill:#C8E6C9,stroke:#333,stroke-width:1px;
    classDef tools fill:#BBDEFB,stroke:#333,stroke-width:1px;
    classDef vendor fill:#D1C4E9,stroke:#333,stroke-width:1px;
    classDef ci fill:#F8BBD0,stroke:#333,stroke-width:1px;
```
