Dependencies in Gradle are a feature that Gradle prides itself on. We only need to add one line of code and Gradle can automatically download the third-party packages we need through the configured dependency repository. If a dependent project also depends on other projects, Gradle will also use the ** (transient dependencies: Transitive Dependencies) ** Automatically resolve its dependencies

Depend on the warehouse

Traditional third-party package references need to download the corresponding Jar package, and then added to the project, this way to find the Jar package is more cumbersome, and the upgrade of the Jar package is more troublesome, but by configuring a dependency repository in Gradle, By adding a single line of dependency code to the project, the GADle automatically downloads the dependencies from the specified repository.

Gradle does not add dependency repositories by default, but Android Studio will add the following code in the root build.gradle file of the project

repositories{
		google()
		mavenCentral()
		jcenter()
}
Copy the code

Note that JCenter will be out of maintenance by the end of 2021. Use Maven Central or Google’s Maven repository instead

Gradle supports three dependency libraries: When dependencies are added to Maven, Ivy, and static files or directories (aar, JAR, etc.), the related gradle tasks will be synchronized. Gradle will download the corresponding version of the dependency package to the local (~/. Gradle directory), a dependency version will only be downloaded once.

To add a dependency, simply add the following code block in the build.gradle file of the corresponding module

dependencies {
    implementation group: 'com.google.code.gson'.name: 'gson'.version:'2.3'
    implementation group: 'com.squareup.retrofit'.name: 'retrofit'.version: '1.9.0'
}
Copy the code

From above, a line of dependent code has three elements: Group, name,version, where name is required, group and version are optional, but the default is to add group and version to ensure their accuracy and to ensure that Gradle runs correctly. Gradle also provides the following abbreviations, which are generally recommended

dependencies{
	implementation 'com. Google. Code. Gson: gson: 2.3'
	implementation 'com. Squareup. Retrofit: retrofit: 1.9.0'
}
Copy the code

Built-in version repository

For convenience, Gradle already has three built-in Maven repositories. If you want to use Gradle, you only need to declare repositories in the Repositories block. The built-in Maven repositories are JCenter,Maven Center and Local Maven Repository. The following code shows how to add a repository

Using the Android Gradle Plugin in Android Studio will add a built-in Maven library called Google. Android-related dependencies can be downloaded from The Maven library of Google (), so it is recommended to put Google () first

repositories {
    google()
    mavenCentral()  
    jcenter()
    mavenLocal()
}
Copy the code

MavenCentral and jCenter are both well-known repositories and do not need to be used together, since jCenter stops maintenance and is about to close public repository administration, so mavenCentral is recommended for now

If you have previously managed dependencies using Maven, add mavenLocal () to gradle to use the original dependencies, but make sure you have the.m2 directory in your user home directory. On OS X or Linux systems, the directory is: ~/.m2, in the Windows environment is %UserProfile%.m2 directory

Remote warehouse

Some large companies or organizations prefer to keep their open source binaries on their own Maven or Ivy servers rather than publish them to MavenCetral or Jcenter, so to add dependencies to these repositories separately, see the code below

repositories {
       maven {
           url "http://repo.acmecorp.com/maven2"}}Copy the code

Similarly, the Ivy library is shown below

repositories {
       ivy {
           url "http://repo.acmecorp.com/repo"}}Copy the code

If your company has its own Maven or Ivy library and requires user name and password verification, see the following code

 repositories {
       maven {
           url "http://repo.acmecorp.com/maven2"
           credentials {
               username 'user'
               password 'secretpassword'}}}Copy the code

Relying on local

In some cases, you may still need to download Jar packages or reference So packages because there are no such packages available in the public repository. This section describes how to configure Gradle in this case

File is dependent on

To add a Jar as a dependency, you can use gradle’s files method. The following code shows how to use it

dependencies {
       implementation files('libs/domoarigato.jar')}Copy the code

If you have many Jar packages, you can add the directory where the Jar packages are located

dependencies {
       implementation fileTree('libs')}Copy the code

By default, creating a new project in AndroidStudio creates the libs directory by default, adding the following code to the build.gradle file by default

 dependencies {
       implementation fileTree(dir: 'libs'.include: ['*.jar'])}Copy the code

So you can put the jar package directly into the libs directory in the project created by AndroidStudio

A reference to the So package

