One, foreword

Gradle dependencies are often used in Android development. Here is a summary of Gradle dependency management. Gradle finds dependencies from the repository and uses Gradle keywords. Each module may depend on other modules, and dependencies between modules can be viewed using the command line gradle Dependencies.

Find dependencies from the repository

Gradle relies on the following rules to find the repository: First you need to determine the type of repository, whether it is Maven or Ivy. If it is a Maven repository, it will look for.pom files. If it is an Ivy repository, you will look for the Ivy.xml file.

Note: Starting with Gradle 5.0,.module files will be found first. Before Gradle 5.0, if the above file does not exist, the module file (e.g. Xxx.jar) is directly looked for. After Gradle 5.0, module files are not found.

Gradle keyword

1. Transitive keyword

The Transitive keyword is used to process child dependencies and defaults to true. When true, Gradle automatically adds child dependencies to form a multi-layer tree. When false, each dependency needs to be added manually.

We can uniformly specify the transitive to false, as shown below:

configurations.all {
   transitive = false
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com. Android. Support: appcompat - v7:27.1.0'
    compile 'com. Android. Support. The constraint, the constraint - layout: 1.0.2'
    testCompile 'junit: junit: 4.12'
    androidTestCompile 'com. Android. Support. Test: runner: 1.0.1'
    androidTestCompile 'com. Android. Support. Test. Espresso: espresso - core: 3.0.1'
}
Copy the code

You can also specify a single transitive, as shown below:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com. Android. Support: appcompat - v7:27.1.0'
    compile 'com. Android. Support. The constraint, the constraint - layout: 1.0.2'
    testCompile 'junit: junit: 4.12'
    androidTestCompile 'com. Android. Support. Test: runner: 1.0.1'
    androidTestCompile ('com. Android. Support. Test. Espresso: espresso - core: 3.0.1') {
        transitive = false}}Copy the code

Provided provided keyword

Provided means to provide this package during compilation so that compilation is error-free. But it doesn’t actually have this package in its reference dependency, so you need to provide a reference to this package in the app, otherwise you’ll get an error running the app. For example (quoted from http://www.jianshu.com/p/8ae94b825740) :

A, B, and C are libraries. A depends on C, and B depends on C. The App needs to use both A and B, so A (or B) can change its dependency with C to ProvidedCopy the code

A Library actually uses C, but it knows that B has A C there too, so it’s unnecessary to carry another one. When the app starts running, A can get C from B, which means that both libraries share the same C. So just before he joins up with B, let’s say he has C. If you run without C, you’re going to crash.

So a Provided is an indirect dependent Library that must be run with or it will crash. Using a Provided helps to avoid relying on duplicate resources.

Provided can be combined with the @jar keyword to provide a JAR package. Provided can be combined with the @AAR keyword to provide an AAR package.

A provided can also be invoked without explicitly specifying the keyword by providing Group :module (note that it is not followed by @jar or @aar). Locate the. Module,. Pom, and.Ivy files, and then search for the jar or AAR based on the reference (the strategy for finding is not yet clear).

The Provided can also be combined with files to reference a local JAR package. For example, if you have a jar package named test.jar under your project’s libs, you can reference provided files(‘libs/test.jar’) like this. The JAR package is then used for compilation only and does not add dependencies to the library.

2, compile keyword

Compile and Provided are used the same way, but the main difference is that compile writes the component’s reference dependencies to.module,.pom, and.Ivy files, and then uses the reference dependency versions when compiled or run.

@jar

Generally, @jar can be used together with compile and provided. For example, compile Group :module@jar and Provided Group :module@jar indicate that dependencies are used only as JAR packages.

@aar

Aar is a compressed package with resources that can be used in conjunction with Compile and Provided, such as compile Group :module@aar and Provided Group :module@aar.

Adding an AAR means that only the group: Module component is referenced, and no other components referenced by the Group: Module are referenced. For example, if a third-party lib is referenced in an AAR, then the aar does not include these libraries in the aar’s reference dependencies, so if the app references the aar as well as the required third-party libraries, otherwise an error will be reported.