統一第三方庫的版本:
參考: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)
}
}
}
統一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}")
}
}