What is Gradle

  As an Android app developer, you most likely use Android Studio. And if you use Android Studio, then you would definitely have come across the term Gradle in the form of build.gradle files, gradle errors etc. As an Android developer and Android Studio user, it is important for you to understand what Gradle is and how it works. This article will provide you an elementary understanding of what is Gradle and what is its importance in Android Studio.

What is Gradle

  In simple terms, Gradle is an open-source build automation system. Build automation means performing the tasks like compiling source code into binary code, packaging the binary code and running tests, in an automated manner. In short, Gradle converts your source code (.java files, .xml files etc.) into an actual Android app. Without a build automation utility, you would have to manually perform all these tasks using a command-line and that would make the process quite cumbersome. Some other build automation utilities are :- Ant, Maven, Buildr etc.

Gradle’s role in Android Studio

  In Android Studio, Gradle is used to declare the project configuration. In other words, it is used to declare things like :-

  • Compile SDK version and Build Tools version
  • Package name e.g. com.androidcodebits.programmerscalculator
  • Target API level & Minimum API level e.g. 27, 21
  • App’s version name & version code
  • Dependencies and their versions

  In Android Studio, you can see the following items under the Gradle Scripts category :-

    • build.gradle (This file is present in the project’s root folder)
    • build.gradle (This file is present in the /app folder, inside the project’s root folder)
    • gradle-wrapper.properties
    • proguard-rules.pro
    • gradle.properties
    • settings.gradle
    • local.properties
What is Gradle - Various Gradle files

What is Gradle – Various Gradle files

  This article discusses only the two build.gradle files.

The two build.gradle files

  The two build.gradle files perform different functions.

  • The first build.gradle file is present in the project’s root folder. It contains the url(s) of Google repositories. It also contains some dependencies like Gradle version, Google services etc. Here, the Gradle version to be used is specified. In the following image, Gradle 3.0.1 is being used for Programmer’s Calculator. Also, if you’re using Google services like Firebase, Admob etc. in your app, the Google services dependency (the latest version) has to be used here. Please Note that application dependencies are not placed here. They are placed in the second build.gradle file.
build.gradle in Project's root folder

build.gradle in Project’s root folder

  • The second build.gradle file is present in the /app folder, inside the project’s root folder. It contains the details of the build configuration such as the SDK version to be used for compiling source code, the build tools version, app’s package name, target API level, minimum supported API level, etc. Also, this is where proguard is used for tasks like securing your .apk file and minimizing the size of .apk file, etc. If minifyEnabled is set to true, it obfuscates the methods and variables in your .java files and hence protects it from being accessed by a person who possesses the .apk and wants to modify it. However, this feature takes a lot of time while building therefore, you should use it only when you’re generating the final release .apk file. Before that, set minifyEnabled to false. Apart from this, shrinkResources is set to true for minimizing the size of .apk file. At the bottom of this file, you’ll find various dependencies. Some of the these depedencies are libraries to be used for performing automated tests on the project e.g. junit, espresso, while the rest are the libraries which are to be used by the application that is being developed e.g. firebase, admob.

build.gradle in /app folder

build.gradle in /app folder

  I hope this article helped you gain an elementary understanding of what Gradle is, what it does and how it works. I’ll discuss more about Gradle and common Gradle errors in future articles.

(Visited 810 times, 4 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *