统一第三方库的版本:
参考: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}")
}
}