Mobile Development 14 min read

Using Gradle Transform and ASM for Android Privacy API Scanning and Instrumentation

This tutorial explains how to build a custom Gradle Transform plugin, integrate ASM bytecode manipulation, and instrument Android applications to detect and report illegal privacy API calls, covering project setup, plugin implementation, ASM basics, and practical verification steps.

JD Retail Technology
JD Retail Technology
JD Retail Technology
Using Gradle Transform and ASM for Android Privacy API Scanning and Instrumentation

The article introduces the growing need for privacy compliance in Android apps and proposes a solution based on a custom Gradle Transform plugin combined with ASM bytecode instrumentation to automatically detect and report illegal privacy API usage.

It first explains Gradle Transform, an API introduced in Gradle 1.5.0 that allows developers to modify class files during the build process. A custom Transform is created by extending the abstract Transform class, implementing its core methods, and registering it via a Gradle plugin that becomes a Gradle task.

The step‑by‑step guide shows how to create a Gradle Plugin project in IntelliJ IDEA, configure the project structure, implement the plugin class (e.g.,

class HelloGradlePlugin implements Plugin<Project> { ... }

), and publish the plugin to a Maven repository using the maven‑publish plugin.

Next, the article introduces ASM, a lightweight Java bytecode manipulation framework. It outlines the main ASM packages and key classes such as ClassReader, ClassVisitor, MethodVisitor, AdviceAdapter, and ClassWriter, emphasizing their roles in reading, visiting, and generating class files.

The integration of ASM with the custom Transform is demonstrated: the transform(TransformInvocation transformInvocation) method obtains DirectoryInput and JarInput collections, iterates over all .class files, and feeds them to ASM visitors for instrumentation. Sample code snippets illustrate the retrieval of inputs:

public interface TransformInvocation { @NonNull Collection<TransformInput> getInputs(); @Nullable TransformOutputProvider getOutputProvider(); }

A concrete example instruments the android/telephony/TelephonyManager.getNetworkType() method to call PrivacyReportUtil.reportPrivacyApi(). The visitor logic checks the class and method name, then inserts bytecode using mv.visitLdcInsn() and mv.visitMethodInsn() with appropriate opcode and descriptors.

After building the plugin, the article shows how to apply it in an Android project by adding the plugin classpath:

classpath "com.jd.plugin:helloplugin:1.0.0-SNAPSHOT"

and applying the plugin in the app module: plugins { id 'hellogradleplugin' } Verification steps include running the app, inspecting the generated dex or class files, and confirming that the reporting call has been injected at the expected location.

Finally, the article discusses practical considerations such as limiting instrumentation to debug builds, integrating the scanning results with an internal SunGlasses dashboard for privacy compliance tracking, and the broader impact of automating privacy API detection on development efficiency.

The conclusion emphasizes that ASM, while powerful, has a steep learning curve, and encourages more developers to adopt it for AOP‑style bytecode manipulation to improve code quality and compliance.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

InstrumentationAndroidbytecodepluginprivacyGradleASM
JD Retail Technology
Written by

JD Retail Technology

Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.

0 followers
Reader feedback

How this landed with the community

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.