爱上AndroidAndroid开发经验谈Android开发

Gradle依赖关键字学习

2018-04-03  本文已影响212人  一只好奇的茂

查看依赖树

 ./gradlew :app:dependencies 

总共有releaseUnitTestCompileClasspath、releaseCompileClasspath、debugCompileClasspath等依赖树,直接用上面这条命令的话,会列举出所有的,不利于查看,可以使用类似以下命令,

./gradlew :app:dependencies --configuration debugCompileClasspath

Transitive

Transitive用于自动处理子依赖项。默认为true,gradle自动添加子依赖项,形成一个多层树形结构;设置为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')
}

运行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
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
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

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
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
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。

provided files('libs/glide-3.7.0.jar')
上一篇 下一篇

猜你喜欢

热点阅读