How to update Gradle dependencies to their latest

2019-02-01 08:25发布

问题:

Is there an easy way to get gradle to update dependencies to their latest available version?

For build reproducibility all my dependencies are defined with a version number like this in my build.gradle file:

dependencies {
    compile 'namespace:package1:version'
    compile 'namespace:package2:version'
    compile 'namespace:package3:version'
}

Periodically I want to update every package to their latest version. Typically this is the first thing I do for a new sprint after making a release.

It's a real pain doing this manually for each package. Ideally I would like a command to update the build.gradle file for me but at the very least a command that prints out which package needs an update and what the latest version number is.

In ruby land I would run bundler update.

回答1:

This is all I've been able to come up with. I will happily accept another answer if there is a less manual method of doing this.

  1. In Android studio I replace every dependency version with a plus example: compile 'namespace:package1:+'

  2. Sync or build the project which will cause all the dependencies to be resolved to their latest version.

  3. In Android Studio place the cursor on each dependency line in build.gradle and press alt+enter a menu pops up and you can select Replace with specific version



回答2:

It is not a really good practice as libraries can include changes that may break your code.

A common "tolerated" syntax for

compile 'namespace:package:major_version.minor_version.revision'

would be like

compile 'namespace:package:1.0.+'

considering revision is used by the library authors as bug fixes and improvements updates

Note: I just did that and you could do

compile 'namespace:package:+'

Edit:
A Proof Of Concept of my latest comment you may want to test.
This was made in 5 minutes, so don't expect it to be perfect nor flexible.



回答3:

Add to build.gradle:

plugins {
  id 'com.github.ben-manes.versions' version '0.17.0'
}

Then you can do gradle dependencyUpdates to get a report of new versions. Unlike the eponymous Maven plugin, there doesn't seem to be a way of automatically updating the build.gradle yet.

More documentation: https://github.com/ben-manes/gradle-versions-plugin



回答4:

I suffer from it, too. And the best way to check dependencies, even manually, is to go through Project Structure and search for the dependency name and see if there is a newer version.

The problem that this query only checks for the dependencies present in the Maven repository. At least it already goes for Google's.

Note: If you choose to add the dependency with the new version, this will add a duplicity in the your App Gradle, so be sure to delete the old dependency row.

#

Another possible quick fix is through the command line:

./gradlew app: dependencies

This will generate an output like the one below. Note that the asterisk points to a possible new existing version.