I want to build an Android Studio app (the Gradle build system), but I want to do this via the command line.
- 9For example ./gradlew assembleRelease, ./gradlew assembleDebugGabriele Mariotti– Gabriele Mariotti2014-06-25 06:09:44 +00:00Commented Jun 25, 2014 at 6:09
- If you have multiple build variants, then you should go ./gradlew assembleYourVariantReleaseMichał Dobi Dobrzański– Michał Dobi Dobrzański2019-07-17 13:06:45 +00:00Commented Jul 17, 2019 at 13:06
14 Answers
Android Studio automatically creates a Gradle wrapper in the root of your project, which is how it invokes Gradle. The wrapper is basically a script that calls through to the actual Gradle binary and allows you to keep Gradle up to date, which makes using version control easier. To run a Gradle command, you can simply use the gradlew script found in the root of your project (or gradlew.bat on Windows) followed by the name of the task you want to run. For instance, to build a debug version of your Android application, you can run ./gradlew assembleDebug from the root of your repository. In a default project setup, the resulting apk can then be found in app/build/outputs/apk/app-debug.apk. On a *nix machine, you can also just run find . -name '*.apk' to find it, if it's not there.
8 Comments
c:\path_to_my_app\gradlew.bat ./gradlew assemblyDebug, and it showed me Downloading services.gradle.org/distributions/gradle-2.10-all.zip. And it's stuck there. What do I miss?./gradlew installArm7Debug. Other tasks are easy to find from ./gradlew tasks./gradlew assembleDebug and ./gradlew installDebug commands. In the AndroidStudio there is a console which monitors all Android device activities real-time (e.g. if the Android device crashes, the console prints the exception trace). How to get this monitoring facility on a terminal?there are two build types to build your application using the Gradle build settings: one for debugging your application — debug — and one for building your final package for release — release mode.
Building in Debug Mode
First Navigate to Android studio project Root folder using CMD

