Gradle - Getting the Latest Release Version of a Dependency

Gradle - getting the latest release version of a dependency

Gradle currently does not support Maven's RELEASE (which is rarely used and deprecated) but it does support Ivy's latest.release (and for snapshots latest.integration). However, the general recommendation is to build against exact versions. Otherwise, the build can become a lottery.

Gradle: find resolved version of a dependency imported with +

Within app module's build.gradle I've imported Square's Moshi library as follows:



dependencies {
compile 'com.squareup.moshi:moshi:+'
}

Then I executed following command in terminal:



./gradlew app:dependencyInsight --configuration compile --dependency com.squareup.moshi:moshi

Here's the output that I've received:

Sample Image

How to check if Gradle dependency has new version?

This is now built-in to Android Studio as a Lint check. You can enable it here:

Settings > Editor > Inspections > "Newer Library Versions Available"

The description for this inspection:

Newer Library Versions Available

This detector checks with a central repository to see if there are newer versions available for the dependencies used by this project. This is similar to the GradleDependency check, which checks for newer versions available in the Android SDK tools and libraries, but this works with any MavenCentral dependency, and connects to the library every time, which makes it more flexible but also much slower.

Because of the slowdown this can introduce I'd recommend running it manually periodically, rather than having it always on. You can do this by:

Analyze > "Run Inspection By Name"

Then search for "Newer Library Versions Available", and run it on your module.

Edit: The above should just work as of Android Studio 2.3. If you're on an old version (you should update) the following may help:

Note that it appears you must have the inspection enabled in order to run it manually - so (as of Android Studio 2.0 Beta 2) you need to find the inspection in settings, enable it, then run it by name, then disable it again (to regain previous performance).

Can gradle use latest.release as default dependency version?

Generally, best practice is to always build against the exact version in order to avoid unpredicted scenarios.

However, if you really want to build against latest release, you can do it like this:

dependencies {
myConfiguration 'group:module:latest.release'
}

The only (proper) way to enforce latest.release for all the artefacts is to write a resolution strategy.
Have a look at: ResolutionStrategy

Gradle does not pull latest version from artifact repo?

When you say "sometimes", that's probably within 24 hours of the last update check, as by default, Gradle caches dynamic modules for 24 hours. If you want to enforce an update check before 24 hours have passed, you indeed need to use --refresh-dependencies.

Or you lower the time for how long resolved versions will be cached like this (in Kotlin DSL):

configurations.all {
resolutionStrategy.cacheDynamicVersionsFor(10, "minutes")
}

How to check what is the latest version of a dependency to use in gradle

There may be other ways, but here is what i use:

You can find out the latest version using Android Studio by replacing the version number of your library in build.gradle compile line, with just + , and click on Sync Now in upper right corner of the window.

in your case, for example

dependencies {
compile 'com.android.support:mediarouter-v7:+'
}

Android Studio will pop up a hint/bulb, which has options Replace with specific version you can click, which will fill-in the latest version in-place of +. Please see below screeshot:

android studio hint

If this doesn't work the first time, let gradle complete its sync, and retry (replace + with + or any file modification will do, click the sync now again and hint bulb will show up).

For example, for your library, i simply pasted this line compile 'com.android.support:mediarouter-v7:+' under my dependencies and followed the above process, Android Studio filled in with below version

.

Is there a way to get the version of a dependency from within build.gradle?

It looks like you just want to path of the resolved JAR. If I understood correctly, then you can resolve the configuration and filter the dependency you want. For example, using commons-lang3:

dependencies {
implementation("org.apache.commons:commons-lang3:3.11")
}

val commonsLang3Jar = configurations.compileClasspath.get().filter { it.name.startsWith("commons-lang3") }.singleFile

println(commonsLang3Jar.absolutePath)

This results in the following line being printed:

~/.gradle/caches/modules-2/files-2.1/org.apache.commons/commons-lang3/3.11/68e9a6adf7cf8eb7e9d31bbc554c7c75eeaac568/commons-lang3-3.11.jar

Why is my gradle build not loading the most recent dependency?

By default, gradle caches SNAPSHOT dependencies for 24 hours.

You can refresh them manually with

--refresh-dependencies

Or you can configure the cache durations as per the guide:
https://docs.gradle.org/current/userguide/dependency_management.html#sub:cache_refresh



Related Topics



Leave a reply



Submit