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.
Next, we need to add the module to the app's dependency list. To do this, right click on the module that has been added and click on the Open Module Settings
option:

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.