gradle-build-performance by new-silvermoon/awesome-android-agent-skills
npx skills add https://github.com/new-silvermoon/awesome-android-agent-skills --skill gradle-build-performance./gradlew assembleDebug --scan./gradlew assembleDebug --scan
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
./gradlew assembleDebug --profile
# 在 build/reports/profile/ 目录中打开报告
./gradlew assembleDebug --info | grep -E "^\:.*"
# 或在 Android Studio 中查看:构建 > 分析 APK 构建
| 阶段 | 发生内容 | 常见问题 |
|---|---|---|
| 初始化 | 评估 settings.gradle.kts | 过多的 include() 语句 |
| 配置 | 评估所有 build.gradle.kts 文件 | 开销大的插件,急切的任务创建 |
| 执行 | 基于输入/输出运行任务 | 缓存未命中,非增量任务 |
构建扫描 → 性能 → 构建时间线
跨构建缓存配置阶段(AGP 8.0+):
# gradle.properties
org.gradle.configuration-cache=true
org.gradle.configuration-cache.problems=warn
跨构建和机器重用任务输出:
# gradle.properties
org.gradle.caching=true
同时构建独立模块:
# gradle.properties
org.gradle.parallel=true
为大型项目分配更多内存:
# gradle.properties
org.gradle.jvmargs=-Xmx4g -XX:+UseParallelGC
减少 R 类大小和编译(AGP 8.0+ 默认):
# gradle.properties
android.nonTransitiveRClass=true
对于 Kotlin,KSP 比 kapt 快 2 倍:
// 之前(慢)
kapt("com.google.dagger:hilt-compiler:2.51.1")
// 之后(快)
ksp("com.google.dagger:hilt-compiler:2.51.1")
固定依赖版本:
// 不好:每次构建都强制解析
implementation("com.example:lib:+")
implementation("com.example:lib:1.0.+")
// 好:固定版本
implementation("com.example:lib:1.2.3")
将最常用的仓库放在前面:
// settings.gradle.kts
dependencyResolutionManagement {
repositories {
google() // 第一:Android 依赖
mavenCentral() // 第二:大多数库
// 第三方仓库放在最后
}
}
对于大型单体仓库,复合构建比 project() 更快:
// settings.gradle.kts
includeBuild("shared-library") {
dependencySubstitution {
substitute(module("com.example:shared")).using(project(":"))
}
}
# gradle.properties
kapt.incremental.apt=true
kapt.use.worker.api=true
不要在配置阶段读取文件或进行网络调用:
// 不好:在配置阶段运行
val version = file("version.txt").readText()
// 好:推迟到执行阶段
val version = providers.fileContents(file("version.txt")).asText
避免 create(),使用 register():
// 不好:急切配置
tasks.create("myTask") { ... }
// 好:惰性配置
tasks.register("myTask") { ... }
症状:构建扫描显示"配置构建"时间过长
原因与修复:
| 原因 | 修复 |
|---|---|
| 急切的任务创建 | 使用 tasks.register() 替代 tasks.create() |
| buildSrc 依赖过多 | 迁移到使用 includeBuild 的约定插件 |
| 构建脚本中的文件 I/O | 使用 providers.fileContents() |
| 插件中的网络调用 | 缓存结果或使用离线模式 |
症状::app:compileDebugKotlin 耗时过长
原因与修复:
| 原因 | 修复 |
|---|---|
| 非增量更改 | 避免使缓存失效的 build.gradle.kts 更改 |
| 模块过大 | 拆分为更小的功能模块 |
| 过度使用 kapt | 迁移到 KSP |
| Kotlin 编译器内存不足 | 增加 kotlin.daemon.jvmargs |
症状:即使没有更改,任务也总是重新运行
原因与修复:
| 原因 | 修复 |
|---|---|
| 不稳定的任务输入 | 使用 @PathSensitive, @NormalizeLineEndings |
| 输出中的绝对路径 | 使用相对路径 |
缺少 @CacheableTask | 为自定义任务添加注解 |
| 不同的 JDK 版本 | 跨环境标准化 JDK |
// settings.gradle.kts
buildCache {
local { isEnabled = true }
remote<HttpBuildCache> {
url = uri("https://cache.example.com/")
isPush = System.getenv("CI") == "true"
credentials {
username = System.getenv("CACHE_USER")
password = System.getenv("CACHE_PASS")
}
}
}
用于高级构建分析:
// settings.gradle.kts
plugins {
id("com.gradle.develocity") version "3.17"
}
develocity {
buildScan {
termsOfUseUrl.set("https://gradle.com/help/legal-terms-of-use")
termsOfUseAgree.set("yes")
publishing.onlyIf { System.getenv("CI") != null }
}
}
# 跳过仅 UI 更改的测试
./gradlew assembleDebug -x test -x lint
# 仅运行受影响的模块测试
./gradlew :feature:login:test
优化后,请验证:
每周安装量
241
仓库
GitHub 星标
552
首次出现
2026年1月27日
安全审计
安装于
opencode219
codex211
gemini-cli193
github-copilot184
kimi-cli174
amp172
./gradlew assembleDebug --scan./gradlew assembleDebug --scan
./gradlew assembleDebug --profile
# Opens report in build/reports/profile/
./gradlew assembleDebug --info | grep -E "^\:.*"
# Or view in Android Studio: Build > Analyze APK Build
| Phase | What Happens | Common Issues |
|---|---|---|
| Initialization | settings.gradle.kts evaluated | Too many include() statements |
| Configuration | All build.gradle.kts files evaluated | Expensive plugins, eager task creation |
| Execution | Tasks run based on inputs/outputs | Cache misses, non-incremental tasks |
Build scan → Performance → Build timeline
Caches configuration phase across builds (AGP 8.0+):
# gradle.properties
org.gradle.configuration-cache=true
org.gradle.configuration-cache.problems=warn
Reuses task outputs across builds and machines:
# gradle.properties
org.gradle.caching=true
Build independent modules simultaneously:
# gradle.properties
org.gradle.parallel=true
Allocate more memory for large projects:
# gradle.properties
org.gradle.jvmargs=-Xmx4g -XX:+UseParallelGC
Reduces R class size and compilation (AGP 8.0+ default):
# gradle.properties
android.nonTransitiveRClass=true
KSP is 2x faster than kapt for Kotlin:
// Before (slow)
kapt("com.google.dagger:hilt-compiler:2.51.1")
// After (fast)
ksp("com.google.dagger:hilt-compiler:2.51.1")
Pin dependency versions:
// BAD: Forces resolution every build
implementation("com.example:lib:+")
implementation("com.example:lib:1.0.+")
// GOOD: Fixed version
implementation("com.example:lib:1.2.3")
Put most-used repositories first:
// settings.gradle.kts
dependencyResolutionManagement {
repositories {
google() // First: Android dependencies
mavenCentral() // Second: Most libraries
// Third-party repos last
}
}
Composite builds are faster than project() for large monorepos:
// settings.gradle.kts
includeBuild("shared-library") {
dependencySubstitution {
substitute(module("com.example:shared")).using(project(":"))
}
}
# gradle.properties
kapt.incremental.apt=true
kapt.use.worker.api=true
Don't read files or make network calls during configuration:
// BAD: Runs during configuration
val version = file("version.txt").readText()
// GOOD: Defer to execution
val version = providers.fileContents(file("version.txt")).asText
Avoid create(), use register():
// BAD: Eagerly configured
tasks.create("myTask") { ... }
// GOOD: Lazily configured
tasks.register("myTask") { ... }
Symptoms : Build scan shows long "Configuring build" time
Causes & Fixes:
| Cause | Fix |
|---|---|
| Eager task creation | Use tasks.register() instead of tasks.create() |
| buildSrc with many dependencies | Migrate to Convention Plugins with includeBuild |
| File I/O in build scripts | Use providers.fileContents() |
| Network calls in plugins | Cache results or use offline mode |
Symptoms : :app:compileDebugKotlin takes too long
Causes & Fixes:
| Cause | Fix |
|---|---|
| Non-incremental changes | Avoid build.gradle.kts changes that invalidate cache |
| Large modules | Break into smaller feature modules |
| Excessive kapt usage | Migrate to KSP |
| Kotlin compiler memory | Increase kotlin.daemon.jvmargs |
Symptoms : Tasks always rerun despite no changes
Causes & Fixes:
| Cause | Fix |
|---|---|
| Unstable task inputs | Use @PathSensitive, @NormalizeLineEndings |
| Absolute paths in outputs | Use relative paths |
Missing @CacheableTask | Add annotation to custom tasks |
| Different JDK versions | Standardize JDK across environments |
// settings.gradle.kts
buildCache {
local { isEnabled = true }
remote<HttpBuildCache> {
url = uri("https://cache.example.com/")
isPush = System.getenv("CI") == "true"
credentials {
username = System.getenv("CACHE_USER")
password = System.getenv("CACHE_PASS")
}
}
}
For advanced build analytics:
// settings.gradle.kts
plugins {
id("com.gradle.develocity") version "3.17"
}
develocity {
buildScan {
termsOfUseUrl.set("https://gradle.com/help/legal-terms-of-use")
termsOfUseAgree.set("yes")
publishing.onlyIf { System.getenv("CI") != null }
}
}
# Skip tests for UI-only changes
./gradlew assembleDebug -x test -x lint
# Only run affected module tests
./gradlew :feature:login:test
After optimizations, verify:
Weekly Installs
241
Repository
GitHub Stars
552
First Seen
Jan 27, 2026
Security Audits
Gen Agent Trust HubFailSocketPassSnykPass
Installed on
opencode219
codex211
gemini-cli193
github-copilot184
kimi-cli174
amp172
Azure 升级评估与自动化工具 - 轻松迁移 Functions 计划、托管层级和 SKU
68,100 周安装