Requesting Dependencies

With all that as background, let’s explore a bit more about how those implementation statements are working and how you can add your own for other dependencies that you may want to use.

Find What You Need

First, you need to identify the libraries that address whatever problems you need to solve.

Many libraries will be covered in this book. Yet more are covered in other CommonsWare books. A few more are in the developer documentation.

The biggest catalog of open source libraries is the Android Arsenal. Here you can browse and search for libraries. Each listing will contain links to where you can find out more about that particular library, typically in the form of a GitHub repository.

Beyond that, you will find blog posts, Stack Overflow answers, and other resources pointing out candidate libraries to use.

Configure the Repositories

If you are using one of Google’s libraries, a project created in Android Studio should be set up with the proper artifact repository already. Your project should also come pre-configured to pull from JCenter, where many open source libraries are from. As a result, for the vast majority of libraries, you do not need to configure an additional artifact repository.

But, sometimes you do.

For example, you may find some libraries that advise you to configure support for jitpack.io:

repositories {
    maven {
        url "https://jitpack.io"
    }
}

This is because they are using JitPack to publish their libraries, instead of JCenter.

You can do one of two things:

  1. Add that maven {} closure to the repositories closure in the allprojects closure in your top-level build.gradle file
  2. Add the entire sample repositories closure to your module’s build.gradle file

Either of these will make this artifact repository available to you for that module. The first approach makes it available for all modules in your app, for if you create a more complex app that involves multiple modules.

So, when you review the documentation for a dependency, it should indicate what artifact repository to use, and you need to ensure that you are set up to use that repository. If the library does not state what artifact repository it uses, try adding the dependency, and if it works, then (hopefully) they published the artifact on JCenter.

Identify the Version That You Want

As noted above, each artifact has an identifier made up of three pieces: a group ID, an artifact ID within the group, and a version number.

Technically, the version number can contain wildcards. For example, while 1.0.2 indicates a specific version, 1.0.+ says “pick the latest among all versions that start with 1.0”. This is convenient for getting patches, but it means that on some random day, all of a sudden, you are using a new version of the library, and that might cause problems. Typically, we do not use wildcards, but instead just keep tabs on when the artifacts get new versions. Android Studio will help with this, highlighting artifacts that have newer versions available.

For Google’s artifacts, you can browse a Web page for their Maven repository to find the available versions for an artifact given its group ID and artifact ID.

For other artifacts, you usually go to the support site for that artifact, which for many artifacts is a GitHub project. A well-maintained library will have details of the latest version of that artifact, so you know what version to request in your module’s build.gradle file.

Add the Dependencies

Then, you just need to add the appropriate implementation lines for whatever dependencies that you wish to add, alongside the existing ones in your module’s build.gradle file.

Note that some libraries showing sample code for adding them to your build.gradle file will show a slightly different syntax, with a compile keyword instead of implementation:

compile 'com.whatever:something:1.2.3'

Older versions of Android Studio (3.0 and back) used compile where we now use implementation, and older documentation may still reference that compile keyword as a result.

We will see lots of dependencies closures throughout the rest of this book, showing different artifacts that we can depend upon.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.