Mobile Development 10 min read

RocketX: A Gradle Plugin for Incremental Android Compilation by Dynamically Replacing Modules with AARs

RocketX is an open‑source Gradle plugin that speeds up large Android projects by dynamically swapping unchanged modules with pre‑built AAR artifacts, compiling only the changed modules and the app module, thereby reducing full‑build times by over 300% while preserving project integrity.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
RocketX: A Gradle Plugin for Incremental Android Compilation by Dynamically Replacing Modules with AARs

When Android projects grow to tens of thousands of classes, full builds can take several minutes, slowing development. RocketX addresses this problem by dynamically modifying Gradle dependencies at build time: unchanged modules are replaced with their corresponding AAR files, so only the app module and the modules that actually changed are compiled.

Test results : A sample project with more than 30,000 classes and resources normally requires about 4 minutes for a full build on a 2018 MacBook Pro (i7, 16 GB). After applying RocketX, each incremental build averages around 30 seconds, achieving a speed‑up of over 300% for top‑level modules.

Core idea : Implement a Gradle plugin that, during the configuration phase, inspects the project's dependency graph, replaces implementation/api module dependencies with equivalent implementation/api AAR dependencies, and stores the AARs in a local Maven repository (or a flatDir repository for faster resolution).

Key implementation details :

Dynamic method handling is performed in DynamicAddDependencyMethods.tryInvokeMethod , which forwards calls to a DirectDependencyAdder that ultimately invokes DefaultDependencyHandler.doAdd to add the dependency.

The helper function addAarDependencyToProject(aarName, configName, project) adds an AAR to a configuration using a linked map (e.g., {"name": "libaccount-2.0.0", "ext": "aar"} ).

A flatDirs() utility creates a flatDir repository pointing to the local Maven cache, allowing fast AAR resolution.

Module change detection relies on scanning each module’s files for the latest lastModifyTime , aggregating them into a unique countTime fingerprint; a mismatch with the previous fingerprint indicates a modification.

The plugin registers a DependencyResolutionListener on the Gradle project to capture the full dependency graph and later replace module dependencies with their AAR equivalents.

When a module is unchanged, its AAR (and its child dependencies) are attached to the first‑level parent module to keep the compile graph consistent.

Hooking into the assembleDebug task via finalizedBy runs an uploadLocalMavenTask that publishes the newly generated AARs to the local Maven cache.

Additional fixes include handling duplicate JARs, preferring implementation(name: "libXXX", ext: "aar") over file‑tree inclusion, and ensuring Android modules can also produce JAR artifacts when needed.

Future work : Optimize the dex‑building task, resolve remaining compatibility issues, and continue polishing the plugin’s Maven publishing workflow. The project is open‑source on GitHub (https://github.com/trycatchx/RocketXPlugin) and welcomes contributions.

AndroidPluginbuild optimizationGradleAARCompilation SpeedModule Dependency
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

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.