Android Project 检查依赖库和插件版本

globalme 7年前
   <h2>0x01 问题来源</h2>    <p>随着项目的开发,引用的库也不断增加,维护引用的版本也是一个耗时的问题.比如一个项目的依赖库如下:</p>    <pre>  <code class="language-groovy">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</code></pre>    <p>这么多版本,如何检查是否有新版呢?</p>    <h2>0x02 查找解决方法</h2>    <p>通过Google 查找找到一个问题和答案:</p>    <p><a href="/misc/goto?guid=4959749668639382315" rel="nofollow,noindex">android studio - How to detect when dependency library version updates exist in build.gradle in AndroidStudio project - Stack Overflow</a></p>    <p>该问题的回答者写了个检测依赖库检查的插件,地址如下:</p>    <p><a href="/misc/goto?guid=4959749668736553282" rel="nofollow,noindex">ben-manes/gradle-versions-plugin: Gradle plugin to discover dependency updates</a></p>    <p>根据使用说明,对现有的项目改造,即可实现版本检查.</p>    <h2>0x03 方案实施</h2>    <h3>0x031. 修改依赖配置</h3>    <p>根据插件的使用说明,将项目的依赖改为以下文件,文件地址:</p>    <p><a href="/misc/goto?guid=4959749668812604609" rel="nofollow,noindex">VersionCheck/dependencies.gradle at master · Dev-Wiki/VersionCheck</a></p>    <pre>  <code class="language-groovy">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}",      ]  }</code></pre>    <h3>0x032. 修改项目配置</h3>    <p>然后修改项目的 build.gradle ,引入上述的代码,文件在此:</p>    <p><a href="/misc/goto?guid=4959749668893552808" rel="nofollow,noindex">VersionCheck/build.gradle at master · Dev-Wiki/VersionCheck</a> :</p>    <pre>  <code class="language-groovy">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  }</code></pre>    <h2>0x033. 修改module的引用</h2>    <p>在 module 的 build.gradle 中添加如下:</p>    <pre>  <code class="language-groovy">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')              }          }      }  }</code></pre>    <p>如果是多个module还可以直接修改项目的 build.gradle ,将上述内容抽成一个gradle文件 check.gradle ,修改项目的 build.gradle 如下:</p>    <pre>  <code class="language-groovy">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      }  }    ...</code></pre>    <p>到此,即完成了配置改造.</p>    <h2>0x04 执行检测</h2>    <p>执行一下命令,来执行检查任务:</p>    <pre>  <code class="language-groovy">gradle dependencyUpdates</code></pre>    <p>结果如下:</p>    <pre>  <code class="language-groovy">------------------------------------------------------------  : 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]</code></pre>    <p>从上述结果可以看出版本有无更新.</p>    <h2>0x05 后记</h2>    <p>以上项目地址如下:</p>    <p><a href="/misc/goto?guid=4959749668975266161" rel="nofollow,noindex">Dev-Wiki/VersionCheck: 依赖版本检测Demo</a></p>    <p>有问题留言,谢谢~</p>    <p> </p>    <p>来自:http://blog.devwiki.net/index.php/2017/06/06/android-project-check-dependencies-plugin-version.html</p>    <p> </p>