一.根目录下定义config.gradle
ext {
androidConfig = [
compileSdkVersion = 30,
buildToolsVersion = "23.0.2",
minSdkVersion = 26,
targetSdkVersion = 30,
//项目版本3
versionX = 3,
//项目需求版本4期
versionY = 4,
//小版本更新,每次发布需要+1
versionZ = 0,
versionCode = versionCode(versionX,versionY,versionZ),
versionName = versionName(versionX,versionY,versionZ),
]
dependencies = [
appcompatV7 : 'com.android.support:appcompat-v7:30+',
]
}
static def versionCode(x,y,z) {
return x * 10000000 +y * 1000000 + z
}
static def versionName(x,y,z) {
return x+"."+y+"."+z
}
2.使用配置文件
1.先引入配置文件
apply from: "../config.gradle"
2.调用变量
android {
compileSdkVersion rootProject.ext.compileSdkVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode rootProject.ext.versionCode
versionName rootProject.ext.versionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}