You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gradle:获取使用+引入的依赖的实际解析版本

获取Gradle中+依赖的实际解析版本

嘿,我懂你碰到的麻烦——用+通配符引入依赖后,不管怎么看配置里都是+,想知道实际拉下来的具体版本却摸不着头脑。别慌,这几个方法能帮你轻松拿到真实的解析版本:

方法1:用Gradle依赖报告命令

这是最直接的方式,Gradle自带的dependencies任务会输出所有依赖的完整解析树,包括实际版本。

  • 运行全局依赖报告(针对整个项目):
    # Linux/macOS
    ./gradlew dependencies
    
    # Windows
    gradlew.bat dependencies
    
  • 如果项目有多个模块,指定模块更高效:
    ./gradlew :app:dependencies  # 替换:app为你的模块名
    
  • 想快速定位特定依赖?用过滤命令:
    # Linux/macOS 过滤指定依赖
    ./gradlew :app:dependencies | grep "com.example:your-library"
    
    # Windows 过滤指定依赖
    gradlew.bat :app:dependencies | findstr "com.example:your-library"
    
    报告里会明确显示解析后的具体版本,比如com.example:your-library:1.5.2,而不是你声明的+。

方法2:在构建脚本中主动打印版本

如果你想在构建过程中直接输出某个依赖的版本,可以在build.gradle(或build.gradle.kts)里添加一段代码:

Groovy DSL 示例

afterEvaluate {
    configurations.all { config ->
        config.resolvedConfiguration.firstLevelModuleDependencies.each { dep ->
            // 替换成你要查询的依赖组和模块名
            if (dep.moduleGroup == "com.example" && dep.moduleName == "your-library") {
                println "✅ ${dep.moduleGroup}:${dep.moduleName} 实际解析版本: ${dep.moduleVersion}"
            }
        }
    }
}

Kotlin DSL 示例

afterEvaluate {
    configurations.all {
        resolvedConfiguration.firstLevelModuleDependencies.forEach { dep ->
            if (dep.moduleGroup == "com.example" && dep.moduleName == "your-library") {
                println("✅ ${dep.moduleGroup}:${dep.moduleName} 实际解析版本: ${dep.moduleVersion}")
            }
        }
    }
}

运行任何构建命令(比如./gradlew build),控制台就会输出你指定依赖的真实版本。

方法3:用dependencyInsight精准查询

这个任务专门用来深入分析单个依赖的解析过程,输出结果更聚焦:

./gradlew :app:dependencyInsight \
  --dependency com.example:your-library \
  --configuration implementation  # 替换成你的依赖配置(api/compileOnly等)

执行后会直接显示该依赖的最终版本,还会列出它的依赖路径、冲突解决情况,帮你彻底搞清楚版本是怎么确定的。

小提醒

为什么配置里显示的是+?因为+是你声明的依赖版本占位符,实际版本是Gradle在依赖解析阶段才确定的,所以必须通过上述依赖报告或脚本的方式,才能获取到解析后的真实版本。

内容的提问来源于stack exchange,提问作者Daniel Taub

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:33:31