Android Gradle构建耗时过长,附相关配置文件寻求优化方案
Hey there! Let's tackle those slow Gradle builds you're facing. I've reviewed your configuration files, and here are some practical, actionable optimizations you can implement right away:
1. Upgrade Gradle & Android Gradle Plugin (AGP)
Your current setup uses Gradle 7.1, which is a bit outdated. Newer versions of Gradle and AGP come with significant build speed improvements—like better incremental compilation, optimized dependency resolution, and faster task execution.
- Upgrade Gradle: Switch to a recent stable version (e.g., 7.5 or 8.0.2) by updating the
distributionUrlin yourgradle-wrapper.properties:distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-all.zip - Upgrade AGP: Ensure your project uses the AGP version compatible with your Gradle version. For Gradle 8.0.2, use AGP 8.0.2. Update this in your project-level
build.gradle(the top-level plugins block you didn't share).
2. Tweak gradle.properties for Better Performance
Your existing config has good basics, but we can refine it:
- Update JVM Arguments:
MaxPermSizeis deprecated in Java 8+ (which you're using). Replace it withMaxMetaspaceSize, and add garbage collection optimizations to reduce runtime overhead:
(Adjustorg.gradle.jvmargs=-Xmx4096m -XX:MaxMetaspaceSize=768m -XX:+UseG1GC -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8-Xmxbased on your available RAM—4GB works well if you have 8GB+ total.) - Add Parallel Worker Limit: Control the number of parallel workers to avoid overwhelming your CPU. Set it to half your CPU core count (e.g., 4 for an 8-core machine):
org.gradle.parallel.max-workers=4 - Switch to KSP instead of KAPT: KSP (Kotlin Symbol Processing) is significantly faster than KAPT for annotation processing. If you use libraries like Room or Dagger, migrate to KSP by adding this:
(You'll also need to update your processor dependencies to use their KSP variants.)kapt.use.ksp=true
3. Optimize App Module build.gradle
Let's trim unnecessary work and speed up debug/release builds:
- Remove
buildToolsVersion: AGP automatically selects the correct build tools version based on yourcompileSdk. Removing this line avoids version mismatches and extra overhead. - Unify SDK Versions: Set
targetSdkVersionto matchcompileSdk(31, or better yet, upgrade to 33). This ensures you're using the latest build optimizations and avoids compatibility checks. - Speed Up Debug Builds:
- Disable Firebase Performance instrumentation in debug (it adds runtime overhead):
debug { ext.firebasePerfInstrumentationEnabled = false // ... your existing config } - Enable the new resource processing pipeline for faster resource compilation:
Add this to yourandroidblock:android { experimental { enableNewResourceProcessing true } // ... rest of your config }
- Disable Firebase Performance instrumentation in debug (it adds runtime overhead):
- Optimize Release Builds:
- Ensure you're using R8 instead of ProGuard (AGP uses R8 by default now, but double-check
useProguard falseis set in your release buildType—minifyEnabled trueshould handle this). - Move keystore paths and credentials to
gradle.propertiesinstead of hardcoding them in the build script. This avoids dynamic try-catch logic that can break incremental builds.
- Ensure you're using R8 instead of ProGuard (AGP uses R8 by default now, but double-check
4. Additional Quick Wins
- Trim Dependency Bloat: Run
./gradlew app:dependenciesto identify duplicate or unused dependencies. Removing these reduces dependency resolution time and APK size. - Use Gradle Build Profiler: Generate a build report with
./gradlew --profileto see exactly which tasks are taking the longest. This helps you target specific bottlenecks (e.g., slow annotation processing, resource compilation). - Update Kotlin Version: Use the latest stable Kotlin version (e.g., 1.8.20) for faster compilation and better incremental support. Update this in your project-level
build.gradle.
5. Keep Your Existing Strong Configs
You already have some great settings enabled—don't change these:
org.gradle.daemon=true(reuses the Gradle daemon to avoid startup overhead)org.gradle.parallel=true(builds modules in parallel)org.gradle.configureondemand=true(only configures modules you're working on)org.gradle.caching=true(reuses outputs from previous builds)kapt.incremental.apt=trueandkapt.use.worker.api=true(speeds up annotation processing)
After making these changes, run a clean build with ./gradlew clean build to see the improvements. You should notice a noticeable reduction in build time, especially for incremental builds!
内容的提问来源于stack exchange,提问作者Rethinavel

