版本冲突有效的解决方案

有时项目中依赖过多的版本支持库,model,及组件化等…然而如果没有一个较好的版本控制,因为AndroidStudio不允许有多版本的存在,因而避免不了的会出现版本冲突的问题,这节我们就来谈一谈版本冲突的问题。

//步骤一:
//解决支持库冲突 同一版本(很重要)
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'
            }
        }
    }
}
//步骤二:
//解决部分依赖冲突:比如腾讯云IM
//去除冲突的版本,也就是你不想保留的版本
implementation ('com.tencent.imsdk:tuikit:4.7.10') {
    exclude group: 'com.android.support:appcompat-v7'
    exclude group: 'com.android.support:design:'
    exclude group: 'com.android.support:support-v4'
    exclude group: 'com.android.support:recyclerview-v7'
    exclude group: 'com.android.support:support-annotations'
    exclude group: 'com.github.bumptech.glide'
    exclude group: 'com.google.code.gson:gson'
}
//如果步骤二未解决某些版本依赖
//步骤三 强制使用此版本依赖
configurations.all {
    exclude module: 'gson'
    resolutionStrategy {
        force 'com.android.support:support-v4:27.1.1'
        force 'com.android.support:design:27.1.1'
        force 'com.google.code.gson:gson:2.8.5'
    }
}
//步骤四
//在模块中可以使用 compileOnly 替换 implementation
//只在此模块编译过程中有效

最有效的方式是:使用相同的版本号来控制版本来规避版本冲突的问题

在project build.gradle里添加:

ext {
    compileSdkVersion = 28
    buildToolsVersion = '28.0.3'
    minSdkVersion = 21
    targetSdkVersion = 28
    androidXVersion = '1.0.0'
}

用法:

compileSdkVersion rootProject.compileSdkVersion
buildToolsVersion rootProject.buildToolsVersion
defaultConfig {
    minSdkVersion rootProject.minSdkVersion
    targetSdkVersion rootProject.targetSdkVersion
    versionCode 1
    versionName "1.0"
}

implementation "com.google.android.material:material:$rootProject.androidXVersion"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值