run this command gradlew.bat assembleDebug
Build signed apk in Release Mode
Edit the build.gradle file to build your project in release mode:
android { ... defaultConfig { ... } signingConfigs { release { storeFile file("myreleasekey.keystore") storePassword "password" keyAlias "MyReleaseKey" keyPassword "password" } } buildTypes { release { ... signingConfig signingConfigs.release } }}
- run this command gradlew.bat assembleRelease
5 Comments
Try this (OS X only):
brew install homebrew/versions/gradle110
gradle build
You can use gradle tasks to see all tasks available for the current project. No Android Studio is needed here.
1. Install Gradle and the Android SDK
Either
- Install these however you see fit
- Run
./gradlew, orgradlew.batif on Windowschmod +x ./gradlewmay be necessary
From this point onwards, gradle refers to running Gradle whichever way you've chosen. Substitute accordingly.
2. Setup the Android SDK
If you've manually installed the SDK
export ANDROID_HOME=<install location>- You may want to put that in your
~/.profileif it's not done automatically
Accept the licenses:
yes | sdkmanager --licensessdkmanagercan be found in$ANDROID_HOME/tools/binsdkmanagermay have to be run as root
Try running
gradle- If there are complaints about licenses or SDKs not being found, fix the directory permissions
chown -R user:group $ANDROID_HOME- If you're reckless and/or the only user:
chmod 777 -R $ANDROID_HOME
- If there are complaints about licenses or SDKs not being found, fix the directory permissions
3. Building
gradle taskslists all tasks that can be run:app:[appname]is the prefix of all tasks, which you'll see in the Gradle logs when you're building- This can be excluded when running a task
Some essential tasks
gradle assemble: build all variants of your app- Resulting .apks are in
app/[appname]/build/outputs/apk/[debug/release]
- Resulting .apks are in
gradle assembleDebugorassembleRelease: build just the debug or release versionsgradle installDebugorinstallReleasebuild and install to an attached device- Have adb installed
- Attach a device with USB debugging and USB file transfer enabled
- Run
adb devices, check that your device is listed and device is beside it
Automatically build and install upon changes
This avoids having to continuously run the same commands
gradle -t --continue installDebug -t: aka--continuous, automatically re-runs the task after a file is changed--continue: Continue after errors. Prevents stopping when errors occur
Run gradle -h for more help
Comments
You're likely here because you want to install it too!
Build
gradlew (On Windows gradlew.bat)
Then Install
adb install -r exampleApp.apk (The -r makes it replace the existing copy, add an -s if installing on an emulator)
Bonus
I set up an alias in my ~/.bash_profile, to make it a 2char command.
alias bi="gradlew && adb install -r exampleApp.apk" (Short for Build and Install)
Comments
I faced the same problem and seems that there have been many changes by google.
I can tell you the steps for installing purely via command line from scratch. I tested it on Ubuntu on 22 Feb 2021.
create sdk folder
export ANDROID_SDK_ROOT=/usr/lib/android-sdk sudo mkdir -p $ANDROID_SDK_ROOT install openjdk
sudo apt-get install openjdk-8-jdk download android sdk
Go to https://developer.android.com/studio/index.html Then down to Command line tools only Click on Linux link, accept the agreement and instead of downloading right click and copy link address
cd $ANDROID_SDK_ROOT sudo wget https://dl.google.com/android/repository/commandlinetools-linux-6858069_latest.zip sudo unzip commandlinetools-linux-6858069_latest.zip move folders
Rename the unpacked directory from cmdline-tools to tools, and place it under $ANDROID_SDK_ROOT/cmdline-tools, so now it should look like: $ANDROID_SDK_ROOT/cmdline-tools/tools. And inside it, you should have: NOTICE.txt bin lib source.properties.
set path
PATH=$PATH:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$ANDROID_SDK_ROOT/cmdline-tools/tools/bin This had no effect for me, hence the next step
browse to sdkmanager
cd $ANDROID_SDK_ROOT/cmdline-tools/tools/bin accept licenses
yes | sudo sdkmanager --licenses create build
Finally, run this inside your project
chmod 777 gradlew sudo ./gradlew assembleDebug This creates an APK named -debug.apk at //build/outputs/apk/debug The file is already signed with the debug key and aligned with zipalign, so you can immediately install it on a device.
FINAL STEPS
Here are the final steps. Make 2 .sh files with these contents. Use chmod 777 before on both. No sudo required.
Download_APK_Code_NOSUDO4.sh
# Don't forget to do chmod 777 Download_APK_Code_NOSUDO2.sh #!/bin/bash if [ -d "camera-samples" ]; then echo "############################# Deleting older code base. ######################################" rm -rf camera-samples fi echo "########################### Download Source Code: Start ... #####################################" git clone git://git.quicinc.com/camera-samples -b iot-concam-apk.lnx.1.1 echo "########################## Download Source Code: Done . . . ####################################" Build_App_NOSUDO4.sh
# Don't forget to do chmod 777 Build_App_NOSUDO2.sh #!/bin/bash currentDir=$(pwd) export ANDROID_SDK_ROOT=$(pwd) # echo "############################################ Install JDK ... ################################################" # apt-get install openjdk-8-jdk if [ -e "commandlinetools-linux-6858069_latest.zip" ]; then echo "############################# Deleting older zip file. ######################################" rm -rf commandlinetools-linux-6858069_latest.zip fi echo "########################################### Download Command Line Tools .. ###################################" wget https://dl.google.com/android/repository/commandlinetools-linux-6858069_latest.zip echo "########################################### Download Command Line Tools Done .. ##############################" if [ -d "cmdline-tools" ]; then echo "############################# Deleting older cmdline-tools. ######################################" rm -rf cmdline-tools fi echo "########################################### Unzip Command Line Tools Start .. #################################" unzip commandlinetools-linux-6858069_latest.zip echo "########################################### Unzip Command Line Tools Done .. #################################" echo "########################################### Creating Directory Structure .. #################################" mv cmdline-tools tools mkdir cmdline-tools cp -r tools cmdline-tools/ rm -rf tools/ PATH=$PATH:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$ANDROID_SDK_ROOT/cmdline-tools/tools/bin echo "########################################## Updated Path : $PATH ###############################################" cd $ANDROID_SDK_ROOT/cmdline-tools/tools/bin echo "########################################## Accept All Licenses .. #############################################" yes | sdkmanager --licenses cd $currentDir/camera-samples/Camera2Video echo "sdk.dir = $ANDROID_SDK_ROOT" > local.properties echo "######################################## Building APK . . . #################################################" chmod 777 gradlew ./gradlew assembleDebug echo "####################################### Building APK Done. . . ############################################" echo "##################################### APK generated here: $currentDir/app/build/outputs/apk/debug/app-debug.apk ###################################" ########################################################## Run these commands.
chmod 777 Download_APK_Code_NOSUDO4.sh chmod 777 Build_App_NOSUDO4.sh ./Download_APK_Code_NOSUDO4.sh ./Build_App_NOSUDO4.sh REFERENCES
https://gist.github.com/guipmourao/3e7edc951b043f6de30ca15a5cc2be40
Android Command line tools sdkmanager always shows: Warning: Could not create settings
"Failed to install the following Android SDK packages as some licences have not been accepted" error
https://developer.android.com/studio/build/building-cmdline#sign_cmdline
///////////////////////////////////
WINDOWS
Here are the steps for Windows via Powershell. Tested on 6th March 2021. You can start completely from scratch.
Prerequisites
- Download git
- Install JDK. I used jdk-8u281-windows-x64.exe
Make a file DownloadAndBuild.ps1 with these contents.
$location = Get-Location if (Test-Path "$location\camera-samples") { Write-Host "########################### Deleting older code base: Start. ################################" Remove-Item -Force -Recurse -Path "$location\camera-samples" Write-Host "########################### Deleting older code base: Done. #################################" } Write-Host "########################## Download Source Code: Start. #####################################" git clone https://source.codeaurora.org/quic/la/camera-samples -b iot-concam-apk.lnx.1.1 Write-Host "########################## Download Source Code: Done. ####################################" if (Test-Path "$location\commandlinetools-win-6858069_latest.zip") { Write-Host "########################### Deleting older zip file: Start. ################################" Remove-Item -Force -Recurse -Path "$location\commandlinetools-win-6858069_latest.zip" Write-Host "########################### Deleting older zip file: Done. #################################" } Write-Host "########################## Download Command Line Tools: Start. #####################################" $client = new-object System.Net.WebClient $client.DownloadFile("https://dl.google.com/android/repository/commandlinetools-win-6858069_latest.zip","commandlinetools-win-6858069_latest.zip") Write-Host "########################## Download Command Line Tools: End. #####################################" if (Test-Path "$location\cmdline-tools") { Write-Host "########################### Deleting older folder: Start. ################################" Remove-Item -Force -Recurse -Path "$location\cmdline-tools" Write-Host "########################### Deleting older folder: Done. #################################" } Write-Host "########################## Extract Command Line Tools: Start. #####################################" Expand-Archive "$location\commandlinetools-win-6858069_latest.zip" -DestinationPath "$location" Write-Host "########################## Extract Command Line Tools: End. #####################################" Write-Host "########################## Create Directory Structure: Start. #####################################" Rename-Item -Path "$location\cmdline-tools" -newName "$location\tools" New-Item -ItemType Directory -Force -Path "$location\cmdline-tools" Move-Item -Path "$location\tools" -Destination "$location\cmdline-tools" Write-Host "########################## Create Directory Structure: End. #####################################" Write-Host "########################## Accept Licenses: Start. #####################################" Set-Location -Path $location/cmdline-tools/tools/bin for($i=0;$i -lt 100;$i++) { $response += "y`n"}; $response | ./sdkmanager.bat --licenses Write-Host "########################## Accept Licenses: End. #####################################" Write-Host "########################## Build APK: Start. #####################################" Set-Location -Path $location/camera-samples/Camera2Video $Env:ANDROID_SDK_ROOT = $location .\gradlew assembleDebug Write-Host "########################## Build APK: End. #####################################" Write-Host "##################################### APK generated here: $location/camera-samples/Camera2Video/app/build/outputs/apk/debug/app-debug.apk ###################################" PAUSE Right-click and run via Powershell. This will download an Android project via git, install the SDK and build the Android App.
Edit as per your convenience.
2 Comments
Build Android app by command line
MacOS variant
./gradlew <moduleName>:assemble<build_variant> //e.g ./gradlew <moduleName>:assembleDebug *./ means current directory
Comments
Official Documentation is here:
To build a debug APK, open a command line and navigate to the root of your project directory. To initiate a debug build, invoke the assembleDebug task:
gradlew assembleDebug This creates an APK named module_name-debug.apk in project_name/module_name/build/outputs/apk/.
Comments
note, you can also do this within Android Studio by clicking the gradle window, and then the 'elephant' button. This will open a new window called "run anything" (can also be found by searching for that name in 'search everywhere') where you can manually type any gradle command you want in. Not "quite" command line, but often provides more of what I need than windows command line.
This allows you to give optional params to gradle tasks, etc.

