Adding Libraries

While you are writing some code for an app, the vast majority of the code that is the app comes from other developers. Some might be members of your development team, but far more comes from outsiders: Google and other Android developers.

You have seen some of this already. You did not write Activity, TextView, and similar classes. Instead, they came from the Android SDK, written (primarily) by Google.

Beyond the Android SDK, though, there are thousands of libraries that developers have access to, including many from Google itself. We add these as dependencies in our projects, to use their code alongside ours.

You have seen some of that already too, in the form of libraries for things like ConstraintLayout.

In this chapter, we will explore a bit more about these libraries, how you find them, and how you integrate them.

Depending on a Local JAR

Some projects that you look at will have an implementation statement in their module’s build.gradle file that looks like this:

implementation fileTree(dir: 'libs', include: ['*.jar'])

This pulls in any JAR files that happen to be in the libs/ directory of this module.

JARs, as you probably know, are libraries containing Java code, as created by standard Java build tools (javac, jar, etc.). For the first decade-plus of Java’s existence, we distributed reusable bits of code in the form of JAR files. You would download a JAR from a Web site, drop it into your project, and through something like this implementation statement, say that your project should use the JAR.

In Android, the contents of these JARs are packaged into your APK. Whatever public classes happen to be in those JARs are available to you at compile time.

However, in general, using plain JARs nowadays is considered to be a bad idea. There is no information in a JAR about:

Instead, nowadays, you should try to use artifacts, rather than bare JAR files.


Prev Table of Contents Next

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