一個(gè)項(xiàng)目(project)文件夾中有多個(gè)工程(module)符衔,這多個(gè)module使用的
compileSdkVersion
buildToolsVersion
minSdkVersion
targetSdkVersion
依賴庫
統(tǒng)一管理其版本號尿招,避免頻繁修改
在module項(xiàng)目里的build.gradle文件中溜腐,通過下面的方式來統(tǒng)一版本
1.$rootProject.supportLibraryVersion
2.$rootProject.ext.junitVersion
舉例
dependencies {
compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
compile "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"
compile "com.android.support:design:$rootProject.supportLibraryVersion"
compile "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"
}
對比
下面是沒有統(tǒng)一管理的依賴
dependencies {
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
testCompile 'junit:junit:4.12'
}
當(dāng)module很多的時(shí)候钱豁,如果沒有統(tǒng)一管理鲫寄,那么將可能導(dǎo)致很多問題:
- A module使用24版本的v7包吉执,B module使用25版本的v7包。當(dāng)另一臺電腦需要運(yùn)行這些module時(shí)而不得不下載24和25兩個(gè)版本的v7包地来,即浪費(fèi)時(shí)間又浪費(fèi)系統(tǒng)資源鼠证;
- 而當(dāng)需要更新依賴庫版本時(shí),必須一個(gè)一個(gè)module去修改靠抑,非常耗時(shí)量九;
廢話少說,下面開始設(shè)置
代碼
在project的build.gradle文件的最底部加上
ext {
// Sdk and tools
minSdkVersion = 15
targetSdkVersion = 25
compileSdkVersion = 25
buildToolsVersion = '25.0.2'
// App dependencies
supportLibraryVersion = '25.1.0'
guavaVersion = '18.0'
junitVersion = '4.12'
mockitoVersion = '1.10.19'
powerMockito = '1.6.2'
hamcrestVersion = '1.3'
runnerVersion = '0.5'
rulesVersion = '0.5'
espressoVersion = '2.2.2'
}
上面的名稱和版本號根據(jù)個(gè)人需要進(jìn)行修改。
在module里的build.gradle里
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
applicationId "你的包名"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
dependencies {
compile (group:'com.android.support',name:'appcompat-v7',version:"$rootProject.supportLibraryVersion")
// App's dependencies, including test
compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
compile "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"
compile "com.android.support:design:$rootProject.supportLibraryVersion"
compile "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"
compile "com.android.support:support-v4:$rootProject.supportLibraryVersion"
compile "com.android.support.test.espresso:espresso-idling-resource:$rootProject.espressoVersion"
compile "com.google.guava:guava:$rootProject.guavaVersion"
// Dependencies for local unit tests
testCompile "junit:junit:$rootProject.ext.junitVersion"
testCompile "org.mockito:mockito-all:$rootProject.ext.mockitoVersion"
testCompile "org.hamcrest:hamcrest-all:$rootProject.ext.hamcrestVersion"
// Android Testing Support Library's runner and rules
androidTestCompile "com.android.support.test:runner:$rootProject.ext.runnerVersion"
androidTestCompile "com.android.support.test:rules:$rootProject.ext.runnerVersion"
// Dependencies for Android unit tests
androidTestCompile "junit:junit:$rootProject.ext.junitVersion"
androidTestCompile "org.mockito:mockito-core:$rootProject.ext.mockitoVersion"
androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'
// Espresso UI Testing
androidTestCompile "com.android.support.test.espresso:espresso-core:$rootProject.espressoVersion"
androidTestCompile "com.android.support.test.espresso:espresso-contrib:$rootProject.espressoVersion"
androidTestCompile "com.android.support.test.espresso:espresso-intents:$rootProject.espressoVersion"
// Resolve conflicts between main and test APK:
androidTestCompile "com.android.support:support-annotations:$rootProject.supportLibraryVersion"
androidTestCompile "com.android.support:support-v4:$rootProject.supportLibraryVersion"
androidTestCompile "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"
androidTestCompile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
androidTestCompile "com.android.support:design:$rootProject.supportLibraryVersion"
}
方法二
你也可以新建一個(gè)工程級別的.gradle文件來單獨(dú)裝在這些版本號荠列,但需要在module級別的build.gradle文件里添加一行代碼(號表示你指定名稱)类浪。
上面刪除線處更正為:“但需要在project級別”(感謝TsuiYC提醒)
apply from:"*.gradle"
在最外層(Project)工程項(xiàng)目build.gradle頂部添加
apply from:"version.gradle"
最終樣式看圖:
apply from:"version.gradle" //最外層的工程級別的build.gradle里面添加
buildscript {
repositories {
mavenCentral()
google()
jcenter()
}
針對第二種方法,不用修改model.gradle里的任何代碼
感謝