From the course: Android Studio Essential Training

Add external libraries with dependencies

From the course: Android Studio Essential Training

Add external libraries with dependencies

- [Instructor] Android Studio projects use the Gradle Build Tool to build their applications. When you compile and package an application, Gradle looks at a set of what are known as dependencies. Dependencies are declared in a build.gradle file for each module. Each line of a dependency starts with an implementation key word, or in the case of tests, test implementation, or Android test implementation, and then is followed by a literal string. Literal strings that are wrapped in single quotes can't have any variable values in them. They include the domain that the package is from, the name of the package, and the version number. If you have anything in here that's outdated, Android Studio will suggest upgrades. So for example, if I change this from 1.4 to 1.3 and then re-sync, I immediately see a visual indicator that there's a more recent version. I can click into that string and then use an intention action, and then change it to the most recent available version. The other way to do this is through the project structure dialogue. Go to the menu and select file, project structure, or you can press that associated keyboard shortcut. From there, go to dependencies, and then select the app module. In this interface, if there are any upgrades available to anything, you should see a squiggly line. And if you move the cursor over that you'll see the message indicating what newer version is available. You can then select that dependency, pull down the list, and then choose the version that you want. In the case of the material design dependency, 1.4.0 is the most recent stable version, but notice there's already an alpha preview version. I'll go back to selecting 1.4 0 and click okay, and that changes made in the file and the re-sync happens automatically. If you want to add a dependency and you don't know what the implementation string is, you can find it in the project structure dialogue. Once again, I'll go to dependencies and the app module, and this time, I'm going to click the plus icon and choose library dependency. Now let's say I'm looking for the Gson library, this is an open source library from Google that lets you process Json content. I'll tag Gson and click search, and then after a few moments, I see that this dependency is available. I'll select it and click okay, it's added here in the project structure dialogue, then I'll click okay, and it's added in my build.gradle file. If I don't want that dependency anymore, I can delete it directly in the text file, or I can go back to the project structure dialogue. If you make direct changes in the text file, remember to re-sync. So that's the basic of how dependencies work, the literal string is interpreted by the IDE and then the associated library is pulled from a repository. And where is that repository? That's defined in the build.gradle file for the project. You'll see here, what appear to be methods, Google and Maven Central, and these are declarations of locations for repositories, where dependencies can pull their libraries from. In general, you just leave those the way they are, and if you see older applications that have JCenter instead of these two, that was an older Maven repository that's no longer being used. If you have one of those applications, upgrade JCenter to Maven Central, and you'll be ready to go.

Contents