PrivacySentry operates using a two-stage Gradle Transform architecture during the compilation process to intercept and redirect sensitive method calls to privacy proxies.
Stage 1: Pre-Transform (Collection)
In this stage, the plugin scans all .class and .jar files to collect interception rules.
- MethodProxyCollectTransform: Scans classes annotated with
@PrivacyClassProxy. It looks for methods within those classes annotated with @PrivacyMethodProxy. - It extracts metadata such as the
originalClass, originalMethod, and originalOpcode from the annotations. - The collected rules are stored in the
HookMethodManager.
Stage 2: Transform (Execution/Replacement)
In this stage, the plugin performs the actual bytecode manipulation.
- MethodHookTransform: Iterates through all class files and searches for method calls (
MethodInsnNode) that match the rules collected in Stage 1. - When a match is found, the instruction is modified: the
opcode is changed to INVOKESTATIC, and the owner, name, and desc are updated to point to the privacy proxy class and method. - Reflection Hooking: If
hookReflex is enabled, the plugin also scans for LdcInsnNode (loading constants) to intercept reflection-based calls.
Output
The process results in:
- Modified
.class files where sensitive calls are redirected. - A
privacy_hook.json file containing the generated hook configurations.
编译期流程:
┌──────────────────────────────────┐
│ 输入:所有 .class 文件和 Jar │
└──────┬───────────────────────────┘
│
┌──────▼─────────────┐
│ 第一阶段(预处理) │
│ Pre-Transform │
└──────┬─────────────┘
│
┌──┴──────────────┐
│ │
┌───▼────────────────┐ ┌───▼──────────────────┐
│MethodProxy │ │ ClassProxy │
│ CollectTransform │ │ CollectTransform │
│ (收集方法拦截规则) │ │ (收集类替换规则) │
└───┬────────────────┘ └───┬──────────────────┘
│ │
│ 结果写入: │
│ HookMethodManager │ ReplaceClassManager
│ │
┌──────▼────────────────┐
│ 第二阶段(执行替换) │
│ Transform │
└──────┬─────────────┘
│
┌──────────┼──────────────┬──────────────┐
│ │ │ │
┌───▼───┐ ┌───▼───┐ ┌───────▼──┐ ┌────────▼───┐
│Method │ │Field │ │ Class │ │ Service │
│Hook │ │Proxy │ │ Proxy │ │ Hook │
│ │ │ │ │ │ │ │
└───┬───┘ └───┬───┘ └───────┬──┘ └────────┬───┘
│ │ │ │
└─────────┴──────────────┴──────────────┘
│
┌──────▼──────────┐
│ FlushHookData │
│ Transform │
│ (生成hook配置) │
└────────┬────────┘
│
┌────────▼──────────────┐
│ 输出:修改后的 .class 和 │
│ privacy_hook.json │
└───────────────────────┘