6

One of the advantages of using Gradle in Android Studio is that it helps in dependency management. So if I have used a particular version of a library in my build.gradle file, then how will I force it to update the dependency version once the higher version is available?

Some of the dependencies in my build.gradle are specified as

dependencies {
    compile project(':facebookSDK')
    compile files('libs/picasso-2.1.1.jar')
    compile files('libs/crouton-1.8.1.jar')
}
Opal
  • 81,889
  • 28
  • 189
  • 210
Diffy
  • 2,339
  • 3
  • 25
  • 47

3 Answers3

10

One of the advantages of using Gradle in Android Studio is that it helps in dependency management.

Not the way that you are using it.

So if i have used a particular version of a library in my build.gradle file, then how will i force it to update the dependency version once the higher version is available?

In your case, you would download the new JARs, put them in libs/, and update your build.gradle to match.

The preferred approach is for you to delete those JARs and replace your two compile files statements with ones that pull down the dependencies from Maven Central or another artifact repository. You can find the proper statements for popular open source libraries like those via the Gradle, please site.

In your case, you would use:

compile 'com.squareup.picasso:picasso:2.3.3'
compile 'de.keyboardsurfer.android.widget:crouton:1.8.5'

These will require you to also have a repositories closure as a peer to your dependencies closure:

repositories {
    mavenCentral()
}

This may already exist.

These compile statements still pin you to a specific version of those libraries, but moving to a new version would be a simple matter of updating the compile statement, and Gradle will pull down the new dependency on your next build.

If you want, you could replace part of the version number with a wildcard (e.g., 2.3.+). This will cause Gradle to automatically update to new patchlevels of the library, in this case. Some developers do not approve of this approach, as while it is convenient, it does reduce your ability to be able to reproduce a build (e.g., you need to recompile some older branch of your code, and now you don't know what version of the artifact you were using back then).

Opal
  • 81,889
  • 28
  • 189
  • 210
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Perfect answer (as ever @CommonsWare). But recently, my gradle doesn't highlight the libraries that have new version in the repository, and that is a powerfull feature for me. – wendigo Jan 22 '15 at 14:55
  • @wendigo: I am not aware that Gradle ever "highlights" libraries newer than what you ask for. If you use wildcards, it will automatically grab them; if you do not use wildcards, it ignores them. There's a plugin for Gradle that will give you a report of available updates to dependencies, though I forget where I saw it. – CommonsWare Jan 22 '15 at 14:57
  • I'll add this answer too : http://stackoverflow.com/a/35371234/2057809 Having gradle doing dependencies update is cool, but it's even more usefull when it can also detect automatically which dependencies need to be updated. – Edouard Brèthes Mar 07 '16 at 16:51
3

As you are compiling files from your local project, I don't think you can automatically compile a new individual jar version if available. What you can do instead of compiling individual files is:

compile fileTree(dir: 'libs', include: '*.jar')

This will compile all jars in the libs directory so you will always have the latest version.

Both the libraries you are using are available to be compiled as dependencies from mavencentral.

compile 'de.keyboardsurfer.android.widget:crouton:1.8.5'
compile 'com.squareup.picasso:picasso:2.3.3'

If you want to ensure you are getting the latest versions is you use a plus in place of the version number. It's up to you how open you want to be with this.. so

compile 'de.keyboardsurfer.android.widget:crouton:1.+'
compile 'com.squareup.picasso:picasso:2.+'

will give you the latest version under the 1. or 2. versioning cycles...

speedynomads
  • 2,632
  • 1
  • 26
  • 24
  • Thanks for the answer. @CommonsWare has already given a perfect answer. – Diffy Jul 25 '14 at 11:26
  • This is not something i'll not recommend : having an automatic update for dependencies means to loose an easy way to control which version is currently installed. For corrections update, it can be, but for minor update this is kinda unsafe. – Edouard Brèthes Mar 07 '16 at 16:49
  • 1
    @Aldo If you read the answer, I am not recommending always using the latest dependencies, I am just informing that it is possible if it is something that is required. This answers the question correctly and does not deserve to be voted down. – speedynomads Mar 17 '16 at 10:56
  • @domji84 well wasn't really obvious :( But i agree with you, my vote down was a little bit harsh, i'm sorry. I edited your answer to put a new vote up after :) – Edouard Brèthes Mar 17 '16 at 11:15
0
  • If you want, you could replace part of the version number with a wildcard (e.g., 2.3.+).
  • This will cause Gradle to automatically update to new patch-levels of the library, in this case.
  • Some developers do not approve of this approach, as while it is convenient it does reduce your ability to be able to reproduce a build (e.g., you need to recompile some older branch of your code, and now you don't know what version of the artifact you were using back then).

I have found Gradle, please to be my answer here.

  • Easily get the latest android library gradle compile statement.