Gradle可以把module的Android、dependence等各种配置提取到一个单独的文件统一管理,非常方便。步骤如下:
- 在和项目build.gradle相同的目录下新建一个gradle文件,例如common.gradle
ext {
android = [
applicationId : "com.example.gradledemo",
minSdkVersion : 21,
compileSdkVersion: 28,
targetSdkVersion : 28,
versionCode : 1,
versionName : "1.0",
]
signConfig = [
'storeFile' : 'xxx.jks',
'storePassword': '123',
'keyAlias' : 'Zxz',
'keyPassword' : '123',
]
dependence = [
'supportV7': 'com.android.support:appcompat-v7:28.0.0'
]
}
- 在项目的build.gradle中引入改文件
apply from: file('common.gradle')
- 在module的build.gradle中使用ext属性
android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
defaultConfig {
applicationId rootProject.ext.android.applicationId
minSdkVersion rootProject.ext.android.minSdkVersion
targetSdkVersion rootProject.ext.android.targetSdkVersion
versionCode rootProject.ext.android.versionCode
versionName rootProject.ext.android.versionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
//...
}
dependencies {
implementation rootProject.ext.dependence.supportV7
//...