Overview of ByteHook
ByteHook is a versatile and efficient PLT hook library for Android, developed to cater to diverse architectures including armeabi-v7a, arm64-v8a, x86, and x86_64. Its functionality is crucial in enhancing the capabilities of applications by allowing dynamic function hooks. Notably, ByteHook is trusted by prominent apps like TikTok, Douyin, Toutiao, Xigua Video, and Lark.
Key Features
- Wide Android Support: Compatible with Android versions 4.1 to 14, covering API levels 16 to 34.
- Broad Architecture Coverage: Works seamlessly with four major architectures: armeabi-v7a, arm64-v8a, x86, and x86_64.
- Multiple Hooks Capability: Allows multiple hooks and unhooks on the same function without conflicts, enhancing flexibility.
- Comprehensive Library Hooking: Capable of hooking individual, multiple, or all dynamic libraries in a process.
- Automatic Hooking: Automatically hooks newly loaded dynamic libraries, facilitating ease of operations.
- Function Call Optimization: Prevents recursive and circular function calls automatically, ensuring stability.
- Traceback Support: Supports unwinding backtrace in proxy functions, which is crucial for debugging.
- Open Source License: Distributed under the MIT license, promoting openness and collaboration.
Getting Started with ByteHook
To integrate ByteHook into your Android project, follow these steps:
Include ByteHook in your Project
-
Modify build.gradle: Add ByteHook as a dependency, ensuring to enable the Prefab package feature for native dependencies.
android { buildFeatures { prefab true } } dependencies { implementation 'com.bytedance:bytehook:1.0.10' }
-
Configure with Prefab: Ensure compatibility with Prefab schema v2 by updating
gradle.properties
if necessary.android.prefabVersion=2.0.0
-
CMakeLists.txt or Android.mk Setup: Link ByteHook in your native library settings.
find_package(bytehook REQUIRED CONFIG) add_library(mylib SHARED mylib.c) target_link_libraries(mylib bytehook::bytehook)
Adjust Project Abilities and Packaging
-
Specify ABI Requirements: Configure the ABIs in your
build.gradle
to match your app needs.android { defaultConfig { ndk { abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64' } } }
-
Resolve Packaging Conflicts: Manage potential conflicts of
libbytehook.so
in the app or SDK projects.android { packagingOptions { pickFirst '**/libbytehook.so' } }
Initialize and Implement Hooking
-
Initialization: Initialize ByteHook in your Java entry class.
import com.bytedance.android.bytehook.ByteHook; public class MySdk { public static synchronized void init() { ByteHook.init(); } }
-
Hook Functions: Use ByteHook's C interface to implement hooking:
#include "bytehook.h" bytehook_stub_t bytehook_hook_single( const char *caller_path_name, const char *callee_path_name, const char *sym_name, void *new_func, bytehook_hooked_t hooked, void *hooked_arg);
These hooks allow selective or broad dynamic library linking within processes, facilitating extensive control.
Contribution and Licensing
ByteHook is developed under the MIT License, encouraging community contributions and shared enhancements. Contributors should adhere to the Code of Conduct and refer to the Contributing Guide for details on engaging with the project.