简体   繁体   中英

Gradle: find resolved version of a dependency imported with +

I want to print the last version of a dependency in gradle.

I added my dependency in this way :

compile 'test:test:+'

now I want to print the version of my dependency, because I want to know which version I'm using.

I'm using it in this way :

gradle dependencyInsight --configuration compile --dependency test:test

But my output is this :

+--- test:test:+ -> project : (*)

Is there anyway I can get the real version of my dependency and not the + ?

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:

在此输入图像描述

All easy, open hierarchy of view Project and see External Libraries 在此输入图像描述

If you want to check the overview for all your dependencies, you can check with this command -

Solution 1-

./gradlew app:dependencies

Or

Solution 2-

If you want to check for any specific dependency.you can use gradles' build-in 'dependencyInsight : -

gradle dependencyInsight --configuration compile --dependency compile 'test:test:+'

or

Solution 3-

You can check your project .idea folder

inside your project -> .idea/libraries

there also you can see the final version of dependencies used.

Once you have added your dependency as "compile 'test:test:+'" build the project.

Then within the " Project " folder structure hierarchy find that dependency within " External Libraries " at the bottommost of folder structure , it will along with its version there. Use that version with your dependency and re-sync/build project again.

You can do the following:

  • Use the configuration that contains your jar file
  • Filter for the the jar file's name
  • Print the results

This will print the full path as well as the version. You can extract just the jar name if needed.

 task printPmdVersion << {
    FileTree pmdJar = zipTree(configurations.pmd.filter {
        dep -> dep.name.contains("pmd-core")
    }.singleFile)
    println pmdJar
}

Example of output:

ZIP '/home/user/java/gradle_user_home/caches/modules-2/files-2.1/net.sourceforge.pmd/pmd-core/5.4.1/28715c2f768b58759bb5b373365997c30ac35899/pmd-core-5.4.1.jar'

It's not a best practice use the '+' sign to always use the latest library version because you could not be able to have a repeatable build if you need one.

I mean, if you have to checkout your previous version of your APK from your Source Control Management system (eg Git) that you know it works fine, if you compile today (new library version could have been release)... maybe your old friend APK that was working fine... now it doesn't work fine like your latest one.

That said I suggest you using a gradle plugin like that:

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

You will install in your build.gradle at project level like that:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply plugin: 'com.github.ben-manes.versions'

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.github.ben-manes:gradle-versions-plugin:0.17.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

And you'll find a new gradle task named dependencyUpdate that if you lunch it it will report you all your library versions compared with the latest ones:

------------------------------------------------------------
: Project Dependency Updates (report to plain text file)
------------------------------------------------------------

The following dependencies are using the latest milestone version:
 - com.github.ben-manes:gradle-versions-plugin:0.17.0
 - junit:junit:4.12

The following dependencies have later milestone versions:
 - com.android.support:appcompat-v7 [26.1.0 -> 27.0.2]
 - com.android.support.constraint:constraint-layout [1.0.2 -> 1.1.0-beta5]
 - com.android.support.test.espresso:espresso-core [3.0.1 -> 3.0.2-alpha1]
 - com.android.tools.build:gradle [3.0.1 -> 3.2.0-alpha03]
 - org.jacoco:org.jacoco.agent [0.7.4.201502262128 -> 0.8.0]
 - org.jacoco:org.jacoco.ant [0.7.4.201502262128 -> 0.8.0]
 - com.android.support.test:runner [1.0.1 -> 1.0.2-alpha1]

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM