# bytedance/bytex architecture

> Architecture diagram of the GitHub repository bytedance/bytex, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bytedance/bytex
- Repository: https://github.com/bytedance/bytex

## Overview

##### Project Type and Purpose
- ByteX is an open-source plugin platform for Android bytecode transformation, built on the Android Gradle Transform API and ASM.
- It provides a host Gradle plugin (base-plugin) and a collection of independent feature plugins that are automatically merged into a single Transform to optimize build performance and maintain a decoupled architecture.

##### Top-Level Modules and Their Roles
- gradle (build scripts and publishing)
- common (shared abstractions, logging, graph builders, hook injection, configuration)
- TransformEngine (orchestrates reading inputs, dispatching to plugins, writing outputs, handles threading and caching)
- base-plugin (ByteX host; integrates with Gradle, loads plugins, wires TransformEngine)
- plugin modules (access-inline-plugin, shrink-r-plugin, closeable-check-plugin, const-inline-plugin, field-assign-opt-plugin, getter-setter-inline-plugin, method-call-opt-plugin, refer-check-plugin, serialization-check-plugin, SourceFileKiller, HookProguard, PluginConfig, GradleToolKit, coverage-plugin, etc.)
- example (Android sample app showing how to apply ByteX host and plugins)

##### Core Architectural Patterns
- Plugin Architecture: each feature lives in its own module, depends on common and registers via meta-inf.
- Pipeline / Chain of Responsibility: TransformEngine builds a ProcessorChain; each processor or plugin visitor handles one pass.
- Concurrency: TransformEngine schedules file traversal and transformation tasks on a thread pool.
- Extension Mechanism: Gradle plugin extension objects expose plugin configuration to build scripts.

##### Main Components and Their Interactions (to include
A. Gradle Build System
- build.gradle (settings.gradle, classpath entries)
- Applies “bytex” plugin (base-plugin) and feature plugins via `apply plugin: 'bytex.xxx'`
B. ByteX Host (base-plugin)
- Registers a single Android Transform
- Scans META-INF/gradle-plugins for available plugins
- Reads each plugin’s Extension and ByteXPlugin implementation
- Passes collected plugin instances to TransformEngine
C. TransformEngine
- Input Reader: locates class/jar inputs (project + Android SDK)
- ProcessorChain: for each class/jar, invokes common processors and plugin-provided visitors
- Thread Pool: executes FileTraverseTask, FileTransformTask, FileCacheWriter in parallel
- Caching Layer: DirCache, JarCache, FileCache, NewFileCache to avoid redundant work
- Output Writer: writes transformed classes/jars back to Gradle’s output folder
D. Common Library
- Interfaces: IPlugin, ITransformPipeline, Processor, FlowListener, BuildListener
- Utilities: logging (ILogger, HtmlLogger, FileLogger), graph construction/caching, reflection utils, XML reader, manifest analyzer
- Flow Management: TransformFlow, LifecycleAwareTransformFlow, MainTransformFlow
E. Feature Plugins
- Extension class (e.g. AccessInlineExtension) for Gradle DSL
- Plugin entry (e.g. AccessInlinePlugin) implementing IPlugin, registering visitors/processors
- Context class holding per-plugin state
- ASM ClassVisitor implementations under visitor/…
F. Example App
- Demonstrates applying ByteX host + a subset of feature plugins
- Regular Android application inputs flow through the single merged Transform

