Compile an Android project via the Gradle command line (gradlew), suitable for local builds, scripts, and CI/CD.
Prerequisites
- Installed JDK(Android development usually requires JDK 11 or 17, matching the project
compileSdk/AGP) - The project root directory contains gradlew(or the Gradle Wrapper can be generated)
- Configured ANDROID_HOME(or
sdk.dirinlocal.properties)
Basic commands
# 进入工程根目录(含 build.gradle / build.gradle.kts 的目录)
cd /path/to/YourAndroidProject
# 调试包(APK)
./gradlew assembleDebug
Release package (APK, signing must be configured)
./gradlew assembleRelease
Full build (including tests)./gradlew build
Windows 使用 `gradlew.bat`,例如:`gradlew.bat assembleDebug`。
## 常用任务
| 任务 | 说明 |
|------|------|
| `assembleDebug` | 打 Debug APK |
| `assembleRelease` | 打 Release APK |
| `build` | 编译 + 跑测试,产出 Debug/Release |
| `clean` | 清理 build 输出 |
| `installDebug` | 安装 Debug 到已连接设备/模拟器 |
| `bundleRelease` | 打 AAB(Google Play 上架用) |
## 清理后构建
```bash
./gradlew clean assembleDebug
# 或
./gradlew clean build
View all tasks
./gradlew tasks
# 仅看应用相关
./gradlew tasks --group=build
Common issues
- gradlew not found: Run
gradle wrapperin the project root directory to generate it (Gradle must be installed locally), or copygradlew、gradlew.bat、gradle/wrapper/。 - sdk.dir not configured: Create
local.propertiesin the project root directory with the following content:sdk.dir=/path/to/Android/sdk(or set the environment variable ANDROID_HOME). - JDK version mismatch: Use
./gradlew -versionto check the JVM in use; it must match the project/AGP requirements (e.g., JDK 17). - Dependency download failure: Check the network and repository configuration (
repositoriesincludesgoogle()、mavenCentral(), etc.). - CI non-interactive: You can add
-no-daemon、--no-build-cache, etc., to avoid environment differences caused by the daemon and cache.
More references
- For signing configuration, AAB, multiple channels, and CI recommendations, see reference.md