ByteX
ByteX is a bytecode plugin platform based on Android Gradle Transform API and ASM. 字节码插件开发平台
Top Related Projects
Matrix is a plugin style, non-invasive APM system developed by WeChat.
A bytecode optimizer for Android apps
🚀Optimizer for mobile applications
proguard resource for Android by wechat team
ProGuard, Java optimizer and obfuscator
Quick Overview
ByteX is an open-source Android plugin platform developed by ByteDance. It aims to improve Android app performance, reduce APK size, and enhance development efficiency through various plugins that can be integrated into the Gradle build process.
Pros
- Modular architecture allows for easy integration of specific plugins based on project needs
- Provides a wide range of optimization and analysis tools for Android development
- Actively maintained by ByteDance, a major tech company with extensive Android experience
- Open-source nature allows for community contributions and customizations
Cons
- Primarily documentation and comments are in Chinese, which may be challenging for non-Chinese speakers
- Some plugins may have a learning curve or require additional configuration
- Potential compatibility issues with certain Android project setups or other Gradle plugins
- Performance impact during build time when using multiple plugins simultaneously
Code Examples
- Enabling ByteX in your project's build.gradle:
buildscript {
dependencies {
classpath "com.bytedance.android.byteX:base-plugin:${bytex_version}"
}
}
apply plugin: 'com.android.application'
apply plugin: 'bytex'
- Configuring the R-inline plugin to optimize R field access:
bytex {
enable true
enableInDebug false
r-inline {
enable true
enableInDebug false
forceRInline true
whiteList = ["com.example.myapp"]
}
}
- Using the method-call-opt plugin to optimize method calls:
bytex {
enable true
method-call-opt {
enable true
enableInDebug false
logLevel "DEBUG"
optimizeMethodCall true
optimizeMethodCallWithParams true
}
}
Getting Started
To start using ByteX in your Android project:
- Add the ByteX classpath to your project's build.gradle:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.bytedance.android.byteX:base-plugin:0.3.0"
}
}
- Apply the ByteX plugin in your app's build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'bytex'
bytex {
enable true
enableInDebug false
}
-
Add and configure specific ByteX plugins as needed, such as 'r-inline' or 'method-call-opt'.
-
Sync your project and build to see the optimizations in action.
Competitor Comparisons
Matrix is a plugin style, non-invasive APM system developed by WeChat.
Pros of Matrix
- More comprehensive performance monitoring, including memory, battery, and I/O
- Stronger focus on crash analysis and reporting
- Wider range of supported platforms, including iOS and Windows
Cons of Matrix
- Steeper learning curve due to more complex features
- Potentially higher resource overhead for extensive monitoring
- Less focus on build optimization compared to ByteX
Code Comparison
Matrix (APM initialization):
Matrix.Builder builder = new Matrix.Builder(application);
builder.patchListener(new TestPluginListener());
Matrix.init(builder.build());
ByteX (Gradle configuration):
bytex {
enable true
enableInDebug false
logLevel "DEBUG"
}
Key Differences
- Matrix is a comprehensive APM (Application Performance Management) tool, while ByteX focuses primarily on build-time optimizations
- Matrix offers more runtime monitoring capabilities, whereas ByteX excels in compile-time code analysis and optimization
- ByteX is more tightly integrated with the Android build process, while Matrix provides broader cross-platform support
Both tools serve different primary purposes: Matrix for performance monitoring and issue detection, and ByteX for code optimization and build improvement. The choice between them depends on specific project needs and development focus.
A bytecode optimizer for Android apps
Pros of Redex
- More mature project with longer development history and wider adoption
- Extensive documentation and community support
- Broader range of optimization techniques, including method inlining and class merging
Cons of Redex
- Primarily focused on Android optimization, limiting its applicability to other platforms
- Steeper learning curve due to more complex configuration options
- May require more manual intervention for optimal results
Code Comparison
ByteX example:
@ByteXPlugin(
name = "res_guard",
priority = 0
)
class ResGuardPlugin : CommonPlugin<ResGuardExtension>() {
// Plugin implementation
}
Redex example:
struct RedexOptions {
bool verify_none{false};
bool verify_metadata{false};
bool is_art_build{false};
// More options...
};
Both projects aim to optimize Android applications, but they differ in their approach and implementation. ByteX offers a more modular plugin-based system, while Redex provides a comprehensive set of optimization passes. ByteX may be easier to integrate into existing build processes, whereas Redex offers more fine-grained control over optimizations at the cost of increased complexity.
🚀Optimizer for mobile applications
Pros of Booster
- More extensive documentation and examples, making it easier for developers to get started
- Broader focus on overall app optimization, not just bytecode manipulation
- Active community with regular updates and contributions
Cons of Booster
- Less specialized in bytecode optimization compared to ByteX
- May have a steeper learning curve due to its broader feature set
- Potentially slower build times for large projects due to comprehensive optimizations
Code Comparison
ByteX example:
apply plugin: 'bytex'
bytex {
enable true
enableInDebug false
logLevel "DEBUG"
}
Booster example:
apply plugin: 'com.didiglobal.booster'
booster {
enableInDebug = false
transformers {
imageCompressor {
enable = true
}
}
}
Both projects aim to optimize Android apps, but they have different approaches. ByteX focuses primarily on bytecode manipulation, while Booster offers a more comprehensive set of optimization tools. ByteX may be more suitable for projects requiring specific bytecode optimizations, whereas Booster provides a broader range of features for overall app performance improvement.
proguard resource for Android by wechat team
Pros of AndResGuard
- Focused specifically on Android resource obfuscation and optimization
- Lightweight and easy to integrate into existing Android projects
- Provides a command-line interface for standalone usage
Cons of AndResGuard
- Limited scope compared to ByteX's comprehensive optimization features
- Less active development and community support
- May require manual configuration for complex projects
Code Comparison
AndResGuard configuration:
andResGuard {
mappingFile = file("./resource_mapping.txt")
use7zip = true
useSign = true
keepRoot = false
}
ByteX configuration:
bytex {
enable true
enableInDebug false
logLevel "DEBUG"
plugin {
resguard {
enable true
whiteList = ["R.drawable.icon"]
}
}
}
Summary
AndResGuard is a specialized tool for Android resource optimization, offering simplicity and ease of use. ByteX, on the other hand, provides a more comprehensive suite of optimization tools, including resource guarding, but with a steeper learning curve. While AndResGuard excels in its focused approach, ByteX offers broader optimization capabilities for Android projects.
ProGuard, Java optimizer and obfuscator
Pros of ProGuard
- Mature and widely adopted in the Android development community
- Extensive documentation and community support
- Offers additional features like optimization and verification
Cons of ProGuard
- Can be complex to configure, especially for large projects
- May require manual fine-tuning to avoid issues with reflection or dynamic class loading
Code Comparison
ProGuard configuration example:
-keep class com.example.MyClass { *; }
-keepclassmembers class * {
@android.webkit.JavascriptInterface <methods>;
}
ByteX configuration example:
bytex {
enable true
enableInDebug false
logLevel "DEBUG"
}
Key Differences
- ProGuard focuses on code obfuscation, optimization, and shrinking, while ByteX is a more comprehensive build optimization toolkit
- ByteX offers a modular approach with various plugins for different optimization tasks
- ProGuard is primarily used for Android projects, whereas ByteX can be applied to both Android and Java projects
Use Cases
- ProGuard: Best for projects requiring robust code obfuscation and size reduction
- ByteX: Ideal for projects needing flexible, customizable build-time optimizations beyond just obfuscation
Both tools have their strengths, and the choice depends on specific project requirements and the development team's familiarity with each tool.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
English | ç®ä½ä¸æ
ByteXï¼Infinite Possibilitiesï¼
Powered by ByteDance TikTok & Douyin Android team.
ByteX is a bytecode plugin platform based on Android Gradle Transform Api and ASM.
(Maybe you can think of it as a socket with unlimited plugs?)
In the apk building process, each plugin is completely independent. It not only can run independently from the ByteX host, but also can be automatically integrated into the host into a single Transform along with other plugins.
What's more, each plugin's code is decoupled from one another, as well as their host, which makes it extensible and highly efficient for the development of new plugins.
Background
If all features are developed as a separate plugin, each plugin will cost 10+s, and the compilation time will increase linearly.
But if the iterative development in only one plugin module will make it more and more cluttered, for the code is deeply coupled.
So an idea was raised. It could make sense to build a bytecode plugin platform, and the new feature can be extended based on it as a new plugin.
Feature
-
Code reuse. The common code sinks to
common
module and is reused by all plugins, so that each plugin only needs to focus on bytecode operation. -
Plugin code is isolated and decoupled from each other. Based on ByteX, each feature is independent as a single plugin, and the code of each plugin is isolated from each other, which is conducive to develop a new plugin.
-
Platformization makes
Transform
more efficient.-
Class files are processed in multiple threads concurrently, making full use of the CPU resources of your machine.
-
Plugins are automatically and seamlessly integrated into a single
Transform
to improve the efficiency of processing. During theTransform
process, the IO of the class file is time-consuming. Integrating all the plugins into a single transform can avoid the costing time increasing linearly. It makes "1 + 1 = 2" become "1 + 1 <2" or approximately equal to 1.
-
-
Plugin portability is outstanding. Each plugin can be separated from the ByteX host, working as a transform independently.
Structure
TransformEngine
Reading all the class files in the project and Android SDK, and writing back to the specified directory.
base-plugin
ByteX host.
common
Basic code library, including class diagram construction, logs, interfaces provided to various plugins.
other plugin modules
Depending on common
module and focusing on bytecode operation.
Quick Start
Add those configuration code to your build.gradle, and apply your plugins on demand.
buildscript {
ext.plugin_version="0.3.0"
repositories {
google()
jcenter()
maven {
url "https://artifact.bytedance.com/repository/byteX/"
}
}
dependencies {
classpath "com.bytedance.android.byteX:base-plugin:${plugin_version}"
// Add bytex plugins' dependencies on demand
classpath "com.bytedance.android.byteX:refer-check-plugin:${plugin_version}"
// ...
}
}
apply plugin: 'com.android.application'
// apply bytex host
apply plugin: 'bytex'
ByteX {
enable true
enableInDebug false
logLevel "DEBUG"
}
// apply bytex plugins on demand...
apply plugin: 'bytex.refer_check'
// ...
Note: If ByteX host is not applied, there is no difference between ByteX plugins and ordinary ones, and all of them will run separately. On the contrary, all ByteX plugins can be automatically merged into one single Transform with the help of the ByteX host.
Plugins
- access-inline-pluginï¼Inline access methodï¼
- shrink-r-pluginï¼Slimming R files&Unused resource checkï¼
- closeable-check-pluginï¼Detect opening stream without closeï¼
- const-inline-pluginï¼Inline constantsï¼
- field-assign-opt-pluginï¼Optimize redundant assignment instructionsï¼
- getter-setter-inline-plugin ï¼Inline Getters and Settersï¼
- method-call-opt-pluginï¼Delete some method invoking instructions clearly e.g.
Log.d
ï¼ - coverage-pluginï¼Online code coverageï¼
- refer-check-pluginï¼Detect if there is a invocation to a non-existent method and a reference to a non-existent fieldï¼
- serialization-check-pluginï¼Serialization checkï¼
- SourceFileKillerï¼Shrink SourceFile and linenumberï¼
- ButterKnifeCheckerï¼detect exceptions caused by cross-module use of ButterKnifeï¼
- RFileKnifeï¼Fix R.java code too largeï¼
Apps Using ByteX
æé³ | Tik Tok | ä»æ¥å¤´æ¡ |
ç«å±±å°è§é¢ | é£ä¹¦ | å¤éª |
FaceUæ¿è | è½»é¢ | é£è |
What else can the ByteX do?
There are more than 25 plugins developed based on ByteX in ByteDance (We only open sourced some of them). You can quickly develop the following related plugins based on ByteX:
- Performance optimization(SPI...)
- Optimize apk size(xxx-inline...)
- Fix Bug
- Code analysis / security scan
- AOP(replace SharedPreferences...)
- and so on
Contribution
If you have some creative ideas and demands related to bytecode, come to join us to develop a new bytecode plugin based on ByteX!
Please read through our Developer API.
Thanks
Contact us
If you have any question or advice about ByteX, feel free to join our WeChat group.
Besides, sending email to yangzhiqian@bytedance.com or tanlehua@bytedance.com is also available for you.
Change Log
License
Apache 2.0
Top Related Projects
Matrix is a plugin style, non-invasive APM system developed by WeChat.
A bytecode optimizer for Android apps
🚀Optimizer for mobile applications
proguard resource for Android by wechat team
ProGuard, Java optimizer and obfuscator
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot