Gradle builds dependencies on Android. Gradle builds dependencies on Android and builds dependencies on Android. Gradle builds dependencies on Android and builds dependencies on Android.

Depend on the type

The Dependencies DSL tag is part of the standard Gradle API and is not a feature of the Android Gradle plug-in, so it does not belong to the Android tag.

Dependencies come in three ways, as shown in the following example:

apply plugin: 'com.android.application'

android { ... }

dependencies {
    // Dependency on a local library module
    implementation project(":mylibrary")

    // Dependency on local binaries
    implementation fileTree(dir: 'libs'.include: ['*.jar'])

    // Dependency on a remote binary
    implementation 'com. The example. The android: app - magic: 12.3'
}
Copy the code

1. Local Library module dependencies

implementation project(":mylibrary")
Copy the code

This dependency depends directly on the local library project code (note that the name of myLibrary must match the module name defined under the include tag in settings.gradle).

2. Local binary dependency

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

This dependency depends on Jar files in the module_name/libs/ directory in the project (note that Gradle is read relative to build. Gradle).

If you only want to rely on a single specific local binary library, you can configure it as follows:

implementation files('libs/foo.jar'.'libs/bar.jar')
Copy the code

3. Remote binary dependencies

implementation 'com. The example. The android: app - magic: 12.3'
Copy the code

Here is the shorthand. The dependency is written as follows:

implementation group: 'com.example.android', name: 'app-magic', version: '12.3'
Copy the code

Group, name, and Version together locate a remote dependent library. It is important to note that version should not be written as “12.3+” unless it is clearly expected, as unexpected version updates can cause build problems. Remote dependencies need to declare remote repositories, such as JCenter (), Google (), Maven repositories, and so on, under the repositories TAB.

Depend on the configuration

Gradle versions now support implementation, API, compileOnly, runtimeOnly, and annotationProcessor dependencies. Compile, provided, APK, providedCompile. Depend on the configuration also can add some configuration items, such as AndroidTestImplementation, debugApi and so on.

The three dependency configurations are implementation, API, and compileOnly. The meanings are as follows:

implementation

As with compile, dependencies are added to the compile path and packaged into the output (AAR or APK), but the implementation of the dependency is not exposed to other modules at compile time, meaning that the implementation in the dependency is only accessible to other modules at run time. Using this configuration can significantly improve build times because it reduces the number of modules that need to be recompiled. It is recommended that you use this dependency configuration whenever possible.

api

The dependency is added to the compile path and packaged into the output (AAR or APK). Unlike implementation, this dependency can be passed. Other modules can access the implementation of this dependency both at compile time and at run time. That is, it leaks implementations that should not be unused. For example, A depends on B, and B depends on C. If the API configuration is used, A can directly use C classes (compile time and run time). If the implementation configuration is used, A cannot access C classes at compile time.

compileOnly

In contrast to provided, Gradle adds dependencies to the compile path, which are used at compile time and are not packaged into the output (AAR or APK). This reduces the size of the output, which is useful in cases where it is only needed at compile time and optional at run time.

runtimeOnly

In contrast to APK, Gradle adds dependencies that are only packaged into APK and used at runtime, but not added to the compile path. This is not used.

annotationProcessor

Compile corresponds to the dependency configuration of the annotation processor, which is not used.

Viewing the dependency tree

You can view the dependencies of a single module or project by running the dependent Gradle task as follows:

View -> Tools Windows -> Gradle

2. Expand AppName -> Tasks -> Android and double-click AndroidDependencies. After running, the dependency tree will be displayed in the Run window.

Dependent conflict resolution

As many dependencies are added to the project, it is inevitable that there will be dependency conflicts. How to resolve them?

Positioning conflict

A dependency conflict may result in an error like the following:

Program type already present com.example.MyClass
Copy the code

By searching for classes (Command + O), the conflicting dependencies are located and excluded.

How to eliminate dependencies

1. Exclude from Dependencies (fine granularity)

compile('com. Taobao. Android: accs - huawei: 1.1.2 @ aar') {
        transitive = true
        exclude group: 'com.taobao.android', module: 'accs_sdk_taobao'
}
Copy the code

2. Global configuration is excluded

configurations {
    compile.exclude module: 'cglib'// Exclude all*. Exclude group:'com.taobao.android', module: 'tnet-jni'
    all*.exclude group: 'com.taobao.android', module: 'tnet-so'
}
Copy the code

3. Disable dependency passing

compile('com. Zhyea: ar4j: 1.0') {
    transitive = false
}

configurations.all {
    transitive = false
}
Copy the code

You can also omit passing dependencies in a single dependency using the @JAR identifier:

compile 'com. Zhyea: ar4j: 1.0 @ jar'
Copy the code

4. Force a version

If a dependency is required and a dependency conflict exists, the force attribute can be used to indicate the need for dependency unification. This can also be configured globally:

compile('com. Zhyea: ar4j: 1.0') {
    force = true
}

configurations.all {
    resolutionStrategy {
        force 'org. Hamcrest: hamcrest - core: 1.3'}}Copy the code

5. Eliminate dependencies when packaging

Let’s start with an example:

task zip(type: Zip) {
    into('lib') {
        from(configurations.runtime) {
            exclude '*unwanted*'.'*log*'
        }
    }
    into(' ') {
        from jar
        from 'doc'}}Copy the code

The code indicates that the NEXT ZIP package will filter out the JAR that contains the “log” and “name”. The call to exclude takes a different argument than the previous example, which takes a map structure, and a regular expression string.

You can also choose to package only some needed dependencies by calling the include method at package time:

task zip(type: Zip) {
    into('lib') {
        from(configurations.runtime) {
            include '*ar4j*'.'*spring*'
        }
    }
    into(' ') {
        from jar
        from 'doc'}}Copy the code

Exclude in Dependencies and global configuration.





reference