查看依赖树
- 查看所有依赖树:
./gradlew :app:dependencies
总共有releaseUnitTestCompileClasspath、releaseCompileClasspath、debugCompileClasspath等依赖树,直接用上面这条命令的话,会列举出所有的,不利于查看,可以使用类似以下命令,
- 查看目标依赖树:
./gradlew :app:dependencies --configuration debugCompileClasspath
Transitive
Transitive用于自动处理子依赖项。默认为true,gradle自动添加子依赖项,形成一个多层树形结构;设置为false,则需要手动添加每个依赖项。
-
案例
以安卓单元测试espresso的配置为例,gradle依赖如下:
dependencies {
androidTestCompile('com.android.support.test:runner:0.2')
androidTestCompile('com.android.support.test:rules:0.2')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
}
运行gradle dependencies的结果如下。可以看到每个包的依赖项都被递归分析并添加进来。
+--- com.android.support.test:runner:0.2
| +--- junit:junit-dep:4.10
| | \--- org.hamcrest:hamcrest-core:1.1
| +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
| \--- com.android.support:support-annotations:22.0.0
+--- com.android.support.test:rules:0.2
| \--- com.android.support.test:runner:0.2 (*)
\--- com.android.support.test.espresso:espresso-core:2.1
+--- com.android.support.test:rules:0.2 (*)
+--- com.squareup:javawriter:2.1.1
+--- org.hamcrest:hamcrest-integration:1.1
| \--- org.hamcrest:hamcrest-core:1.1
+--- com.android.support.test.espresso:espresso-idling-resource:2.1
+--- org.hamcrest:hamcrest-library:1.1
| \--- org.hamcrest:hamcrest-core:1.1
+--- javax.inject:javax.inject:1
+--- com.google.code.findbugs:jsr305:2.0.1
+--- com.android.support.test:runner:0.2 (*)
+--- javax.annotation:javax.annotation-api:1.2
\--- org.hamcrest:hamcrest-core:1.1
-
统一指定transitive
可以给dependencies统一指定transitive为false,再次执行dependencies可以看到如下结果。
configurations.all {
transitive = false
}
dependencies {
androidTestCompile('com.android.support.test:runner:0.2')
androidTestCompile('com.android.support.test:rules:0.2')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
}
+--- com.android.support.test:runner:0.2
+--- com.android.support.test:rules:0.2
\--- com.android.support.test.espresso:espresso-core:2.1
- 单独指定依赖项的transitive
dependencies {
androidTestCompile('com.android.support.test:runner:0.2')
androidTestCompile('com.android.support.test:rules:0.2')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.1') {
transitive = false
}
}
Force
- force强制设置某个模块的版本。
configurations.all {
resolutionStrategy {
force 'org.hamcrest:hamcrest-core:1.3'
}
}
dependencies {
androidTestCompile('com.android.support.test:runner:0.2')
androidTestCompile('com.android.support.test:rules:0.2')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
}
可以看到,原本对hamcrest-core 1.1的依赖,全部变成了1.3。
+--- com.android.support.test:runner:0.2
| +--- junit:junit-dep:4.10
| | \--- org.hamcrest:hamcrest-core:1.1 -> 1.3
| +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
| \--- com.android.support:support-annotations:22.0.0
+--- com.android.support.test:rules:0.2
| \--- com.android.support.test:runner:0.2 (*)
\--- com.android.support.test.espresso:espresso-core:2.1
+--- com.android.support.test:rules:0.2 (*)
+--- com.squareup:javawriter:2.1.1
+--- org.hamcrest:hamcrest-integration:1.1
| \--- org.hamcrest:hamcrest-core:1.1 -> 1.3
+--- com.android.support.test.espresso:espresso-idling-resource:2.1
+--- org.hamcrest:hamcrest-library:1.1
| \--- org.hamcrest:hamcrest-core:1.1 -> 1.3
+--- javax.inject:javax.inject:1
+--- com.google.code.findbugs:jsr305:2.0.1
+--- com.android.support.test:runner:0.2 (*)
+--- javax.annotation:javax.annotation-api:1.2
\--- org.hamcrest:hamcrest-core:1.1 -> 1.3
force=true
compile ('org.greenrobot:eventbus:3.0.0'){
force = true
}
如果主工程和模块中对eventbus都有依赖,且eventbus版本不一致,在主工程中使用以上代码,将坚持使用这个被标注的eventbus版本。(注意:在依赖module中设置该属性,不起作用)
Exclude
Exclude可以设置不编译指定的模块
configurations {
all*.exclude group: 'org.hamcrest', module: 'hamcrest-core'
}
dependencies {
androidTestCompile('com.android.support.test:runner:0.2')
androidTestCompile('com.android.support.test:rules:0.2')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
}
+--- com.android.support.test:runner:0.2
| +--- junit:junit-dep:4.10
| +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
| \--- com.android.support:support-annotations:22.0.0
+--- com.android.support.test:rules:0.2
| \--- com.android.support.test:runner:0.2 (*)
\--- com.android.support.test.espresso:espresso-core:2.1
+--- com.android.support.test:rules:0.2 (*)
+--- com.squareup:javawriter:2.1.1
+--- org.hamcrest:hamcrest-integration:1.1
+--- com.android.support.test.espresso:espresso-idling-resource:2.1
+--- org.hamcrest:hamcrest-library:1.1
+--- javax.inject:javax.inject:1
+--- com.google.code.findbugs:jsr305:2.0.1
+--- com.android.support.test:runner:0.2 (*)
\--- javax.annotation:javax.annotation-api:1.2
-
单独使用group或module参数
exclude后的参数有group和module,可以分别单独使用,会排除所有匹配项。例如下面的脚本匹配了所有的group为’com.android.support.test’的模块。
configurations {
all*.exclude group: 'com.android.support.test'
}
dependencies {
androidTestCompile('com.android.support.test:runner:0.2')
androidTestCompile('com.android.support.test:rules:0.2')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
}
\--- com.android.support.test.espresso:espresso-core:2.1
+--- com.squareup:javawriter:2.1.1
+--- org.hamcrest:hamcrest-integration:1.1
| \--- org.hamcrest:hamcrest-core:1.1
+--- com.android.support.test.espresso:espresso-idling-resource:2.1
+--- org.hamcrest:hamcrest-library:1.1
| \--- org.hamcrest:hamcrest-core:1.1
+--- javax.inject:javax.inject:1
+--- com.google.code.findbugs:jsr305:2.0.1
+--- javax.annotation:javax.annotation-api:1.2
\--- org.hamcrest:hamcrest-core:1.1
- 单独给某个模块指定exclude
dependencies {
androidTestCompile('com.android.support.test:runner:0.2')
androidTestCompile('com.android.support.test:rules:0.2')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.1') {
exclude group: 'org.hamcrest'
}
}
+--- com.android.support.test:runner:0.2
| +--- junit:junit-dep:4.10
| | \--- org.hamcrest:hamcrest-core:1.1
| +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
| \--- com.android.support:support-annotations:22.0.0
+--- com.android.support.test:rules:0.2
| \--- com.android.support.test:runner:0.2 (*)
\--- com.android.support.test.espresso:espresso-core:2.1
+--- com.android.support.test:rules:0.2 (*)
+--- com.squareup:javawriter:2.1.1
+--- com.android.support.test.espresso:espresso-idling-resource:2.1
+--- javax.inject:javax.inject:1
+--- com.google.code.findbugs:jsr305:2.0.1
+--- com.android.support.test:runner:0.2 (*)
\--- javax.annotation:javax.annotation-api:1.2
compile、api、implement和provided
com.android.tools.build:gradle 3.X以后,新增了两个依赖指令,一个是implement 和api。
-
api 指令
完全等同于compile指令。 -
implement指令
这个指令的特点就是,对于使用了该命令编译的依赖,对该项目有依赖的项目将无法访问到使用该命令编译的依赖中的任何程序,也就是将该依赖隐藏在内部,而不对外部公开。 -
用api指令编译,Glide依赖对app Module 是可见的:
-
用implement指令编译依赖对app Module 是不可见的:
-
为什么用implement:
使用 implementation 时,依赖库变动的话只会影响、重新编译到当前库,不会影响到其他,因此编译速度会提高。 -
provided
仅仅在编译时使用,但最终不会被编译到apk或aar里
provided files('libs/glide-3.7.0.jar')