例子5 编写一个通用的利于管理依赖库版本的脚本文件
参考项目
googlesamples的BasicSample
再应用Android插件时,我们通常会配置很多版本号,例如
...
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
}
}
...
其中的com.android.tools.build:gradle:3.1.2
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.newtrekwang.test"
minSdkVersion 18
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
其中的18,28,com.android.support:appcompat-v7:28.0.0-alpha3,com.android.support:appcompat-v7:28.0.0-alpha3等等
问题
这样直接显示配置的方式,版本号少量还行,多了就有点难受了。
- 构建的时候你要一个一个从别的地方copy版本号
- 加入多个module都依赖了相同的版本号,岂不是改一处,到处都要改
- 一些配置语句太长了
解决
把版本号分在另一个文件,其它地方要用就从文件里拿
例:
1.建一个gradle文件,存放常用的版本号和库名,可以看作存放的都是些键值对,利用gradle的project的扩展可以把这些键值对暂时放进去,然后其他gradle文件要用这些值时通过project获取就行。
version.gradle
// 版本号
ext.deps = [:]
def versions = [:]
versions.arch_core = '1.1.1'
versions.constrant_layout= '1.0.2'
versions.room = '1.1.1'
versions.lifcycle = '1.1.1'
versions.support = "27.1.1"
versions.dagger = "2.15"
versions.junit = "4.12"
versions.espresso = "3.0.1"
versions.rxjava2 = "2.1.3"
versions.rx_android = "2.0.1"
versions.retrofit = "2.3.0"
versions.okhttp3 = "3.9.1"
versions.mockwebserver = "3.8.1"
versions.apache_commons = "2.5"
versions.mockito = "2.7.19"
versions.mockito_all = "1.10.19"
versions.dexmaker = "2.2.0"
versions.glide = "4.7.1"
versions.timber = "4.5.1"
versions.android_gradle_plugin = "3.1.2"
versions.atsl_runner = "1.0.1"
versions.atsl_rules = "1.0.1"
versions.hamcrest = "1.3"
versions.kotlin = "1.2.41"
versions.paging = "1.0.1"
versions.work = "1.0.0-alpha04"
versions.navigation = "1.0.0-alpha01"
versions.arouter = "1.2.2"
versions.arouter_compiler = "1.1.3"
def deps = [:]
// support库
def support = [:]
support.annotations = "com.android.support-annotations:$versions.support"
support.app_compat = "com.android.support:appcompat-v7:$versions.support"
support.recyclerview = "com.android.support:recyclerview-v7:$versions.support"
support.cardview = "com.android.support:cardview-v7:$versions.support"
support.design = "com.android.support:design:$versions.support"
support.v4 = "com.android.support:support-v4:$versions.support"
support.core_utils = "com.android.support:support-core-utils:$versions.support"
support.constrant_layout = "com.android.support.constrant:constrant-layout:$versions.constrant_layout"
deps.support = support
//room相关库
def room = [:]
room.runtime = "android.arch.persistence.room:runtime:$versions.room"
room.compiler = "android.arch.persistence.room:compiler:$versions.room"
room.rxjava2 = "android.arch.persistence.room:rxjava2:$versions.room"
room.testing = "android.arch.persistence.room:testing:$versions.room"
deps.room = room
//lifecycle相关库
def lifecycle = [:]
lifecycle.runtime = "android.arch.lifecycle:runtime:$versions.lifecycle"
lifecycle.extensions = "android.arch.lifecycle:extensions:$versions.lifecycle"
lifecycle.java8 = "android.arch.lifecycle:common-java8:$versions.lifecycle"
lifecycle.compiler = "android.arch.lifecycle:compiler:$versions.lifecycle"
deps.lifecycle = lifecycle
// arch_core库
def arch_core = [:]
arch_core.testing = "android.arch.core:core-testing:$versions.arch_core"
deps.arch_core = arch_core
//dagger 库
def dagger = [:]
dagger.runtime = "com.google.dagger:dagger:$versions.dagger"
dagger.android = "com.google.dagger:dagger-android:$versions.dagger"
dagger.android_support = "com.google.dagger:dagger-android-support:$versions.dagger"
dagger.compiler = "com.google.dagger:dagger-compiler:$versions.dagger"
dagger.android_support_compiler = "com.google.dagger:dagger-android-processor:$versions.dagger"
deps.dagger = dagger
//glide库
def glide = [:]
glide.runtime = "com.github.bumptech.glide:glide:$versions.glide"
glide.compiler = "com.github.bumptech.glide:compiler:$versions.glide"
deps.glide = glide
// rxjava
deps.rxjava2 = "io.reactivex.rxjava2:rxjava:$versions.rxjava2"
deps.rx_android = "io.reactivex.rxjava2:rxandroid:$versions.rx_android"
// gradle插件
deps.android_gradle_plugin = "com.android.tools.build:gradle:$versions.android_gradle_plugin"
// junit
deps.junit = "junit:junit:$versions.junit"
// ARouter
deps.arouter = "com.alibaba:arouter-api:$versions.arouter"
deps.arouter_compiler = "com.alibaba:arouter-compiler:$versions.arouter_compiler"
// retrofit
deps.retrofit = "com.squareup.retrofit2:retrofit:$versions.retrofit"
deps.okhttp3 = "com.squareup.okhttp3:okhttp:$versions.okhttp3"
deps.okhttp3_interceptor = "com.squareup.okhttp3:logging-interceptor:$versions.okhttp3"
deps.retrofit_converter_gson = "com.squareup.retrofit2:converter-gson:$versions.retrofit"
deps.retrofti_adapter_rxjava = "com.squareup.retrofit2:adapter-rxjava:$versions.retrofit"
ext.deps = deps
def build_versions = [:]
build_versions.min_sdk = 19
build_versions.target_sdk = 28
build_versions.build_tools = "27.0.3"
ext.build_versions = build_versions
2.在根目录的build.gradle的buildscript块中使project应用version.gradle,把version.gradle里的键值对加入扩展,应用后,后面的地方就可以由键名获取键值了,当然键值就是你配置的常用的版本号。
buildscript {
apply from: 'version.gradle'
repositories {
google()
jcenter()
}
dependencies {
classpath deps.android_gradle_plugin
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
比如其中deps.android_gradle_plugin就指的是com.android.tools.build:gradle:versions.android_gradle_plugin,然后versions.android_gradle_plugin指的是3.1.2
同样的道理,一个libModule的build.gradle就可以配置为这样
apply plugin: 'com.android.library'
android {
compileSdkVersion build_versions.target_sdk
buildToolsVersion build_versions.build_tools
defaultConfig {
minSdkVersion build_versions.min_sdk
targetSdkVersion build_versions.target_sdk
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//support
api deps.support.app_compat
api deps.support.recyclerview
api deps.support.cardview
api deps.support.design
api deps.support.v4
api deps.support.core_utils
//junit
testImplementation deps.junit
//rxJava
api deps.rxjava2
api deps.rx_android
// net
api deps.retrofit
api deps.okhttp3
api deps.okhttp3_interceptor
api deps.retrofit_converter_gson
api deps.retrofti_adapter_rxjava
//dagger2
api deps.dagger.runtime
annotationProcessor deps.dagger.compiler
}
要改版本号就在version.gradle里改,而且可以在version.gradle尽可能多的版本号或库名字,下次新建项目,直接copy version.gradle进去应用就可以了,不必再到处查库名和版本号