0x01 问题来源
随着项目的开发,引用的库也不断增加,维护引用的版本也是一个耗时的问题.比如一个项目的依赖库如下:
compile libraries.supportAppCompat compile libraries.rxJava compile libraries.rxAndroid compile libraries.retrofit compile libraries.retrofitConvertGson compile libraries.retrofitAdapterRxJava compile libraries.okhttp compile libraries.okhttpLogInterceptor compile libraries.okio compile libraries.wire compile libraries.glide
这么多版本,如何检查是否有新版呢?
0x02 查找解决方法
通过Google 查找找到一个问题和答案:
该问题的回答者写了个检测依赖库检查的插件,地址如下:
ben-manes/gradle-versions-plugin: Gradle plugin to discover dependency updates
根据使用说明,对现有的项目改造,即可实现版本检查.
0x03 方案实施
0x031. 修改依赖配置
根据插件的使用说明,将项目的依赖改为以下文件,文件地址:
VersionCheck/dependencies.gradle at master · Dev-Wiki/VersionCheck
ext { //编译SDK版本 compileSdkVer = 25 //构建工具版本 buildToolsVer = "25.0.2" //最低兼容版本 minSdkVer = 14 //测试过的版本 targetSdkVer = 25 //支持库的版本 supportVer = "25.0.0" //JDK版本 sourceCompatVer = JavaVersion.VERSION_1_7 targetCompatVer = JavaVersion.VERSION_1_7 versions = [ supportVersion:"25.0.0", supportMultidex : "1.0.1", junit : "4.12", gson : "2.6.1", rxJava : "1.3.0", // rxAndroid : "1.2.1", // retrofit : "2.3.0", // retrofitConvertGson : "2.3.0", // retrofitAdapterRxJava : "2.3.0", // okhttp : "3.4.1",// okhttpLogInterceptor : "3.4.1", okio : "1.9.0", // wire : "2.1.2", glide : "3.7.0", ] plugin_versions = [ versions: "0.15.0", ] libraries = [ supportAnnotations : "com.android.support:support-annotations:${versions.supportVersion}", supportAppCompat : "com.android.support:appcompat-v7:${versions.supportVersion}", supportRecyclerView : "com.android.support:recyclerview-v7:${versions.supportVersion}", supportDesign : "com.android.support:design:${versions.supportVersion}", supportCardView : "com.android.support:cardview-v7:${versions.supportVersion}", supportMultidex : "com.android.support:multidex:${versions.supportMultidex}", junit : "junit:junit:${versions.junit}", gson : "com.google.code.gson:gson:${versions.gson}", rxJava : "io.reactivex:rxjava:${versions.rxJava}", // rxAndroid : "io.reactivex:rxandroid:${versions.rxAndroid}", // retrofit : "com.squareup.retrofit2:retrofit:${versions.retrofit}", // retrofitConvertGson : "com.squareup.retrofit2:converter-gson:${versions.retrofitConvertGson}", // retrofitAdapterRxJava : "com.squareup.retrofit2:adapter-rxjava:${versions.retrofitAdapterRxJava}", // okhttp : "com.squareup.okhttp3:okhttp:${versions.okhttp}",// okhttpLogInterceptor : "com.squareup.okhttp3:logging-interceptor:${versions.okhttpLogInterceptor}", okio : "com.squareup.okio:okio:${versions.okio}", // wire : "com.squareup.wire:wire-runtime:${versions.wire}", glide : "com.github.bumptech.glide:glide:${versions.glide}", ] gradle_plugins = [ versions: "com.github.ben-manes:gradle-versions-plugin:${plugin_versions.versions}", ] }
0x032. 修改项目配置
然后修改项目的 build.gradle ,引入上述的代码,文件在此:
VersionCheck/build.gradle at master · Dev-Wiki/VersionCheck :
buildscript { apply from: "${rootDir}/dependencies.gradle" repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.2.3' gradle_plugins.each { name, dependency -> classpath dependency } // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { apply from: "${rootDir}/dependencies.gradle" repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
0x033. 修改module的引用
在 module 的 build.gradle 中添加如下:
apply plugin: 'com.github.ben-manes.versions' defaultTasks 'dependencyUpdates' dependencyUpdates.resolutionStrategy = { componentSelection { rules -> rules.all { ComponentSelection selection -> boolean rejected = ['alpha', 'beta', 'rc', 'cr', 'm'].any { qualifier -> selection.candidate.version ==~ /(?i).*[.-]${qualifier}[./d-]*/ } if (rejected) { selection.reject('Release candidate') } } } }
如果是多个module还可以直接修改项目的 build.gradle ,将上述内容抽成一个gradle文件 check.gradle ,修改项目的 build.gradle 如下:
apply from: 'check.gradle' buildscript { apply from: "${rootDir}/dependencies.gradle" repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.2.3' gradle_plugins.each { name, dependency -> classpath dependency } // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } ...
到此,即完成了配置改造.
0x04 执行检测
执行一下命令,来执行检查任务:
gradle dependencyUpdates
结果如下:
------------------------------------------------------------ : Project Dependency Updates (report to plain text file) ------------------------------------------------------------ The following dependencies are using the latest milestone version: - com.squareup.retrofit2:adapter-rxjava:2.3.0 - com.squareup.retrofit2:converter-gson:2.3.0 - com.github.ben-manes:gradle-versions-plugin:0.15.0 - com.squareup.retrofit2:retrofit:2.3.0 - io.reactivex:rxandroid:1.2.1 - io.reactivex:rxjava:1.3.0 The following dependencies exceed the version found at the milestone revision level: - com.github.bumptech.glide:glide [3.7.0 <- 3.6.1] The following dependencies have later milestone versions: - com.android.support:appcompat-v7 [25.0.0 -> 25.3.1] - com.android.tools.build:gradle [2.2.3 -> 2.5.0-alpha-preview-02] - com.squareup.okhttp3:logging-interceptor [3.4.1 -> 3.8.0] - com.squareup.okhttp3:okhttp [3.4.1 -> 3.8.0] - com.squareup.okio:okio [1.9.0 -> 1.13.0] - com.squareup.wire:wire-runtime [2.1.2 -> 2.2.0]
从上述结果可以看出版本有无更新.
0x05 后记
以上项目地址如下:
Dev-Wiki/VersionCheck: 依赖版本检测Demo
有问题留言,谢谢~
来自:http://blog.devwiki.net/index.php/2017/06/06/android-project-check-dependencies-plugin-version.html