Build Your App from the Command Line

In this document

You can execute all the build tasks available to your Android project using
the Gradle wrapper command line tool. It's
available as a batch file for Windows (gradlew.bat) and a shell
script for Linux and Mac (gradlew.sh), and it's
accessible from the root of each project you create with Android Studio.

To run a task with the wrapper, use one of the following commands:

On Windows:

gradlew task-name

On Mac or Linux:

./gradlew task-name

To see a list of all available build tasks for
your project, execute tasks:

gradlew tasks

The rest of this page describes the basics to build and run your app
with the Gradle wrapper. For more information about how to set up your
Android build, see Configure
Your Build.

If you'd prefer to use the Android Studio tools instead of the command line
tools, instead see Build and Run Your App.

About build types

By default, there are two build types available for every Android app: one
for debugging your app—the debug build—and one for releasing your app
to users—the release build. The resulting APK from each build must be
signed with a certificate before you can install on an emulator or device. The
debug build is automatically signed with a debug key provided by the SDK tools
(it's insecure and you cannot publish this APK to Google Play Store), and the
release build must be signed with your own private key.

If you want to build an APK for release, it's important that you first read
Sign Your App.
That page describes the procedure for generating a private key
and then using it to sign your APK file. If you're just getting started,
however, you can quickly run your apps on an emulator or a connected
device by building a debug APK.

You can also define a custom build type in your build.gradle
file and configure it to be signed as a debug build by including
debuggable true. For more information,
see Configure Build Variants.

Build a debug APK

For immediate app testing and debugging, you can build a debug APK.
The debug APK is signed with a debug key provided by the SDK tools and
allows debugging through adb.

To build a debug APK, open a command line and navigate to the root of your
project directory—from Android Studio, select View > Tool Windows >
Terminal. 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/.
The file is already signed with the debug key and aligned with
zipalign, so you can
immediately install it on a device.

Or to build the APK and immediately install it on a running emulator or
connected device, instead invoke installDebug:

gradlew installDebug

The "Debug" part in the above task names is just a
camel-case version of the build variant name, so it can be replaced with whichever build variant you
want to assemble or install. For example, if you have a "demo" product flavor,
then you can build the debug version with the
assembleDemoDebug task.

To see all the build and install tasks available for each variant (including
uninstall tasks), run the tasks task.

Run your app on a device

Before you can run your app on a device, you must enable USB
debugging on your device. You can find the option under
Settings > Developer options.

Note: On Android 4.2 and newer,
Developer options is hidden by default. To make it available,
go to Settings > About phone and tap Build
number seven times. Return to the previous screen to find
Developer options.

Once your device is set up and connected via USB, you can install your app
using either the Gradle install tasks mentioned above or the
adb tool:

adb -d install path/to/your_app.apk

All built APKs are saved in
project_name/module_name/build/outputs/apk/.