一个项目(project)文件夹中有多个工程(module),这多个module使用的
compileSdkVersion
buildToolsVersion
minSdkVersion
targetSdkVersion
依赖库
统一管理其版本号,避免频繁修改
在module项目里的build.gradle文件中,通过下面的方式来统一版本
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"
}
对比
下面是没有统一管理的依赖
dependencies {
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
testCompile 'junit:junit:4.12'
}
当module很多的时候,如果没有统一管理,那么将可能导致很多问题:
- A module使用24版本的v7包,B module使用25版本的v7包。当另一台电脑需要运行这些module时而不得不下载24和25两个版本的v7包,即浪费时间又浪费系统资源;
- 而当需要更新依赖库版本时,必须一个一个module去修改,非常耗时;
废话少说,下面开始设置
代码
在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'
}
上面的名称和版本号根据个人需要进行修改。
在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"
}
方法二
你也可以新建一个工程级别的.gradle文件来单独装在这些版本号,但需要在module级别的build.gradle文件里添加一行代码(号表示你指定名称)。
上面删除线处更正为:“但需要在project级别”(感谢TsuiYC提醒)
apply from:"*.gradle"
在最外层(Project)工程项目build.gradle顶部添加
apply from:"version.gradle"
最终样式看图:
apply from:"version.gradle" //最外层的工程级别的build.gradle里面添加
buildscript {
repositories {
mavenCentral()
google()
jcenter()
}
针对第二种方法,不用修改model.gradle里的任何代码
感谢
网友评论