如何检测AndroidStudio项目的build.gradle中何时存在依赖库版本更新 [英] How to detect when dependency library version updates exist in build.gradle in AndroidStudio project

查看:44
本文介绍了如何检测AndroidStudio项目的build.gradle中何时存在依赖库版本更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个模块的 android 项目(典型的前端应用程序和后端).我有三个 build.gradle 文件,一个在每个模块中,一个在根目录中.

I've an android project with two modules (typical front-end app and backend). I have three build.gradle files, one in each module and one in the root.

我构建依赖项的方式是将所有版本提取到根级别 build.gradle 中的单独变量中

The way I've structured my dependencies is by extracting all the versions into separate variables in the root level build.gradle as such

ext {
    // SDK and tools
    MIN_SDK_VERSION = 19
    TARGET_SDK_VERSION = 23
    COMPILE_SDK_VERSION = 23
    BUILD_TOOLS_VERSION = '24'

    // app dependencies
    GOOGLE_API_CLIENT_VERSION = '1.19.0'
    GOOGLE_PLAY_SERVICES_VERSION = '8.4.0'
    ANDROID_SUPPORT_LIB_VERSION = '23.1.0'
    [...]

    // backend dependencies
    [...]
}

稍后在我说的应用程序 build.gradle 文件中使用

which are later used in my say app build.gradle file as such

dependencies {
    [...]
    compile(group: 'com.google.oauth-client', name: 'google-oauth-client', version: rootProject.ext.GOOGLE_API_CLIENT_VERSION)
    /////////////////////////////////
    // Google Play Services explicit dependency
    compile(group: 'com.google.android.gms', name: 'play-services-auth', version: rootProject.ext.GOOGLE_PLAY_SERVICES_VERSION)
    compile(group: 'com.google.android.gms', name: 'play-services-plus', version: rootProject.ext.GOOGLE_PLAY_SERVICES_VERSION)
    [...]

    /////////////////////////////////
    // Local Testing
    testCompile(group: 'junit', name: 'junit', version: rootProject.ext.JUNIT_VERSION)
    testCompile(group: 'pl.pragmatists', name: 'JUnitParams', version: rootProject.ext.JUNIT_PARAMS_VERSION)
    [...]
}

注意:我在某个地方的教程中发现了这个想法,我认为它非常漂亮.

NOTE: I found that idea in a tutorial somewhere and I thought it was very nifty.

但是,我正在努力跟踪哪些 lib 版本可用,哪些是可升级的,等等.由于我有一个合理大小的依赖项列表,因此很难跟踪这些事情.好奇其他人是如何解决这个问题的.谢谢.

However, I'm struggling to keep track of which lib versions are available, what is upgradable, etc. It is becoming hard to keep track of these things as I have a reasonably sized list of dependencies. Curious how others have approached this problem. Thanks.

推荐答案

我如何管理依赖项的总结.

A summary of how I manage dependencies.

所有依赖定义都在 gradle/dependencies.gradle 中定义,它适用于所有项目和构建脚本.我通常把它分为三类.一个完整的例子是这里.

All dependency definitions are defined in gradle/dependencies.gradle, which is applied to all projects and the buildscript. I usually break it into three categories. A full example is here.

ext {
  versions = [
    caffeine: '2.3.1',
  ]
  test_versions = [
    testng: '6.9.12',
  ]
  plugin_versions = [
    versions: '0.13.0',
  ]

  libraries = [
    caffeine: "com.github.ben-manes.caffeine:caffeine:${versions.caffeine}",
  ]
  test_libraries = [
    testng: dependencies.create("org.testng:testng:${test_versions.testng}") {
      exclude group: 'junit'
    },
  ]
  gradle_plugins = [
    versions: "com.github.ben-manes:gradle-versions-plugin:${plugin_versions.versions}",
  ]
}

然后在我的根项目中引导它,

Then in my root project I bootstrap it as,

buildscript {
  apply from: "${rootDir}/gradle/dependencies.gradle"

  repositories {
    jcenter()
  }

  dependencies {
    gradle_plugins.each { name, dependency -> classpath dependency }
  }
}

allprojects {
  apply from: "${rootDir}/gradle/dependencies.gradle"

  repositories {
    jcenter()
  }
}

这允许将项目中的依赖项定义为,

This allows defining the dependency in a project as,

dependencies {
  compile libraries.caffeine
}

为了检测更新的版本,我编写了 gradle-versions-plugin.通过查询版本信息的存储库并将其与您的定义进行比较来生成报告.我经常手动运行它,但其他人在他们的 CI 中编写脚本并使用 json 或 xml 报告.

To detect newer versions I wrote the gradle-versions-plugin. That generates a report by querying the repositories for the version information and comparing it to your definitions. I run it manually every so often, but others script it in their CI and use the json or xml reports.

在我编写插件之后,还开发了其他一些方法.Spring 的 dependency-management-plugin 适用于 Maven BOM,Netfix 的 nebula-dependency-recommender-plugin.Netflix 使用 gradle-dependency-lock-plugin 来定义动态版本并生成用于修复发布的锁定文件.还有依赖版本警报服务,不过一个简单的 CI 工作可能是等价的.

There are a few other approaches that were developed after I wrote my plugin. Spring's dependency-management-plugin works with Maven BOMs, as does Netfix's nebula-dependency-recommender-plugin. Netflix uses gradle-dependency-lock-plugin to define dynamic versions and generate a lock file to fix a release. There are also dependency version alerting services, though a simple CI job is likely equivalent.

我从未使用过任何替代方案,因为它们似乎不太直观(对我而言),在我有一个很好的解决方案多年后才出现,如果您来自 Maven,这是一种熟悉的方法.希望其他人可以阐明其他方法的好处.

I've never used any of the alternatives as they seem less intuitive (to me), came out years after I had a nice solution, and it is a familiar approach if you come from Maven. Hopefully someone else can shed light on the benefits of other approaches.

这篇关于如何检测AndroidStudio项目的build.gradle中何时存在依赖库版本更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