gradle解决包冲突笔记

3,786 阅读2分钟

转载:androidstudio gradle 解决包冲突的方法

1.exclude
// 依据构件名称排除
exclude module: 'edf'
// 依据组织名称排除
exclude group: 'com.adc'
// 依据组织名称 + 构件名称排除
exclude group: 'com.abc', module: 'edf'
2.单独去掉某一个依赖的冲突
去除com.github.niorgai:StatusBarCompat:2.1.3引用的com.android.support包下内容
compile('com.github.niorgai:StatusBarCompat:2.1.3') {
    exclude group: 'com.android.support'
})

去除module引用的'com.google.code.findbugs:jar305'的相关内容
compile(project(':downlibrary')) {
    exclude group: 'com.google.code.findbugs', module: 'jsr305'
}
3.去掉所有module中指定的依赖
configurations {
    //编译期排除commons模块
    compile.exclude module: 'commons'
    //在整个构建过程中排除pkaq.tiger: share
    all*.exclude group: 'pkaq.tiger', module: 'share'
}
4.configuration与configurations.all的区别
5.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')
}

使用./gradlew 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
5.1 统一指定transitive
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')
}

使用./gradlew dependencies命令查看

+--- com.android.support.test:runner:0.2
+--- com.android.support.test:rules:0.2
\--- com.android.support.test.espresso:espresso-core:2.1
5.2 单独指定依赖项的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
   }
}
6.强制依赖某个版本, 包依赖冲突的场景
configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '26.1.0'
            }
        }
    }
}

使用强制版本
  当然,有时候你可能仅仅是需要强制使用某个统一的依赖版本,而不是排除他们,
  那么此时force就该登场了。指定force = true属性可以冲突时优先使用该版本进行解决。
compile('org.hibernate:hibernate:3.1') {
    force = true
}

全局配置强制使用某个版本的依赖来解决依赖冲突中出现的依赖
configurations.all {
   resolutionStrategy {
       force 'org.hamcrest:hamcrest-core:1.3'
   }
}
7.综合案例
compile('org.hibernate:hibernate:3.1') {
    //冲突时优先使用该版本
    force = true
    //依据构建名称排除
    exclude module: 'cglib' 
    //依据组织名称排除
    exclude group: 'org.jmock' 
    //依据组织名称+构件名称排除
    exclude group: 'org.unwanted', module: 'iAmBuggy' 
    //为本依赖关闭依赖传递特性
    transitive = false
}