Understanding the Gradle System
Google introduced Gradle and Android Studio in order to help make the development process more streamlined. They wanted to ensure that it becomes easier for developers to reuse code and also help them create build variants with ease. Having it closely integrated with an IDE such as Android Studio ensured that Gradle has a good IDE integration without making the build system dependent on the IDE.
In this chapter, we will discuss:
- Setting up Gradle in Android Studio
- Dependent libraries to be used in Android Studio, including Identifiers
If you have been using Eclipse, it's likely that some of you won't know of any alternative to the default APK generation technique within the IDE. But, as such as alternative, you can do this using the command line. The Android build system compiles app resources and source code, and packages them into APKs that you can test, deploy, sign, and distribute.
Stepping into the Gradle world
Gradle is an open source build automation system that is based on the Apache ANT and Maven concept. It introduces a Groovy-based Domain Specific Language (DSL) instead of the XML which is primarily used by Apache Maven for declaring the project configuration. Gradle was designed keeping in mind the support for multi-project builds which grow to be quite a large size, and it supports incremental builds. Gradle does this by understanding which parts of the build tree are up to date.
This ensures that tasks dependent on those parts will not be re-executed. Gradle determines which tasks need to be run and in which order, using Directed Acyclic Graph (DAG).
Gradle can automate the building, testing, publishing, deployment, and more of software packages or other types of projects. Using the combination of the power and flexibility of ANT and the dependency management and conventions of Maven, Gradle helps to build in a more effective manner.
First, let's get familiarized with the Gradle environment inside Android Studio. To do that we should first create a new Android Project. I assume you have Android Studio installed by now. Here is the link in case you wish to know more about the install: https://developer.android.com/studio/install.html
Now that you have the Android Studio installed, we will first create a new project,
Open Android Studio, create a New Project, and give a name to your project as seen in the following figure:

At the Target Android Devices screen, without making any changes, just click on Next:

Next, at the Add an Activity to Mobile screen, select the Empty Activity option for now:

Now, the Activity and an XML file will be generated by default. Just click Finish when done:

When the project load is complete, just change the view structure to Project. You may leave it at Project Files by default:

Once this is done open the build.gradle file. Here you will see the libraries that are compiled in the project:

We have completed the launch of a blank project and we now should understand the basic setup of the same. In the next section, we will talk about adding Gradle to this app.
Adding Gradle to your app
You can Gradle build script dependency to your app in the following way:
Open the file from your app module.
Here, in the dependencies, add the Gradle identifier for a library that you want to import:

Let us consider the current Gradle library, for instance:
com.android.support:appcompat-v7:23.1.1
The components of this Gradle library could be distributed in sections to ease understanding. Here are a few pointers to make note of:
- com.android.support is the package name of the project
- appcompat-v7 is the project name
- 23.1.1 is the version of the project
We have now completed the setup for Gradle in Android Studio. We will be using several libraries in our App. In the next section, we will see how to add new Gradle Libraries.
Adding a new Gradle library
Making Android Development more awesome, Gradle allows us to incorporate libraries in to Android Studio in different ways. Using these, developers can easily include their libraries using Gradle dependencies. In this section we will discuss the following techniques:
- Adding a Gradle identifier
- Adding as a module
Adding a Gradle identifier
To add a new Gradle library, find the Gradle identifier for the third party library, and add it to the dependencies list.
When you make changes to the build configuration files in your project, Android Studio requires that you sync your project files so that it can import your build configuration changes and run some checks to make sure your configuration won't create build errors.
To sync your project files, click Sync Now (as seen in the following figure) in the notification bar (this appears when you make a change), or click Sync Project from the menu bar. If Android Studio notices any errors with your configuration--for example, if your code uses API features that are only available in an API level higher than your compileSdkVersion-- the Messages window appears to describe the issue:

Next, we will discuss how libraries can be added using a module.
Adding as a module
You can also add a library in Android Studio by adding it as a module. To add the module:
- First, place the library code in any folder of your choice
- Then, you need to Import the library as a module in your app. The figure below shows the steps to add the library as a Module:

This will open a new window where you need to select the library that you have saved to the directory. When you have selected the directory, click on Done. This will import the external library into your project.
As an example, I have added the module crop image to my project, which appears in my project folder.

It will open a new window with your app module and library module in the list. Choose you app module, and then select the dependency list.
Next, click on the plus icon which will open another dialog with the module name. Select it and click OK:

This will build the Gradle and add the module to the build.gradle, and it will be seen as a compiled project here:

Note that Core, UI and Util sub projects can also have their own build.gradle file, depending on their specific needs. Alternatively, you can also define the dependencies of a project in the root build.gradle file, as discussed in the preceding section. In this case, we won't be focusing on these points.
Summary
We started the chapter by looking at what Gradle is and how it is important in project development. We briefly looked at the Android Studio setup and how it can help in building the system along with Gradle. After the introduction, we talked about how developers can set up Gradle in Android Studio. We also created a new project in Android and discussed how Gradle libraries can be added to the project.
In Chapter 2, Exploring Android Studio Developer Tools we will discuss some of the key developer tools in Android Studio.