To reference So packages compiled from C or C++ for different platforms, you simply create the jniLibs directory in the module root directory, and then place So packages compiled for different platforms for different platforms. See the architecture below

So ├─ armeabi-v7a │ ├─ nativelib. So ├─ MIPS │ ├─ So ├ ─ x86 │ ── nativelib.soCopy the code

If adding the above structure doesn’t work, the AndroidStudio version or build tools version may be too old to support it. You can solve this problem by adding the following code to the build.gradle file

 android {
       sourceSets.main {
           jniLibs.srcDir 'src/main/libs'}}Copy the code

Library Projects dependencies

If you’re creating cool UI controls or useful toolkits, you can solve this problem by creating a Library Project that generates a.AAR file that can be referenced by the app Project, or you can refer to the Library module directly. See below for details

Create and use library modules

Unlike the Android Application Plugin, library projects use build.gradle at the beginning of the file

apply plugin: 'com. Android. Library'Copy the code

There are two ways to reference a library module: one is to reference it directly in your project, and the other is to generate an AAR file from the library module and then reference the AAR file in your project

If you reference the project in method 1 you need to add the following style code to settings.gradle

include ':app'.':library'
Copy the code

Then add the following code in the dependencies block of the build.gradle file

implementation project(':library')
Copy the code

Use the.aar file

If you want to reuse your library components in different projects without importing the library project, you can use the.aar file. You can find the generated AAR file in the /output/aar/ directory of your library project and add the AAR file as a dependency. You can create aars directory in your project and add repository, as shown in the following code

repositories {
       flatDir {
	   		dirs 'aars'}}Copy the code

Then add the dependencies, as shown below

dependencies {
       implementation(name:'libraryname'.ext:'aar')}Copy the code

Gradle relies on several concepts to understand

Gradle has some concepts to understand when you configure dependencies, even though we may not be using them yet, but understanding the meaning of scope properties such as “implementation” and “testImplementation” will make it much better to use Gradle

The scope of the configuration

At the time of configuration dependent sometimes you may only be used on a specific device, such as you use bluetooth function on a device, you need to add bluetooth SDK reference, but the device itself contains the bluetooth function of binary files, so you don’t have to join the apk your SDK, so this depend on the range of the configuration was born

Gradle scope standard configuration types are as follows:

The new and old Gradle Scope versions are adjusted accordingly, which are differentiated by the new/old format representation, the old representation is deprecated

  1. **implementation/compile: ** This is the default type, and all dependencies configured with this type will be added to the CLASspath as well as the APK file

  2. RuntimeOnly/APK: This type is added to apK files only, not to the compiled CLASspath

  3. CompileOnly/Provided: This is the opposite of apK. This type is added to the compiled CLASspath, not apK. Both APK and provide support jar packages only

  4. TestImplementation/testCompile: when gradle run for unit tests of task will depend on the package to join the classpath

  5. AndroidTestImplementation/androidTestCompile: when gradle run function tests to add such rely on the package to the classpath, and added to the test in the apk

The above five types are standard configuration types. If you also configure build Variant, there will be different build Varialt configurations, such as debugCompile, releaseProvided, etc

Custom of version number

The version number of a dependency package is an important standard for dependency management. The version number of a dependency package is usually specified in the format of major.minor.patch

  • Upgrade major when the API provided is not compatible with older versions
  • Upgrade Minor if there are major improvements and they are compatible with previous releases
  • When you fix a bug, you update the patch

Dynamic version number

In some cases, you may need to rely on the latest version of the project, but you can’t always look up the version and then change it. In this case, you can use the dynamic version number, as shown in the following code example

dependencies {
       implementation 'com. Android. Support: support - v4:22.2 +'
       implementation 'com. Android. Support: appcompat - v7:22.2 +'
       implementation 'com.android.support:recyclerview-v7:+'
}
Copy the code

V4 package is set to the main version 22, small version is good 2, the patch number to use the latest; The v7 package is under the major version number 22, the minor version is the latest; The recycleView package for V7 is the latest available in the repository, and there is no major version limit

It is important to note that when using dynamic version numbers, it may cause Gradle to obtain dependency packages unstable and cause build failure. More serious, it may cause gradle to obtain different versions in the local cache and cause application call API disorder. Androidstudio will also prompt when using dynamic version numbers

conclusion

This section mainly explains how to add dependencies to Gradle package, repository and Libray Project, and also explains some concepts of code keywords in dependencies.