Keywords: Android | Gradle | Command Line Build | APK | App Bundle
Abstract: This article provides a detailed guide on using the Gradle wrapper to build Android Studio applications via command line. It explains the purpose and advantages of the Gradle wrapper, then demonstrates step-by-step commands for building debug APKs, release APKs, and app bundles. The content covers the complete workflow from basic build tasks to advanced signing configurations, including build type management, APK location, automatic installation, and custom build variants. With practical code examples and configuration instructions, it helps developers master the core skills of command-line Android app building.
Gradle Wrapper Overview
Android Studio automatically creates a Gradle wrapper in the root directory of your project, which is a script that invokes the actual Gradle binary. The wrapper's main advantage is its ability to keep Gradle updated, simplifying version control management. In the project root directory, you can find the gradlew script (or gradlew.bat on Windows) to execute various Gradle tasks.
Basic Build Commands
To run a Gradle command, use ./gradlew (Linux/Mac) or gradlew (Windows) in the project root directory followed by the task name. For example, to build a debug version of your Android application:
./gradlew assembleDebug
In a default project setup, the resulting APK file is located at app/build/outputs/apk/app-debug.apk. On Unix-like systems, you can use the find . -name '*.apk' command to locate APK files.
Build Types and Variants
Android applications default to two build types: debug build for development testing and release build for user distribution. The debug build is automatically signed with a debug key provided by SDK tools, while the release build requires signing with the developer's private key.
The "Debug" part in build task names is the camel-case version of the build variant name and can be replaced with any build type or variant. For example, if you have a "demo" product flavor, you can build the debug version using the assembleDemoDebug task.
Installation and Deployment
To build the APK and immediately install it on a running emulator or connected device, use the installDebug task:
./gradlew installDebug
To see all available build and install tasks (including uninstall tasks):
./gradlew tasks
App Bundle Building
Android App Bundles include all compiled code and resources but defer APK generation and signing to Google Play. To build a debug version app bundle:
./gradlew :base:bundleDebug
App bundles cannot be deployed directly to devices and are suitable for distribution via Google Play.
Signing Configuration
Release builds must be signed with the developer's private key. You can configure signing information in the build.gradle.kts file:
android {
signingConfigs {
create("release") {
storeFile = file("my-release-key.jks")
storePassword = "password"
keyAlias = "my-alias"
keyPassword = "password"
}
}
buildTypes {
getByName("release") {
signingConfig = signingConfigs.getByName("release")
}
}
}
After configuration, Gradle automatically signs the application during the build process.