跪拜 Guibai
← Back to the summary

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

AAR and Remote Build Acceleration Principle

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:

  1. Initialization and Configuration — Gradle parses settings.gradle and all modules' build.gradle files to build the complete Task DAG. The more modules, the more complex the custom scripts, the larger the fixed overhead of this step.
  2. Code and Resource Compilation — Includes aapt2 for compiling and linking resources, KAPT/KSP annotation processing, javac/kotlinc for compiling source code, and D8/R8 to convert bytecode into Dex files.
  3. Packaging and Deployment — Packages Dex, resources, and native .so libraries, 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 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

✅ 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:

💡 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.

💡 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 .so libraries. 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":

✅ 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


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.