Android Build Acceleration: From Gradle Tuning to AAR and Remote Cache, Plus a Tool That Bypasses Gradle
Farewell to Long Waits: A Complete Guide to Android Build Principles and Common Build Acceleration Solutions
In the daily development of large Android projects, Gradle builds that take several minutes not only interrupt your flow but also seriously limit debugging and iteration efficiency. To truly solve the slow build problem, you can't just copy and paste a few lines of configuration; you need to understand which step in the build process is consuming time, and then apply targeted optimizations.
This article first quickly walks through the core stages and bottlenecks of Android builds, then systematically summarizes four common build acceleration strategies and selection recommendations, and finally introduces some open-source ultra-fast incremental solutions from the community that bypass Gradle entirely, for those interested in exploring further.
1. Know Where the Time Goes: Three Core Stages of Android Builds
A complete Android build (e.g., ./gradlew assembleDebug) typically goes through three main stages. Different acceleration approaches essentially optimize one or a few of these stages:
- Initialization and Configuration — Gradle parses
settings.gradleand all modules'build.gradlefiles to build the complete Task DAG. The more modules, the more complex the custom scripts, the larger the fixed overhead of this step. - Code and Resource Compilation — Includes
aapt2for compiling and linking resources, KAPT/KSP annotation processing,javac/kotlincfor compiling source code, andD8/R8to convert bytecode into Dex files. - Packaging and Deployment — Packages Dex, resources, and native
.solibraries, signs them into an APK, and installs it to the device via ADB.
2. Comprehensive Overview of Common Android Build Acceleration Methods
From lowest to highest implementation cost, the common acceleration methods in practice fall into four dimensions:
1. Gradle and JVM Basic Configuration Optimization (Zero Cost, Should Be Enabled in All Projects)
Adjust build parameters and toolchain configuration to reduce memory GC pauses, serial waits, and redundant tasks in Debug builds:
- Enable Gradle parallelism and caching — In the project root's
gradle.properties, enable parallel builds and configuration cache to avoid re-parsing scripts on every build:
# Enable parallel build and on-demand configuration
org.gradle.parallel=true
org.gradle.configureondemand=true
# Enable build cache and configuration cache (AGP 8.0+ recommended)
org.gradle.caching=true
org.gradle.configuration-cache=true
# Allocate sufficient JVM heap and optimize GC strategy
org.gradle.jvmargs=-Xmx4096m -XX:+UseParallelGC -Dfile.encoding=UTF-8
- Replace KAPT with KSP — Migrate annotation processing fully to KSP (Kotlin Symbol Processing), eliminating the expensive Java stub generation and significantly reducing build time in Kotlin projects.
- Lock Debug dynamic parameters — Fix versionCode, versionName, and BuildConfig timestamps during development to avoid re-merging AndroidManifest and resources on every build.
- Restrict ABI architectures as needed — In Debug builds, package only the CPU architecture of the current debugging device (e.g., arm64-v8a), skipping compilation and packaging of other architectures.
✅ Applicable to: Baseline configuration for all Android projects. No project invasiveness, and it noticeably speeds up full builds and the configuration phase.
2. Build Cache and Binary Dependencies (Suitable for Medium and Large Teams)
Avoid having the same code repeatedly run the same compile tasks in CI pipelines or across different developers' machines:
- Local / Remote Build Cache — After enabling
org.gradle.caching=true, Gradle caches task outputs based on input hashes.
💡 Plain explanation: The limitation of local cache is that cached files only exist on your own machine. Once you switch branches, change machines, or pull in large upstream updates, many tasks' input hashes change and the local cache invalidates. Remote cache puts the cache on a shared service accessible to the whole team and CI. CI runs the full build first and uploads each task's output by hash. Later, when any developer pulls the same code and builds, Gradle computes the task hash and looks it up remotely; if the cache exists, it downloads the output, skipping the time-consuming local task. The team only needs to run one heavy compilation, and everyone shares the results. Note: Remote cache depends on correct task input/output declarations. If builds include unstable variables like random values or timestamps, the hash changes every time and the cache is directly invalidated — that's why earlier we recommended fixing version and timestamp in Debug. Teams can set up a remote cache server (e.g., Gradle Develocity or a custom HTTP cache node), with CI building and pushing cache, so local branch pulls can reuse remote artifacts.
- Component Binaryization (AAR) — Package stable base libraries and low-level common components into
.aarahead of time and publish them to a Maven private repository. Use a script to switch between "source dependency" and "AAR binary dependency" with one click, greatly reducing the number of modules that actually participate in local compilation.
💡 Plain explanation: With source dependency, every time a module is referenced, the build must run the full pipeline: parse source, execute annotation processors, compile classes, process resources — even if you haven't changed a single line, Gradle still does a round of change verification. An AAR is essentially a pre-compiled artifact that already contains classes, resources, R files, and
.solibraries. Switching to AAR dependency skips compiling that module's source locally; it only does file validation and merge, bypassing the entire compile chain. The cost is also obvious: an AAR is a static snapshot. After modifying low-level library code, you need to repackage and upload, and the business side must update versions to take effect. So it's generally only suitable for stable base components that change infrequently.
✅ Applicable to: Multi-module medium-large projects and multi-developer teams. Especially solves the slow first build after switching Git branches or pulling latest branches.
3. Project Structure and Dependency Governance (Architecture-First Solution)
The deep cause of slow builds is often heavy dependency coupling between modules — "a change to one line in a low-level module triggers a cascade of recompilations in all upper modules":
- Strictly distinguish
implementationfromapi— Useimplementationby default instead ofapi, cutting the propagation chain of ABI changes from low-level modules to external callers and narrowing the blast radius of incremental compilation. - Enable Non-Transitive R Classes — Avoid generating a huge R.jar for each business module containing all dependency resources, greatly reducing compile-time symbol resolution and bytecode merge time.
- Set up single-module shell application (Demo App) — Split oversized monolithic app modules, push independent businesses down to library modules, and configure lightweight shell apps for high-frequency iteration lines, so debugging a single feature doesn't require compiling the entire codebase.
✅ Applicable to: Mature projects with hundreds of thousands of lines of code and full builds that take minutes.
4. Incremental Deployment and Hot Reload Tools (Improve Daily Debug Experience)
When changes are concentrated in a small amount of code or UI styles, use hot-deploy mechanisms to skip the full repackage and install process:
| Solution Type | Representative Tools | Core Mechanism | Applicable Scenarios and Limitations |
|---|---|---|---|
| IDE-native hot deploy | Android Studio Apply Changes / Live Edit | Dynamically replace method bodies via the JVMTI interface on Android 8.0+, or refresh Jetpack Compose nodes in real time | Suitable for lightweight method-internal logic changes and Compose UI style tweaks; once structural class changes like adding/removing fields or methods occur, an app restart is often triggered |
| Cross-platform hot reload | Flutter Hot Reload / RN Fast Refresh | At runtime, push incremental code trees directly to the Dart VM or JS engine while preserving page state | Suitable for pure cross-platform or hybrid projects where the business layer already uses cross-platform frameworks |
3. Team Implementation Recommendations
- Small projects / solo development — Execute step 1 (Gradle parameter tuning + KSP + lock Debug configuration), and use Android Studio's native Apply Changes / Live Edit daily. Nearly zero maintenance cost.
- Medium-large commercial projects — Focus on Remote Build Cache + AAR-ifying core base libraries + Non-Transitive R classes + shell app debugging, systematically solving overall team build efficiency.
4. Further Exploration: Bypassing Gradle with Open-Source Ultra-Fast Incremental Solutions
If your team maintains a giant monolithic project with millions of lines of code where full builds still take minutes, and a thorough modular split is difficult to complete in the short term, the community also has advanced approaches that "bypass the compilation" by skipping Gradle's Task checks.
For example, the Android Studio plugin jugg open-sourced by the Tencent Music engineering team. Its core idea is to reuse the most recent complete Gradle build as a baseline. On Debug incremental changes, it directly invokes lower-level javac/kotlinc for single-file compilation, pairs with a custom aapt2 that supports incremental linking for resources, and combines JVMTI hot reload and reflection injection to achieve second-level effectiveness (usually within 3 seconds).
Such a tool requires no modification to the project's build scripts and is highly valuable for giant projects with slow full builds. Interested readers can visit its open-source repository to understand the implementation details and usage boundaries.