Android Programming The Big Nerd Ranch Guide, 3rd Edition

(Brent) #1

Adding dependencies in Android Studio


131

Adding dependencies in Android Studio


You will use the implementation of fragments that comes with the AppCompat library. The
AppCompat library is one of Google’s many compatibility libraries that you will use throughout this
book. You will learn much more about the AppCompat library in Chapter 13.


To use the AppCompat library, it must be included in your list of dependencies. Your project comes
with two build.gradle files, one for the project as a whole and one for your app module. Open the
build.gradle file located in your app module.


Listing 7.1  Gradle dependencies (app/build.gradle)


apply plugin: 'com.android.application'


android {
...
}


dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
...
compile 'com.android.support:appcompat-v7:25.0.1'
...
}


In the current dependencies section of your build.gradle file, you should see something similar to
Listing 7.1 that specifies that the project depends on all of the .jar files in its libs directory. You will
also see dependencies for other libraries that are automatically included when projects are created with
Android Studio, most likely including the AppCompat library.


Gradle allows for the specification of dependencies that you have not copied into your project. When
your app is compiled, Gradle will find, download, and include the dependencies for you. All you have
to do is specify an exact string incantation and Gradle will do the rest.


If you do not have the AppCompat library listed in your dependencies, Android Studio has a tool to
help you add the library and come up with this string incantation. Navigate to the project structure for
your project (File → Project Structure...).


http://www.ebook3000.com

Free download pdf