How bytedance/bytex works
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