Implementing Full Data Collection on Android Using Gradle Plugin, Transform API, and ASM
This article explains how to build a full‑tracking solution for Android by creating a Gradle plugin that registers a Transform, leveraging the Transform API and ASM bytecode instrumentation to automatically inject analytics code into click events and lifecycle callbacks, while covering the underlying concepts, implementation steps, and deployment process.
This article provides a comprehensive guide to implementing full data collection (full‑tracking) on Android using a custom Gradle plugin, the Transform API, and ASM bytecode instrumentation.
Full‑tracking concept : Full‑tracking (also called no‑code or automatic tracking) captures almost all user interactions without requiring developers to write explicit event‑logging code, though it generates large volumes of data.
Tracking classification : Three main approaches are discussed – code‑based tracking, visual tracking, and full‑tracking – with full‑tracking positioned between the other two in terms of complexity and coverage.
Implementation scheme : The solution adopts a static approach (Transform + ASM) because it operates during compilation, offering higher efficiency and better compatibility than dynamic methods.
Gradle plugin creation :
Create a new module named plugin and keep only the build.gradle and src directories.
Add required dependencies in build.gradle (Groovy DSL).
Write the plugin class AHAnalyticsPlugin.groovy under com.autohome.analytics.android.plugin .
Expose an extension autohomeAnalytics for configuration in the host app.
Define the plugin name in resources/META-INF/gradle-plugins/com.autohome.analytics.android.properties .
Publish the plugin to a remote or local Maven repository.
Using the plugin : Add the plugin dependency in the host project, then apply it in the app module’s build.gradle with apply plugin: "com.autohome.analytics.android" . Configuration options such as debug and disablePlug control its behavior.
Transform overview :
Register a custom AHAnalyticsTransform via registerTransform() on the Android Gradle AppExtension .
Implement required abstract methods: getName() , getInputTypes() , getScopes() , isIncremental() , and the core transform() method.
The transform processes both JarInput and DirectoryInput , handling added, changed, and removed files according to incremental settings.
ASM bytecode instrumentation :
ASM (a low‑level Java bytecode manipulation library) provides ClassReader , ClassVisitor , and ClassWriter for reading, modifying, and writing class files.
Core API (high performance) is used for the full‑tracking implementation.
Key classes: ClassReader reads the original bytecode, ClassVisitor traverses and modifies structures, and ClassWriter outputs the transformed bytecode.
Method-level injection is performed via a custom MethodVisitor (often extending AdviceAdapter ) that overrides onMethodExit() to insert logging statements before method return.
Example : The article shows how to inject a log statement into the onClick(View v) method of any class implementing View.OnClickListener using a custom AsmDemoClassVisitor and AsmDemoMethodVisitor . The injected code is Log.e("AsmDemo", var1.toString()); , demonstrating successful bytecode modification.
Summary : By combining a Gradle plugin, Transform API, and ASM, developers can achieve automatic, comprehensive data collection on Android without manual instrumentation, reducing development effort while providing rich analytics data.
HomeTech
HomeTech tech sharing
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.