Gradle依赖库及配置统一管理
2019-09-17 本文已影响0人
itfitness
目录
为何要统一管理
我们的APP Module可能依赖的很多的本地Library Module,如果我们需要修改compileSdkVersion等属性的时候,就需要挨个Module去修改,而如果进行统一的版本管理则会方便很多。
配置的统一管理
1. 新建common.gradle
2. 编写统一的属性值
ext {
android = [
compileSdkVersion: 29,
defaultConfig : [
applicationId : "com.itfitness.gradletest",
minSdkVersion : 15,
targetSdkVersion : 29,
versionCode : 1,
versionName : "1.0",
testInstrumentationRunner: "androidx.test.runner.AndroidJUnitRunner",
]
]
}
3. RootProject中应用common.gradle
4. 在Module的gradle文件中引用值
android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
defaultConfig {
applicationId rootProject.ext.android.defaultConfig.applicationId
minSdkVersion rootProject.ext.android.defaultConfig.minSdkVersion
targetSdkVersion rootProject.ext.android.defaultConfig.targetSdkVersion
versionCode rootProject.ext.android.defaultConfig.versionCode
versionName rootProject.ext.android.defaultConfig.versionName
testInstrumentationRunner rootProject.ext.android.defaultConfig.testInstrumentationRunner
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
依赖库的统一管理
操作与配置统一管理类似。
1. common.gradle中添加依赖库配置
ext {
android = [
compileSdkVersion: 29,
defaultConfig : [
applicationId : "com.itfitness.gradletest",
minSdkVersion : 15,
targetSdkVersion : 29,
versionCode : 1,
versionName : "1.0",
testInstrumentationRunner: "androidx.test.runner.AndroidJUnitRunner",
]
]
//依赖库配置
dependencies = [
app: [
implementation: [
'androidx.appcompat:appcompat:1.1.0',//库
'androidx.constraintlayout:constraintlayout:1.1.3'
],
androidTestImplementation:[
'androidx.test:runner:1.2.0',
'androidx.test.espresso:espresso-core:3.2.0'
],
testImplementation:[
'junit:junit:4.12'
]
]
]
}
2. 在Module中引入依赖库
为了方便,这里我使用循环遍历引入
apply plugin: 'com.android.application'
android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
defaultConfig {
applicationId rootProject.ext.android.defaultConfig.applicationId
minSdkVersion rootProject.ext.android.defaultConfig.minSdkVersion
targetSdkVersion rootProject.ext.android.defaultConfig.targetSdkVersion
versionCode rootProject.ext.android.defaultConfig.versionCode
versionName rootProject.ext.android.defaultConfig.versionName
testInstrumentationRunner rootProject.ext.android.defaultConfig.testInstrumentationRunner
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
//依赖引入
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//通过循环common.gradle中对应的扩展属性来进行依赖引入
rootProject.ext.dependencies.app.implementation.each{
implementation it
}
rootProject.ext.dependencies.app.androidTestImplementation.each{
androidTestImplementation it
}
rootProject.ext.dependencies.app.testImplementation.each{
testImplementation it
}
//这下面的代码是为了引入本地所有的Library Module
getParent().getSubprojects().each{Project p->
if(p.name != "app"){
implementation project(path: ":${p.name}")
}
}
}