Gradle的使用
Android的版本统一管理
- 1.新建文件config.gradle,并配置相关属性
- 2.在项目的build.gradle中引入
apply from : 'config.gradle'
- 3.App的build.gradle中通过调用this.rootProject调用配置的属性
举个例子
ext {
// 抽取出公共项,定义key-value map
app_android = [
compileSdkVersion : 30,
buildToolsVersion : "30.0.3",
minSdkVersion : 23,
targetSdkVersion : 30,
]
// 依赖相关的内容
app_impl = [
"appcompat": 'androidx.appcompat:appcompat:1.2.0',
"material": 'com.google.android.material:material:1.3.0',
"junit": 'junit:junit:4.13.2',
"androidx_junit": 'androidx.test.ext:junit:1.1.2',
"androidx_espresso": 'androidx.test.espresso:espresso-core:3.3.0'
]
// 编译相关的内容
app_compile = [
sourceCompatibility: JavaVersion.VERSION_1_8,
targetCompatibility: JavaVersion.VERSION_1_8,
]
// debug/release模式下的server_url
app_server_url = [
"debug": "http://test.xxx.com/xxx",
"release": "http://product.xxx.com/xxx"
]
}
app的build.gradle中
android {
compileSdkVersion this.rootProject.app_android.compileSdkVersion
buildToolsVersion app_android.buildToolsVersion
defaultConfig {
applicationId "com.tal.learn_gradle"
minSdkVersion app_android.minSdkVersion
targetSdkVersion app_android.targetSdkVersion
versionCode 1
versionName '1.0'
testInstrumentationRunner app_android.testInstrumentationRunner
}
buildTypes {
debug {
// 测试环境用debug_url
buildConfigField("String", "SERVER_URL", "\"${app_server_url.debug}\"")
}
release {
// 正式环境用release_url
buildConfigField("String", "SERVER_URL", "\"${app_server_url.release}\"")
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility app_compile.sourceCompatibility
targetCompatibility app_compile.targetCompatibility
}
}
dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
// 更简化依赖库的方式
app_impl.each {
k, v -> implementation v
}
}
编译产生的BuildConfig中可以进行根据打包类型更改SERVER_URL