gradle:统一版本 /依赖冲突

统一第三方库的版本:

参考:Android组件化开发实践(十):通过Gradle插件统一规范

  • 示例一:

configurations.all {
    resolutionStrategy {
        force 'com.github.bumptech.glide:glide:4.2.0'
        force 'com.github.bumptech.glide:compiler:4.2.0'
    }
}

  • 示例二:

def SUPPORT_VERSION = "26.1.0"
def MULTIDEX_VERSION = "1.0.2"
def GSON_VERSION = "2.8.0"
def KOTLIN_VERSION = "1.3.40"

ConfigurationContainer container = project.configurations
container.all { Configuration conf ->
    ResolutionStrategy rs = conf.resolutionStrategy
    rs.force 'com.google.code.findbugs:jsr305:2.0.1'
    //统一第三方库的版本号
    rs.eachDependency { details ->
        def requested = details.requested
        if (requested.group == "com.android.support") {
            //强制所有的 com.android.support 库采用固定版本
            if (requested.name.startsWith("multidex")) {
                details.useVersion(MULTIDEX_VERSION)
            } else {
                details.useVersion(SUPPORT_VERSION)
            }
        } else if (requested.group == "com.google.code.gson") {
            //统一 Gson 库的版本号
            details.useVersion(GSON_VERSION)
        } else if (requested.group == "org.jetbrains.kotlin") {
            //统一内部 kotlin 库的版本
            details.useVersion(KOTLIN_VERSION)
        }
    }
}

示例三: 第三方依赖冲突

1、移除某个第三方的依赖

debugImplementation ('me.ele:uetool:1.0.15'){
    exclude group: 'com.android.support', module: 'support-v7'
}

debugImplementation ('me.ele:uetool:1.0.15'){
    exclude group: 'com.android.support'
}

2、更加方便的方式

有时候,乱七八糟的依赖过多,可以使用如下方案:

在 app的module中:

android{
    configurations.all {
         resolutionStrategy.eachDependency { DependencyResolveDetails details ->
             def requested = details.requested
             if (requested.group == 'com.android.support') {
                 if (requested.name.startsWith("appcompat-v7")) {
                     details.useVersion '25.3.0'
                 }
                 if (requested.name.startsWith("appcompat-v4")) {
                     details.useVersion '25.3.0'
                 }
    
                 if (requested.name.startsWith("recyclerview-v7")) {
                     details.useVersion '25.3.0'
                 }
             }
         }
     }
}

这样可以强制使用某个版本,不用再一个个去过滤了。

统一sdk版本


def MIN_SDK = 19
def TARGET_SDK = 26
def COMPILE_SDK = "android-26"

project.afterEvaluate {
    def android = project.extensions.getByName("android")

    //强制统一 compileSdkVersion、 minSdkVersion、targetSdkVersion
    String compileSdkVersion = android.compileSdkVersion
    int targetSdkVersion = android.defaultConfig.targetSdkVersion.apiLevel
    int minSdkVersion = android.defaultConfig.minSdkVersion.apiLevel
    if (compileSdkVersion != COMPILE_SDK) {
        throw new GradleException("compileSdkVersion , must be  ${COMPILE_SDK}")
    }
    if (minSdkVersion != MIN_SDK) {
        throw new GradleException("minSdkVersion , must be ${MIN_SDK}")
    }
    if (targetSdkVersion != TARGET_SDK) {
        throw new GradleException("targetSdkVersion , must be  ${TARGET_SDK}")
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值