Dependencies are declarations you make in your Gradle build script that indicate what features are needed to compile and build your app. These dependencies are based on Maven, a build management tool that originated in the Java community. With dependencies, you can pull Java libraries and other resources from either local repositories that are included in your Android SDK installation, or from remote repositories on the web.
- [Voiceover] Android Studio uses dependencies to tie components together. This differs from the world of Eclipse, where if you wanted to include, say, a Java library, you'd have to copy the JAR file into the project manually. I'll show you how to use a dependency to download a component from a Maven repository. I'll create a brand-new Android Studio project that I'll name Dependencies. Now let's that you wanted to use the jdom library.
Jdom is a popular xml parsing and creation library, it's open-source and completely free and the most recent version works really well with Android. I could go get the JAR file, and I'll show a couple of techniques for working with raw JAR files in different videos. But here, I simply want to go get the JAR file using a Maven dependency. Dependencies are registered in the gradle build script for the module. By default, there's already one dependency here that's using the appcompat library, known as the Android Support Library.
And it's a part of the Android Support Repository. Maven dependencies can use local components, such as this one, so as long as you have the Android Support Repository downloaded into your installation of Android Studio, you'll have access to that code. But the jdom library isn't a part of the Android SDK, and it isn't already installed on my computer. So to get it, here's what I'll do. I'll go to the Menu and choose File, Project Structure.
Then I'll go to my app module, and click on dependencies. Then I'll click the plus icon on the toolbar. On Mac, that's at the bottom of the list. And on Windows, it's at the top right. Then I'll choose Library Dependency. This dialog box gives you a list of some of the most popular dependencies that are used in Android. For example, if I type gson, I would find Google's gson library for working with json. I'm interested in jdom, so I'll type jdom and press Enter or Return, and I see a number of different possibilities.
I'm going to use this version. This is the most recent version of jdom, as of the time of this recording. I'll select it and click Okay, and I see that the dependency listed there and then I'll click Okay again. And the result in the gradle build file is this compiled directive pointing to this particular string. Now during the build process, Android Studio uses that information to go to the Maven repository. It's online, and it downloads the appropriate JAR file.
That JAR file is now a part of my package. I can now go to my main activity class, and let's say that I wanted to create an instance of a class from the jdom library. I'll type the name of one of those classes, Document. There are three different classes named Document that are available to me right now. And I see the one I'm looking for in the package org.jdom2. I'll name the object doc, and I'll instatiate it with the document classes constructor method.
And that's really all there is to it. I haven't gone to get a JAR file, I haven't copied the JAR file into my project, and I don't have to do anything else to configure it. All I need is the dependency declaration in my gradle build file, and everything else is done for me.
- Installing Android Studio on Mac and Windows
- Creating Android Studio projects
- Setting up the development environment, including HAXM and the new Android emulator
- Importing existing code into Android Studio projects
- Exploring the interface, including the editor and project windows
- Managing project builds and dependencies
- Creating new Java classes
- Refactoring code
- Using templates
- Using breakpoints and watch expressions
- Updating apps with Instant Run
- Using Git for version control
Skill Level Beginner
Q: This course was updated on 04/27/2017. What changed?
A: New videos were added that highlight the new features introduced in Android Studio 2.3. In addition, the following topic was updated: update apps with Instant Run.