為什么要統(tǒng)一配置Gradle
最近在嘗試模塊化開發(fā)昔搂,當(dāng)我創(chuàng)建到第二個module時,就突然一陣心慌输拇,gradle這樣配置下去后邊怎么維護啊摘符,難道改個buildToolsVersion
、buildToolsVersion
什么的要到每個module里的 build.gradle文件里改一遍策吠?
先看一下正常的build.gradle配置:
里面
minSdkVersion
逛裤、compileSdkVersion
、依賴庫版本
...很多配置都是每個module必備的猴抹,肯定有辦法動一動手指就可以將每個module里的環(huán)境配置改掉了...ok带族,下邊進入裝逼時刻,三步教你統(tǒng)一gradle配置蟀给。
1.創(chuàng)建config.gradle文件
在project根目錄下創(chuàng)建一個config.gradle文件
def tinkerVersion = "1.7.7"
def supportLibraryVersion = "25.2.0"
def frescoVersion = "1.2.0"
def butterknifeVersion = "8.4.0"
ext {
android = [
buildToolsVersion: "25.0.2",
compileSdkVersion: 25,
minSdkVersion : 15,
targetSdkVersion : 25,
versionCode : 47,
versionName : "1.3.6"
]
dependencies = ["appcompat-v7" : "com.android.support:appcompat-v7:${supportLibraryVersion}",
"support-v4" : "com.android.support:support-v4:${supportLibraryVersion}",
"support-v13" : "com.android.support:support-v13:${supportLibraryVersion}",
"recyclerview-v7" : "com.android.support:recyclerview-v7:${supportLibraryVersion}",
"design" : "com.android.support:design:${supportLibraryVersion}",
"cardview-v7" : "com.android.support:cardview-v7:${supportLibraryVersion}",
"support-vector-drawable" : "com.android.support:support-vector-drawable:${supportLibraryVersion}",
"junit" : 'junit:junit:4.12',
"tinker-android-anno" : "com.tencent.tinker:tinker-android-anno:${tinkerVersion}",
"tinker-android-lib" : "com.tencent.tinker:tinker-android-lib:${tinkerVersion}",
"fresco" : "com.facebook.fresco:fresco:${frescoVersion}",
"fresco-animated-gif" : "com.facebook.fresco:animated-gif:${frescoVersion}",
"fresco-animated-webp" : "com.facebook.fresco:animated-webp:${frescoVersion}",
"fresco-webpsupport" : "com.facebook.fresco:webpsupport:${frescoVersion}",
"fresco-animated-base-support": "com.facebook.fresco:animated-base-support:${frescoVersion}",
"butterknife" : "com.jakewharton:butterknife:${butterknifeVersion}",
"butterknife-compiler" : "com.jakewharton:butterknife-compiler:${butterknifeVersion}",
"gson" : "com.google.code.gson:gson:2.8.0"
]
}
如你所見蝙砌,配置文件主要分sdk版本和依賴庫兩部分阳堕,另外第三方庫的版本也被提了出來,一目了然...
PS:這里dependencies= []
只是起到一個聲明作用择克,并不會將所有的依賴庫打包到apk中恬总,so,在這里你可以聲明所有常用的依賴庫肚邢,各個module只需按需引用即可
2.導(dǎo)入config.gradle
一行代碼apply from: "config.gradle"
將剛才創(chuàng)建的config.gradle導(dǎo)入到project根目錄的build.gradle
apply from: "config.gradle"
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
3.引用
配好矣亦導(dǎo)好矣壹堰,你可以在各個module里引用你的配置了。
聲明兩個變量骡湖,將配置引用進來
apply plugin: 'com.android.library'
def cfg = rootProject.ext.android // 工程配置
def libs = rootProject.ext.dependencies // 庫依賴
配置version
android {
compileSdkVersion cfg.compileSdkVersion
buildToolsVersion cfg.buildToolsVersion
defaultConfig {
minSdkVersion cfg.minSdkVersion
targetSdkVersion cfg.targetSdkVersion
versionCode cfg.versionCode
versionName cfg.versionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
按需添加依賴庫
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile libs["junit"]
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile libs["fresco"]
compile libs["fresco-animated-gif"]
compile libs["gson"]
compile libs["appcompat-v7"]
compile libs["design"]
compile libs["recyclerview-v7"]
compile libs["support-v4"]
compile libs["cardview-v7"]
}
應(yīng)該不用過多解釋吧贱纠,相信你們已經(jīng)秒懂...
總結(jié)
后來事實證明我的心慌還是很及時的
config.gradle
文件)谆焊,還可以避免因為第三方庫版本不統(tǒng)一引起的編譯錯誤。