Gradle task or method to find resolved version of the dynamic dependency

If I have a a dynamic dependency included as "org.apache.commons:commons-lang3:3+";

I am looking for gradle task or groovy method (i.e. programatic way) to find the actual version that it resolves to during build process e.g. 3+ resolved to 3.7

1 Like

Firstly you can determine this from the command-line by doing something like

./gradlew dependencies --configuration=compile

(Substitute compile with the configuration of your choice).

To determine programmatically what it will be, you are required to resolve the configuration. So a simple way would be to do

project.configurations.getByName('WHATEVER').files.find { it.name.startsWith('commons-lang') }

or you could do

project.configurations.getByName('WHATEVER').resolvedConfiguration.resolvedArtifacts
    .find { name == 'commons-lang }.moduleVersion.id.version

@Schalk_Cronje What is the “WHATEVER” ? Also I think you miss the ' on the second part, after commons-lang.
Also, suppose I’m the developer of the library that is being used as dependency.
How can I get this version at runtime?
Can I force a gradle task to be used to get the dependency version, and then put it on some global variable that I can reach via Java/Kotlin?

If it’s unclear, suppose I make an Android library called “mySdk”, and I publish it on Jitpack/Maven.

The user of the SDK would use it by adding just the dependency of :

implementation 'com.github.my-sdk:MySdk:1.0.1'

What I’d like to get is the “1.0.1” part from it, whether I do it from within the Android library itself (can be useful to send to the SDK-server which version is used), or from the app that uses it (can be useful to report about specific issues).

Just add the version to your library at build time, for example as generated properties file with a class that reads it from the file, then you and your consumers can simply ask that class for the version.