##### Architectural Highlights to Annotate
- Plugin isolation: each plugin module compiles and runs independently
- Single merged Transform: avoids linear build-time growth
- Caching: annotate cache sub-components

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Gradle Build System"
        G1["settings.gradle"]:::gradle
        G2["build.gradle"]:::gradle
        G3["gradle/"]:::gradle
        G4["gradlew / gradlew.bat"]:::gradle
    end

    subgraph "ByteX Host"
        Host["base-plugin"]:::host
    end

    subgraph "Transform Engine"
        IR["Input Reader"]:::engine
        PC["ProcessorChain"]:::engine
        TP["Thread Pool"]:::engine
        CL["Caching Layer"]:::engine
        OW["Output Writer"]:::engine
    end

    subgraph "Common Library"
        Common["common/"]:::common
    end

    subgraph "Feature Plugins"
        SP1["access-inline-plugin"]:::plugin
        SP2["shrink-r-plugin"]:::plugin
        SP3["closeable-check-plugin"]:::plugin
        SP4["const-inline-plugin"]:::plugin
        SP5["field-assign-opt-plugin"]:::plugin
        SP6["getter-setter-inline-plugin"]:::plugin
        SP7["method-call-opt-plugin"]:::plugin
        SP8["refer-check-plugin"]:::plugin
        SP9["serialization-check-plugin"]:::plugin
        SP10["SourceFileKiller"]:::plugin
        SP11["HookProguard"]:::plugin
        SP12["PluginConfigAnnotation"]:::plugin
        SP13["PluginConfigProcessor"]:::plugin
        SP14["GradleToolKit"]:::plugin
        SP15["coverage-plugin"]:::plugin
        SP16["butterknife-check-plugin"]:::plugin
    end

    Example["example/"]:::example

    AndroidPipeline["Android Build Pipeline (dex → packaging → install)"]:::external
    ExternalASM["ASM"]:::external
    ExternalSDK["Android SDK"]:::external
    ExternalJDK["JDK"]:::external

    %% Connections
    G2 -->|"apply bytex plugin"| Host
    Host -->|"invoke Transform with plugins"| IR
    IR --> PC
    PC --> TP
    TP --> CL
    CL --> OW
    OW -->|"outputs class files/jars"| AndroidPipeline
    SP1 -->|"register visitor"| PC
    SP2 -->|"register visitor"| PC
    SP3 -->|"register visitor"| PC
    SP4 -->|"register visitor"| PC
    SP5 -->|"register visitor"| PC
    SP6 -->|"register visitor"| PC
    SP7 -->|"register visitor"| PC
    SP8 -->|"register visitor"| PC
    SP9 -->|"register visitor"| PC
    SP10 -->|"register visitor"| PC
    SP11 -->|"register visitor"| PC
    SP12 -->|"provide config API"| PC
    SP13 -->|"process annotations"| PC
    SP14 -->|"gradle utilities"| Host
    SP15 -->|"register visitor"| PC
    SP16 -->|"register visitor"| PC

    %% Dotted dependencies
    Common -.-> Host
    Common -.-> IR
    Common -.-> PC
    Common -.-> SP1
    Common -.-> SP2
    Common -.-> SP3
    Common -.-> SP4
    Common -.-> SP5
    Common -.-> SP6
    Common -.-> SP7
    Common -.-> SP8
    Common -.-> SP9
    Common -.-> SP10
    Common -.-> SP11
    Common -.-> SP12
    Common -.-> SP13
    Common -.-> SP14
    Common -.-> SP15
    Common -.-> SP16

    ExternalASM --> IR
    ExternalSDK --> IR
    ExternalJDK --> IR

    %% Click Events
    click G1 "https://github.com/bytedance/bytex/blob/master/settings.gradle"
    click G2 "https://github.com/bytedance/bytex/blob/master/build.gradle"
    click G3 "https://github.com/bytedance/bytex/tree/master/gradle/"
    click G4 "https://github.com/bytedance/bytex/tree/master/gradlew"
    click Common "https://github.com/bytedance/bytex/tree/master/common/"
    click Host "https://github.com/bytedance/bytex/tree/master/base-plugin/"
    click IR "https://github.com/bytedance/bytex/tree/master/TransformEngine/"
    click PC "https://github.com/bytedance/bytex/tree/master/TransformEngine/"
    click TP "https://github.com/bytedance/bytex/tree/master/TransformEngine/"
    click CL "https://github.com/bytedance/bytex/tree/master/TransformEngine/"
    click OW "https://github.com/bytedance/bytex/tree/master/TransformEngine/"
    click SP1 "https://github.com/bytedance/bytex/tree/master/access-inline-plugin/"
    click SP2 "https://github.com/bytedance/bytex/tree/master/shrink-r-plugin/"
    click SP3 "https://github.com/bytedance/bytex/tree/master/closeable-check-plugin/"
    click SP4 "https://github.com/bytedance/bytex/tree/master/const-inline-plugin/"
    click SP5 "https://github.com/bytedance/bytex/tree/master/field-assign-opt-plugin/"
    click SP6 "https://github.com/bytedance/bytex/tree/master/getter-setter-inline-plugin/"
    click SP7 "https://github.com/bytedance/bytex/tree/master/method-call-opt-plugin/"
    click SP8 "https://github.com/bytedance/bytex/tree/master/refer-check-plugin/"
    click SP9 "https://github.com/bytedance/bytex/tree/master/serialization-check-plugin/"
    click SP10 "https://github.com/bytedance/bytex/tree/master/SourceFileKiller/"
    click SP11 "https://github.com/bytedance/bytex/tree/master/HookProguard/"
    click SP12 "https://github.com/bytedance/bytex/tree/master/PluginConfig/PluginConfigAnnotation/"
    click SP13 "https://github.com/bytedance/bytex/tree/master/PluginConfig/PluginConfigProcessor/"
    click SP14 "https://github.com/bytedance/bytex/tree/master/GradleToolKit/"
    click SP15 "https://github.com/bytedance/bytex/tree/master/coverage/coverage-plugin/"
    click SP16 "https://github.com/bytedance/bytex/tree/master/butterknife-check-plugin/"
    click Example "https://github.com/bytedance/bytex/tree/master/example/"

    %% Styles
    classDef gradle fill:#E0BBE4,stroke:#957DAD,color:#000
    classDef host fill:#AEC6CF,stroke:#779ECB,color:#000
    classDef engine fill:#B5E7A0,stroke:#6B8E23,color:#000
    classDef common fill:#D3D3D3,stroke:#A9A9A9,color:#000
    classDef plugin fill:#FFD1DC,stroke:#FF69B4,color:#000
    classDef example fill:#FFDAC1,stroke:#E88, color:#000
    classDef external fill:#F0E68C,stroke:#DAA520,color:#000
```
