Android | build.gradle
Gradle is a build system (open source) that is used to automate building, testing, deployment, etc. “build.gradle” are scripts where one can automate the tasks. For example, the simple task to copy some files from one directory to another can be performed by the Gradle build script before the actual build process happens.
Why is Gradle Needed?
Every Android project needs a Gradle for generating an apk from the .java and .xml files in the project. Simply put, a gradle takes all the source files (java and XML) and applies appropriate tools, e.g., converts the java files into dex files and compresses all of them into a single file known as apk that is actually used. There are two types of build.gradle scripts
- Top-level build.gradle
- Module-level build.gradle
It is located in the root project directory and its main function is to define the build configurations that will be applied to all the modules in the project. It is implemented as:
The top-level build.gradle supports various build configurations like:
buildscript: This block is used to configure the repositories and dependencies for Gradle.
Note: Don’t include dependencies here. (those will be included in the module-level build.gradle)
dependencies: This block in buildscript is used to configure dependencies that the Gradle needs to build during the project.
This line adds the plugins as a classpath dependency for gradle 3.0.1.
- allprojects: This is the block where one can configure the third-party plugins or libraries. For freshly created projects android studio includes mavenCentral() and Google’s maven repository by default.
- task clean(type:Delete): This block is used to delete the directory every time the project is run. This way the projects keep clean when someone modifies some config files like, during settings.gradle which requires a complete clean.
Located in the project/module directory of the project this Gradle script is where all the dependencies are defined and where the SDK versions are declared. This script has many functions in the project which include additional build types and override settings in the main/app manifest or top-level build.gradle file. It is implemented as:
The Module-level build.gradle supports various build configurations like:
- android: This block is used for configuring the specific android build options.
- compileSdkVersion – This is used to define the API level of the app and the app can use the features of this and lower level.
- applicationId– This is used for identifying unique id for publishing of the app.
- minSdkVersion– This defines the minimum API level required to run the application.
- targetSdkVersion– This defines the API level used to test the app.
- versionCode– This defines the version code of the app. Every time an update needs to be of the app, the version code needs to be increased by 1 or more.
- versionName– This defines the version name for the app. this could be increased by much while creating an update.
- minifyEnabled– this will enable code shrinking for release build.
- proguardFiles– this will specify the progaurd settings file.
- dependencies: This specifies the dependencies that are needed to build the project.
Both the top-level and module-level build.gradle files are the main script files for automating the tasks in an android project and are used by Gradle for generating the APK from the source files.