内容简介:Android Project 检查依赖库和插件版本
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/VersionCheckext { //编译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
有问题留言,谢谢~
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 一款检查Gradle依赖配置是否冲突的插件
- 浅谈 Vite 2.0 原理,依赖预编译,插件机制是如何兼容 Rollup 的?
- 浅析依赖倒转、控制反转、IoC 容器、依赖注入。
- Angular 4 依赖注入教程之五 FactoryProvider配置依赖对象
- Gradle构建SpringBoot程序依赖管理之依赖版本自动控制
- Maven学习笔记七【可选的依赖项和依赖项排除】
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。