Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to Build Android Applications with Gradle?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

There are several contemporary build automation technologies available nowadays that may be utilized to create a quicker build when creating a project. Gradle is one of these modern-day automation technologies. Gradle is now used by Android to automate and control the build process while also defining flexible custom build settings. As a result, it is required to design or build an Android application utilizing such sophisticated build tools as Gradle. 

So, in this article, we’ll learn how to use Gradle to boost our builds. In addition, we will study the fundamental syntax in the build.gradle file created by Android Studio. We will learn about gradlew tasks, build variations, and many other uses later in this blog.

What exactly is Gradle?

Before we continue, let’s take a brief look at Gradle. Gradle, a sophisticated build toolkit, is used by Android Studio to automate and control the build process while allowing you to design flexible custom build settings. So, with Gradle, you can build, test, and deploy your application, and the files responsible for this sort of automation are the “Build.gradle” files. Gradle build scripts, for example, may do simple tasks such as copying pictures from one location/directory to another before the real build process begins. Gradle is required for Android projects in order to compile source code and resources.

When you create an Android Project in Android Studio, the build.gradle file is automatically produced, and when you hit the run button in Android Studio, the associated Gradle task is called, and the application is started or launched. When you run your source code in Android Studio, it will be transformed into DEX (Dalvik Executable) files. These DEX files contain the bytecode needed to execute the program on Android. APK Packager is used to bundle multiple DEX files with the application’s resources. The APK Packager then signs your APK using either the debug or release Keystore, and before creating your final APK, it optimizes your program to remove unnecessary code.

Create Configuration Files

When you create a new project in Android Studio, it generates several files for you. Let us first define the scope and purpose of these files. A typical Android project’s project view is as follows:

  1. The Gradle configuration file: The file settings.gradle may be found in the root directory. It is made up of all of the modules included in the app.
  2. The top-level build file is the build.gradle file, which is stored in the root directory. If you wish to use the same configuration for all modules in your project, declare them in this file.
  3. The module-level build file is as follows: The module-level build.gradle file, which can be found in each project/module/ directory, allows us to define build parameters for the module in which it is placed.
  4. Gradle configuration file: You may define parameters for the Gradle toolkit in the Gradle properties file.

Commands for Gradle

We have already covered the fundamentals of Gradle, and now we will look at some of the Gradle commands that may be used to conduct the build process without using Android Studio, i.e. via the command line. You can open gradle and then use the following commands which will help you.

1. Gradle Wrapper: Gradle is still in development, and a new version might arrive at any time. Gradle Wrapper is used to provide a seamless flow while running Gradle instructions. On Mac OS or Linux, the Gradle Wrapper is a shell script named gradlew, while on Windows, it is a batch file called gradlew.bat. So, if you run this script and the specified version of Gradle is not present, it will download the selected version of Gradle for you. 
If you use Android Studio to build an Android project, this Gradle Wrapper will be generated automatically. If you want to make your own Gradle Wrapper, then download Gradle from here and then execute the following command in your project directory:

$ gradle wrapper --gradle-version x.x

2. gradlew: The Gradle wrapper is gradlew. If you used Android Studio to build the Android project, navigate to the root directory of your project on the command line

This will provide a list of things you can perform using Gradle:

> Task :help
Welcome to Gradle 5.1.1.
To run a build, run gradlew <task> ...
To see a list of available tasks, run gradlew tasks
To see a list of command-line options, run gradlew --help
To see more detail about a task, run gradlew help --task <task>
For troubleshooting, visit

3. gradlew responsibilities: This is used to display a list of available jobs. In the command line, type the following command:

> Task :tasks
Tasks runnable from root project
Android tasks
androidDependencies - Displays the Android dependencies of the project.
signingReport - Displays the signing info for the base and test modules
sourceSets - Prints out all the source sets defined in this project.

Build tasks
assemble - Assemble main outputs for all the variants.
assembleAndroidTest - Assembles all the Test applications.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
bundle - Assemble bundles for all the variants.
clean - Deletes the build directory.
cleanBuildCache - Deletes the build cache directory.

Build Setup tasks
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

4. gradlew lint: The gradlew lint command is used to discover errors across the project, i.e. it will look for different errors, typos, and vulnerabilities. When you are at the root of your project, you can use all these commands

> Task :app:lint
Ran lint on variant debug: 30 issues found
Ran lint on variant release: 102 issues found
Wrote HTML report to file:
Wrote XML report to file:

5. gradlew build: To build your project, execute the following command in your root directory:

$ ./gradlew build

6. gradlew clean build: You may use this command to clear and obfusticate your code the build will be rewritten from the ground up.

$ ./gradlew clean build 

7. gradlew examination: Use the following command to perform the application test:

$ ./gradlew test

Other Gradle Characteristics

Gradle, in addition to being useful on command lines, has a plethora of other capabilities. Some of these are as follows:

Manage dependencies version: In a multi-module project, a number of dependencies are utilized, and the most common issue that emerges, as a result, is a conflict in the version of the dependencies, i.e. only if you have the dependency of the server then only they will run. The versions of these dependencies are tough to maintain, however, Gradle provides an ext block where we can declare our common property values and utilize them in dependencies.

apply plugin: ''
android {

ext {
    supportLibraryVersion = '28.1.0'


dependencies {
    // android supported libraries
    implementation "$supportLibraryVersion"
    implementation "$supportLibraryVersion"
    implementation "$supportLibraryVersion"

    // google play service
    implementation "$playServiceVersion"
    implementation "$playServiceVersion"

    // your other project files

Build types and build flavors

By default, there are two types of builds in Android: debug and release. These building types are used to generate distinct application variants. We imply by “flavor” that the same program might have distinct features for various users. For example, the premium edition of the software should have different features than the free one. You may use the productFlavors closure in your app level build.gradle file to specify multiple flavors. As an example, consider the following:

productFlavors {
    paid {
        applicationId = "your app id goes here"
        versionName = "1.1-paid"
free {
        applicationId = "your app id"
        versionName = "1.0-free"

Creating tasks

As we’ve seen, the./gradlew tasks command may be used to retrieve a list of available tasks. However, we may also construct our own tasks. For example, we might write a task that instructs Gradle to generate an APK file with the build date in its name. To do so, add the following code to your module-level build.gradle file:


task postTopic() {
    android.seeAllVariants.all { here ->
        here.outputs.all { output ->
            def gfg = new Date().format("dd-yyyy")
            def geeksforgeeks = + "_" + date + ".apk"
            output.finalName = last

We have one job here called postTopic(), and you are creating a new filename by appending the variable and the build date to it.


We learned how to utilize Gradle in our Android project in this article. We learned several Gradle commands. The whole set of commands is available on the Gradle website.

My Personal Notes arrow_drop_up
Last Updated : 14 Sep, 2022
Like Article
Save Article
Similar Reads
Related Tutorials