Information Security 14 min read

Android Privacy Protection: Privacy API and Permission HOOK Implementation Practice

To meet new Chinese privacy regulations, the article presents an mPaaS‑based solution that uses compile‑time Java HOOKs to replace privacy‑sensitive API calls and dangerous permission requests, automatically detecting and logging violations during build and runtime, and providing backend alerts and management tools for compliance.

Youzan Coder
Youzan Coder
Youzan Coder
Android Privacy Protection: Privacy API and Permission HOOK Implementation Practice

In recent years, the Ministry of Industry and Information Technology has continuously issued regulations on illegal collection of personal information by apps. Major app vendors have started to rectify these issues, but they face limitations in proactively discovering compliance problems. This article introduces a privacy constraint solution using HOOK technology to actively detect and prevent privacy violations.

Personal Information Identification Methods:

1. Not publicly disclosing collection and usage rules - apps must display privacy policy through pop-ups during first launch.

2. Not explicitly stating the purpose, manner, and scope of personal information collection.

3. Collecting personal information without user consent or continuing to collect after user refusal.

4. Collecting personal information unrelated to the provided services.

Architecture Design:

The solution uses mPaaS as the backend platform to manage privacy constraints. During app compilation, custom Transform is registered to perform bytecode proxy replacement for privacy APIs. The system captures non-compliant issues during development and runtime, stores them in the backend, and provides alert mechanisms.

HOOK Technology Selection:

Since privacy APIs and permissions are at the Java layer, Java-level proxy is sufficient. Considering reduced business perception and maintenance costs, compile-time HOOK was chosen over runtime HOOK.

Privacy API HOOK Implementation:

Privacy APIs involve DeviceId, IMEI, Mac address, WiFi, base station positioning, GPS, etc. The example shows TelephonyManager getImei proxy replacement. Custom annotations (@ASMDelegate) are used to mark classes and methods that need HOOK. During compilation, bytecode is redirected from the original API to the delegate class.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
public @interface ASMDelegate {
    Class originClass();
    String originMethod() default "";
    MethodOpcodeEnum originMethodOpcode() default MethodOpcodeEnum.INVOKESTATIC;
}

Dangerous Permission HOOK:

Dangerous permissions include CALENDAR, CAMERA, CONTACTS, LOCATION, MICROPHONE, STORAGE, etc. The solution hooks Activity and Fragment's requestPermissions method to control permission requests. To avoid infinite loops when business activities override the parent method, reflection is used to directly call the super method.

private static <T> T invokeSuperMethod(final Object obj, final String name, final Class[] types, final Object[] args) {
    final Method method = getMethod(obj.getClass().getSuperclass(), name, types);
    if (null != method) {
        method.setAccessible(true);
        return (T) method.invoke(obj, args);
    }
    return null;
}

Privacy Constraint Platform Features:

The platform includes configuration management (app management, privacy API control, permission control, scenario management) and component detection (permission management, dependency library management, version comparison). The system can detect issues like SDKs calling AndroidId before privacy consent, deviceId frequency exceeding limits, and undeclared permissions.

Androidprivacy protectioncomplianceHookPermission ManagementASMBytecode ManipulationmPaaS
Youzan Coder
Written by

Youzan Coder

Official Youzan tech channel, delivering technical insights and occasional daily updates from the Youzan tech team.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.