在我们开发应用的时候,都会用到一些第三方库,设置应用的targetSdkVersion
、compileSdkVersion
,buildToolsVersion
等,一般情况下,我们都是直接在应用的gradle文件中引用库,如下面:
compile project(':FastBleLib')
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.jakewharton:butterknife:8.5.1'
compile 'com.android.support:design:26.0.0-alpha1'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.kaopiz:kprogresshud:1.0.2'
compile 'org.greenrobot:eventbus:3.0.0'
testCompile 'junit:junit:4.12'
但是这种情况下,有一种弊端,我们无法统一管理各个库的版本号,每次改动都要一一去找到这个库,然后修改他的版本号。一旦没有修改,可能造成很多问题,如RxJava的1.x和2.x
。那有没有一种统一管理的方式呢?答案肯定是有的。
实现方法
1. 首先在工程gradle文件中,添加各个库的版本,如下面:
// Define versions in a single place
ext {
// Sdk and tools
minSdkVersion = 10
targetSdkVersion = 24
compileSdkVersion = 24
buildToolsVersion = '24.0.2'
// App dependencies
supportLibraryVersion = '24.2.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'
rxjavaVersion = '1.1.8'
rxandroidVersion = '1.2.1'
sqlbriteVersion = '0.7.0'
}
注意:这里不是添加到
buildscript
里面,而是和它同级的一个配置
2.在应用的gradle中调用
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
applicationId "com.example.android.architecture.blueprints.todomvp"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
jackOptions {
enabled true
}
}
dependencies {
// 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"
compile "io.reactivex:rxjava:$rootProject.rxjavaVersion"
compile "io.reactivex:rxandroid:$rootProject.rxandroidVersion"
compile "com.squareup.sqlbrite:sqlbrite:$rootProject.sqlbriteVersion"
注意:在
android
这个配置中,是直接调用rootProject.ext.版本号变量
*
而在dependencies
配置中调用$rootProject.版本号变量
这样是不是阅读性更好,更好管理我们的应用了呢~~
如果想阅读更多的文章,可以访问博客Lange0x0的博客http://shiyiliang.cn