Libraries and Dependencies
Roughly speaking, the code and assets that make up an app come from three sources:
- The source that you and people that you know are writing for this app.
- The source that comes from your
compileSdkVersion
, representing the Android SDK that you are linking to. - Everything else, generally referred to as dependencies. These are libraries, written by Google, independent Android developers, major firms, and so on. Every modern Android app uses libraries, and bigger apps use more libraries.
From a pure technical standpoint, most dependencies are listed in build.gradle
files in dependencies
closures. We have seen two of these in this chapter.
One dependencies
closure appears in the project-level build.gradle
file, inside of a buildscript
closure:
dependencies {
classpath "com.android.tools.build:gradle:7.0.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Those list places where Gradle plugins come from. You are always depending upon the Android Gradle Plugin, and some other developers publish Gradle plugins that you may elect to use in the future. Kotlin users will also use a Kotlin plugin.
However, the dependencies
closure that we tend to think about the most is the one in our module’s build.gradle
file, such as app/build.gradle
, such as this one from the starter project:
dependencies {
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
Here, there are three types of statements:
-
implementation
says “here is a dependency that I want to use for my actual app” -
androidTestImplementation
says “here is a dependency that I want to use for testing” -
testImplementation
says “here is a dependency that I want to use for… a slightly different type of testing”
(we will explore the differences between those in an upcoming chapter)
Our starter project has eight statements in app/build.gradle
attempting to pull in dependencies:
- Three are tied to testing. We will be discussing those soon.
- Two are tied to Kotlin (
org.jetbrains.kotlin:kotlin-stdlib-jre7
andandroidx.core:core-ktx
) and will not be seen in pure-Java projects. - The
constraintlayout
andappcompat
dependencies are specifically part of Jetpack and are foundations for modern Android UI development. We will be spending quite a bit of the book going over what these offer and how you use them. - The
com.google.android.material:material
contains the Material Components for Android, which we saw briefly back in the chapter on resources
Most likely, you will be adding other similar statements to this set, plus perhaps deleting ones that you are not actually using (e.g., the fileTree()
one). We will see how to add other libraries as the book progresses, as many of the things that are common in Android app development require additional libraries.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.