Gradle builds dependency configuration instructions

First issue of this article: http://yuweiguocn.github.io/

This article introduces Gradle build dependency configuration instructions in Android Studio and how to view dependency trees.

Baidi Xiajiang Ling
Among the clouds of Emperor Bai, Qianli Jiangling returned one day.
The boat passed thousands of mountains with apes shouting without endless.
- Tang, Li Bai

Dependency type

1. Local module class library dependencies

compile project(':mylibrary')

2. Local binary dependencies

//Add all jar packages under the libs folder as dependencies
compile fileTree(dir: 'libs', include: ['*.jar'])
//Specify jar packages as dependencies
compile files('libs/foo.jar', 'libs/bar.jar')

3. Remote binary dependency

compile 'com.example.android:app-magic:12.3'

//or
compile group: 'com.example.android', name: 'app-magic', version: '12.3'

Note that remote dependencies require adding appropriate remote warehouses.

Class library dependency configuration

In dependency blocks, you can declare library dependencies using different dependency configurations.

  • Compoile: gradle adds dependencies to compile paths and packages them into APK.
  • Apk: gradle adds dependencies only packaged into APK (not added to the compilation path).
  • provided: gradle adds dependencies to the compilation path (not packaged into APK).

Note: In app module, you can't add AAR dependencies using provided, you can only add JAR packages. In library module, you can use it to add AAR and JAR dependencies.

The above configuration applies to the main source set of the project and will be applied to all build variants. If you only want to declare dependencies on the specified build variant source set or test source set, you need to prefix it with the configuration name and the name of the build variant or test source set.

For example, you just want to add compile dependencies to the "free" product flavor:

dependencies {
    freeCompile 'com.google.firebase:firebase-ads:9.8.0'
}

If you want to add a dependency to the product flavor and build type variants, you need to initialize the configuration name in the configuration block. The following example illustrates adding an apk dependency to the "free Debug" build variant:

configurations {
    // Initializes a placeholder for the freeDebugApk dependency configuration.
    freeDebugApk {}
}

dependencies {
    freeDebugApk fileTree(dir: 'libs', include: ['*.jar'])
}

Add compile dependencies for local and device tests:

dependencies {
    // Adds a remote binary dependency only for local tests.
    testCompile 'junit:junit:4.12'

    // Adds a remote binary dependency only for the instrumented test APK.
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}

If your library module provides multiple variants, you can add different library variants to different app variants:

dependencies {
  // Adds the 'debug' varaint of the library to the debug varaint of the app
  debugCompile project(path: ':my-library-module', configuration: 'debug')

  // Adds the 'release' varaint of the library to the release varaint of the app
  releaseCompile project(path: ':my-library-module', configuration: 'release')
}

Remote Warehouse

By default, Android Studio declares JCenter as the warehouse location in the build.gradle file in the project root directory:

allprojects {
    repositories {
        jcenter()
    }
}

Add maven Central, maven Local, or the specified Maven repository:

allprojects {
    repositories {
        jcenter()
        mavenCentral()
        mavenLocal()
        maven {
            url "https://repo.example.com/maven2"
        }
        maven {
            url "file://local/repo/"
        }
        ivy {
            url "https://repo.example.com/ivy"
        }
    }
}

Google's Maven Warehouse

The following latest version of the Android class library can be obtained from Google's Maven repository:

Add google's Maven repository to the build. grade file under module:

repositories {
    maven {
        url 'https://maven.google.com'
        // Another available URL is'https://dl.google.com/dl/android/maven2/'
    }
}

Then add the class library you want to dependencies block:

dependencies {
    compile 'com.android.support:appcompat-v7:26.0.0-beta2'
}

Note: If you add an old version of the class library and the dependency fails, it means that the Maven repository is not available and can only be retrieved from the off-line repository (SDK Manager).

Dependency order

The order of dependencies in the dependencies list represents the priority of each, with the first priority being the highest and then the next ranking. The dependency order of class libraries is related Resource consolidation and Merge of List File Elements.

For example, LIB_A and LIB_B dependencies are added, LIB_A dependencies on LIB_C and LIB_D, and LIB_B dependencies on LIB_C. Then the order of dependencies will be:

1.LIB_A
2.LIB_D
3.LIB_B
4.LIB_C

This ensures that both LIB_A and LIB_B can cover LIB_C. LIB_D has a higher priority than LIB_B because LIB_A has a higher priority than LIB_B.

View the dependency tree

Some direct dependencies may have their own dependencies. This is called dependency passing. We can find Android Dependencies double-click execution from the task list.
1. Select View > Tool Windows > Gradle.
2. Expand AppName > Tasks > Android and double-click Android Dependencies.
3. View the report and select View > Tool Windows > Gradle Console.

Below is an example of building a variant dependency tree for debug, including local module dependencies and remote dependencies:

Executing tasks: [androidDependencies]
:app:androidDependencies
debug
/**
 * Both the library module dependency and remote binary dependency are listed
 * with their transitive dependencies.
 */
+--- MyApp:mylibrary:unspecified
|    \--- com.android.support:appcompat-v7:25.4.0
|         +--- com.android.support:animated-vector-drawable:25.4.0
|         |    \--- com.android.support:support-vector-drawable:25.4.0
|         |         \--- com.android.support:support-v4:25.4.0
|         |              \--- LOCAL: internal_impl-25.4.0.jar
|         +--- com.android.support:support-v4:25.4.0
|         |    \--- LOCAL: internal_impl-25.4.0.jar
|         \--- com.android.support:support-vector-drawable:25.4.0
|              \--- com.android.support:support-v4:25.4.0
|                   \--- LOCAL: internal_impl-25.4.0.jar
\--- com.android.support:appcompat-v7:25.4.0
     +--- com.android.support:animated-vector-drawable:25.4.0
     |    \--- com.android.support:support-vector-drawable:25.4.0
     |         \--- com.android.support:support-v4:25.4.0
     |              \--- LOCAL: internal_impl-25.4.0.jar
     +--- com.android.support:support-v4:25.4.0
     |    \--- LOCAL: internal_impl-25.4.0.jar
     \--- com.android.support:support-vector-drawable:25.4.0
          \--- com.android.support:support-v4:25.4.0
               \--- LOCAL: internal_impl-25.4.0.jar
...

Reference resources

Keywords: Android Maven Gradle Google

Added by lordrain11 on Tue, 11 Jun 2019 00:02:15 +0300