跪拜 Guibai
← All articles
Android · Gradle · App

Android Build Acceleration: From Gradle Tuning to AAR and Remote Cache, Plus a Tool That Bypasses Gradle

By Gemini001 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

A developer stuck with multi-minute builds loses hours every week and abandons iterative debugging. The concrete tuning here—KSP over KAPT, locked Debug version codes, remote cache, and AAR for stable modules—directly cuts that wait without rewriting the project, and the jugg plugin shows an escape hatch for the worst monoliths.

Summary

Android builds waste minutes in three stages: initialization and configuration, compilation and resource processing, and packaging and deployment. The fix isn't copying a few lines of config; it's locating the slow step first, then applying the matching optimization.

Four acceleration categories span the cost spectrum. Zero-cost Gradle settings like parallelism, configuration cache, and KSP over KAPT help every project. Medium and large teams benefit from remote build caches and AAR-ifying stable base libraries, so a full build runs once on CI and everyone downloads the outputs. Architecture-level fixes—using `implementation` over `api`, non-transitive R classes, and shell apps—cut the cascade of recompilations in mature codebases. For daily debugging, IDE-native Apply Changes and cross-platform hot reload skip full repackaging.

For million-line monoliths, Tencent Music's jugg plugin goes further: it reuses the last complete Gradle build as a baseline, compiles only changed files with javac/kotlinc, uses a custom aapt2 for incremental resource linking, and applies changes via JVMTI and reflection—typically within 3 seconds, no build script changes required.

Takeaways
— A Gradle build's time splits into initialization/configuration, code and resource compilation, and packaging/deployment; each acceleration tactic targets one of these.
— Enabling org.gradle.parallel, configuration-cache, and a 4GB JVM heap with parallel GC reduces GC pauses and serial waits in all projects.
— Replacing KAPT with KSP eliminates the Java stub generation step, which is a major compile-time win for Kotlin codebases.
— Locking versionCode, versionName, and BuildConfig timestamps during Debug builds prevents frequent manifest and resource re-merges.
— Restricting Debug builds to a single ABI (e.g., arm64-v8a) skips compilation and packaging of other CPU architectures.
— A remote build cache lets CI run a full build once and upload task outputs by hash; developers fetch the cached artifacts instead of recompiling.
— Remote cache fails if task inputs contain unstable values like timestamps or random numbers, so stable Debug configs are a prerequisite.
— Switching stable base modules from source dependencies to pre-compiled AAR binaries skips their entire compile chain locally, at the cost of requiring repackaging for changes.
— Using implementation instead of api prevents ABI changes in low-level modules from propagating to all upstream dependents, narrowing incremental builds.
— Non-transitive R classes avoid generating a bloated R.jar per module, reducing symbol resolution and bytecode merge time.
— Android Studio's Apply Changes and Live Edit use JVMTI to swap method bodies or refresh Compose nodes, but structural class changes still trigger a restart.
— The jugg plugin from Tencent Music reuses the last full Gradle build, compiles changed files directly with javac/kotlinc, and uses a custom aapt2 plus JVMTI hot reload to apply changes in under 3 seconds.
— The author recommends small projects use only the Gradle/JVM tuning plus native Apply Changes, while medium-large teams need remote cache, AAR, non-transitive R, and shell apps.
Conclusions

The article's emphasis on locking Debug versions and timestamps is not a minor optimization; it's the condition that makes remote build cache viable. Without stable task inputs, hashing breaks and the cache is useless.

The four acceleration tiers are cumulative, not independent. A small project can stop at Gradle flags, but a large team only gets full benefit when it combines remote cache, AAR binaryization, and dependency governance.

Bypassing Gradle entirely, as jugg does, signals that Gradle's task graph itself is a bottleneck for incremental compilation in huge monoliths. The tool is a pragmatic stopgap for projects that can't modularize yet.

The move to KSP over KAPT is repeatedly cited across Android communities as the single most impactful change for Kotlin build speed, and this guide ranks it as a zero-cost baseline step, reinforcing that it should be universal.

The article's suggestion to use a shell app for single-module debugging reflects a trade-off between artifact complexity and iteration speed; for high-frequency feature work, the extra infrastructure pays for itself.

Concepts & terms
KSP (Kotlin Symbol Processing)
An annotation processing API that runs directly on Kotlin symbols instead of generating Java stubs (as KAPT does), significantly reducing compile time for Kotlin projects.
Remote Build Cache
A shared cache (e.g., Gradle Develocity or a self-hosted HTTP node) that stores task outputs keyed by input hashes, letting developers and CI reuse artifacts built by others instead of recompiling.
AAR (Android Archive)
A pre-compiled Android library bundle containing classes, resources, R files, and native libraries, which can be consumed as a binary dependency to skip source compilation.
Non-Transitive R Classes
A Gradle/AGP feature where each module's R class only references its own resources, not all transitive dependencies, reducing R.jar size and compile-time symbol processing.
JVMTI (JVM Tool Interface)
A native interface for the JVM that allows tools to inspect and modify running code; used here for hot reload by dynamically replacing method bodies at runtime.
jugg
An Android Studio plugin from Tencent Music that reuses the last full Gradle build as a baseline, compiles single files with javac/kotlinc, and uses custom aapt2 plus JVMTI hot reload to apply changes in under 3 seconds without Gradle's task graph.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