1. 构建相关

# 编译调试版本但不安装
gradle assembleDebug
# 编译发布版本(需要签名)
gradle assembleRelease
# 清理构建产物
gradle clean
# 重新构建(清理+重新编译)
gradle rebuild

2. 安装和运行

# 安装调试版
gradle installDebug
# 安装发布版
gradle installRelease
# 卸载应用
gradle uninstallAll
# 安装并立即启动
gradle installDebug && adb shell am start -n 包名/.MainActivity

3. 测试相关

# 运行所有测试
gradle test
# 运行单元测试
gradle testDebugUnitTest
# 运行设备测试(需要在设备上运行)
gradle connectedDebugAndroidTest
# 生成测试报告
gradle createDebugCoverageReport

4. 代码质量检查

# 代码检查(lint)
gradle lintDebug
# 生成lint报告
gradle lintDebug --continue
# 静态代码分析
gradle detekt

5. 依赖管理

# 显示项目依赖树
gradle dependencies
# 刷新依赖缓存
gradle --refresh-dependencies
# 只下载依赖不编译
gradle build --dry-run

6. 调试和分析

# 生成APK分析报告
gradle analyzeDebugApk
# 查看构建时间分析
gradle build --profile
# 并行构建(加速)
gradle build --parallel
# 离线模式(使用缓存)
gradle build --offline

7. 签名为发布

# 生成签名密钥
gradle signingReport
# 构建并签名发布版
gradle assembleRelease signRelease
# 生成AAB包(Google Play上架用)
gradle bundleRelease

8. 模块化开发命令

# 编译特定模块
gradle :module-name:assembleDebug
# 安装特定模块
gradle :module-name:installDebug
# 运行模块测试
gradle :module-name:test

9. 实用组合命令

# 清理+构建+安装
gradle clean installDebug
# 构建+安装+启动应用
gradle installDebug && adb shell am start -n com.example.webview_bestpractices/.MainActivity
# 构建+lint检查+测试
gradle build lintDebug test

10. 常用的命令行参数

# 详细日志
gradle build --info
# 只显示错误
gradle build --quiet
# 跳过测试
gradle build -x test
# 指定构建变体
gradle assembleDemoDebug

更新: 2026-01-12 19:33:31
原文: https://www.yuque.com/dongpozhouzi-mshe3/zhm85g/yvxafxze4ermku41