Appearance
动态版本与范围版本
动态版本
kotlin
dependencies {
// latest.release:最新稳定版
implementation("com.example:lib:latest.release")
// 前缀通配符:1.2.x 的最新版
implementation("com.example:lib:1.2.+")
// 最新快照
implementation("com.example:lib:latest.integration")
}生产环境警告
动态版本会导致构建不可重现,不推荐在生产项目中使用。每次构建可能使用不同版本,难以排查问题。推荐使用固定版本 + 依赖锁定。
版本范围
kotlin
dependencies {
// [min, max]:包含两端
implementation("com.example:lib:[1.0, 2.0]")
// [min, max):不含最大值
implementation("com.example:lib:[1.0, 2.0)")
// (min, max):不含两端
implementation("com.example:lib:(1.0, 2.0)")
// ]min, max]:不含最小值
implementation("com.example:lib:]1.0, 2.0]")
}动态版本缓存
Gradle 默认缓存动态版本 24 小时,之后重新检查:
kotlin
configurations.all {
resolutionStrategy.cacheDynamicVersionsFor(4, TimeUnit.HOURS)
resolutionStrategy.cacheChangingModulesFor(0, TimeUnit.SECONDS) // 快照不缓存
}bash
# 强制刷新(忽略缓存)
./gradlew build --refresh-dependencies最佳实践
| 场景 | 推荐做法 |
|---|---|
| 生产项目 | 固定版本 + 依赖锁定 |
| 持续集成 | 固定版本或使用 BOM |
| 内部工具 | 可使用 1.+ 跟踪最新小版本 |
| 快照测试 | SNAPSHOT 版本 + 禁用缓存 |