I want to build an Android Studio app (the Gradle build system), but I want to do this via the command line.
Adding value to all these answers,
many have asked the command for running App in AVD after build sucessful.
adb install -r {path-to-your-bild-folder}/{yourAppName}.apk
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.
gradlew
(On Windows gradlew.bat
)
adb install -r exampleApp.apk
(The -r
makes it replace the existing copy, add an -s
if installing on an emulator)
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)
For Mac use this command
./gradlew task-name
Either
./gradlew
, or gradlew.bat
if on Windows
chmod +x ./gradlew
may be necessaryFrom this point onwards, gradle
refers to running Gradle whichever way you've chosen.
Substitute accordingly.
If you've manually installed the SDK
export ANDROID_HOME=<install location>
~/.profile
if it's not done automaticallyAccept the licenses: yes | sdkmanager --licenses
sdkmanager
can be found in $ANDROID_HOME/tools/bin
sdkmanager
may have to be run as rootTry running gradle
chown -R user:group $ANDROID_HOME
chmod 777 -R $ANDROID_HOME
gradle tasks
lists 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
Some essential tasks
gradle assemble
: build all variants of your app
app/[appname]/build/outputs/apk/[debug/release]
gradle assembleDebug
or assembleRelease
: build just the debug or release versionsgradle installDebug
or installRelease
build and install to an attached device
adb devices
, check that your device is listed and device is
beside itAutomatically 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 occurgradle -h
for more helpnote, 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.