Chapter 3. Managing Dependencies

Dependency management is one of the areas where Gradle really shines. In the best case scenario, all you need to do is add one line to your build file, and Gradle will download the dependency from a remote repository and make sure its classes are available to your project. Gradle even goes a step further. In case a dependency for your project has dependencies of its own, Gradle will resolve those, and take care of everything. These dependencies of dependencies are called transitive dependencies.

This chapter introduces the concepts of dependency management, and explains the multiple ways of adding dependencies to Android projects. These are the main topics we will be talking about:

  • Repositories
  • Local dependencies
  • Dependency concepts

Repositories

When we discuss dependencies, we usually mean external dependencies, such as libraries that are provided by other developers. Manually managing dependencies can be a big hassle. You have to locate the library, download the JAR file, copy it into your project, and reference it. Often these JAR files have no version in their name, so you need to remember to add it yourself, in order to know when to update. You also need to make sure the libraries are stored in a source control system, so that the team members can work with the code base without manually downloading the dependencies themselves.

Using repositories can solve these issues. A repository can be seen as a collection of files. Gradle does not define any repositories for your project by default, so you need to add them to the repositories block. If you use Android Studio, this is done for you. We have mentioned the repositories block briefly in the previous chapters; it looks like this:

repositories {
    jcenter()
}

Gradle supports three different kinds of repositories: Maven, Ivy, and static files or directories. Dependencies are fetched from the repositories during the execution phase of the build. Gradle also keeps a local cache, so a particular version of a dependency only needs to be downloaded to your machine once.

A dependency is identified by three elements: group, name, and version. The group specifies the organization that created the library and is usually a reverse domain name. The name is a unique identifier for the library. The version specifies which version of the library you want to use. Using these three elements, a dependency can be declared in the dependencies block with the following structure:

dependencies {
    compile 'com.google.code.gson:gson:2.3'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
}

This is shorthand for the full Groovy map notation, which looks like this:

dependencies {
    compile group: 'com.google.code.gson', name: 'gson', version: '2.3'
    compile group: 'com.squareup.retrofit', name: 'retrofit' version: '1.9.0'
}

Note

The only required field for a dependency is the name. Group and version are optional elements. Nonetheless, it is recommended to add the group for clarity, and the version in order to make sure libraries are not updated automatically, which could cause a build to break.

Preconfigured repositories

For your convenience, Gradle has preconfigured three Maven repositories: JCenter, Maven Central, and the local Maven repository. To include them in your build script, you need to include these lines:

repositories {
    mavenCentral()
    jcenter()
    mavenLocal()
}

Maven Central and JCenter are two well-known online repositories. There is no reason to use both of them at the same time, and it is always recommended to use JCenter, which is also the default repository in Android projects created with Android Studio. JCenter is a superset of Maven Central, so when you make the switch, you can leave your already defined dependencies intact. On top of that, it supports HTTPS, unlike Maven Central.

The local Maven repository is a local cache of all the dependencies you have used, and you can add your own dependencies as well. By default, the repository can be found in the home directory in a folder called .m2. On Linux or Mac OS X, the path is ~/.m2. On Microsoft Windows, it is %UserProfile%.m2.

Besides these preconfigured repositories, you can also add other public, or even private repositories.

Remote repositories

Some organizations create interesting plugins or libraries, and prefer to host them on their own Maven or Ivy server, instead of publishing them to Maven Central or JCenter. To add those repositories to your build, all you need to do is to add the URL to a maven block.

repositories {
    maven {
        url "http://repo.acmecorp.com/maven2"
    }
}

The same goes for Ivy repositories. Apache Ivy is a dependency manager that is popular in the Ant world. Gradle supports these repositories in a format that is identical to the one that is used for Maven repositories. Add the repository URL to an ivy block, and you are good to go:

repositories {
    ivy {
        url "http://repo.acmecorp.com/repo"
    }
}

If your organization is running its own repository, chances are that it is secured, and you need credentials to access it. This is how you add credentials for a repository:

repositories {
    maven {
        url "http://repo.acmecorp.com/maven2"
        credentials {
            username 'user'
            password 'secretpassword'
        }
    }
}

The approach for Maven and Ivy is the same here as well. You can add a credentials block with the same format to the configuration of your Ivy repository.

Tip

Storing credentials

It is not a good idea to store your credentials in the build configuration file. The build configuration file is plain text, and is checked into the source control system. A better idea would be to use a separate Gradle properties file, as we have seen in Chapter 2, Basic Build Customization.

Local repositories

It is possible to run Maven and Ivy repositories on your own hard drive or a network drive. To add these to the build, you just need to configure the URL to a relative or absolute path to the location on the drive:

repositories {
    maven {
        url "../repo"
    }
}

New Android projects have a dependency on the Android Support Library by default. When installing the Google repositories using the SDK manager, two Maven repositories are created on your hard drive ANDROID_SDK/extras/google/m2repository and ANDROID_SDK/extras/android/m2repository. This is where Gradle gets the libraries provided by Google, such as the Android Support Library and Google Play Services.

You can add a regular directory as a repository as well, using flatDirs. This enables you to add files from that directory in the dependency block.

repositories {
    flatDir {
        dirs 'aars'
    }
}

Later in this chapter, when we talk about library projects, we will look at an example of how this can be used.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.104.95