Gradle构建错误:无法找到semver4j-0.16.4-nodeps.jar求解决方案
问题回顾
我们在CircleCI上构建Android项目时频繁遇到以下错误:
A problem occurred configuring root project 'android'. > Could not resolve all artifacts for configuration ':classpath'. > Could not find semver4j-0.16.4-nodeps.jar (com.github.gundy:semver4j:0.16.4). Searched in the following locations: https://jitpack.io/com/github/gundy/semver4j/0.16.4/semver4j-0.16.4-nodeps.jar
重新运行任务偶尔能成功,本地用--refresh-dependencies仅重现一次,后续构建又正常。升级Kotlin Gradle插件到1.7.0(该版本已将semver4j改为内嵌依赖)也未能解决,且已确认仓库配置包含jitpack。
可行解决方案
1. 用Gradle依赖替换强制替换为可用版本
com.github.gundy的semver4j在jitpack上的nodeps版本不稳定,我们可以强制将Kotlin插件依赖的这个包替换为mavenCentral上的官方semver4j,或者同一仓库的完整jar包。在项目根目录的build.gradle(buildscript块所在的文件)中添加以下配置:
buildscript { configurations.classpath { resolutionStrategy { dependencySubstitution { // 替换为mavenCentral上的官方版本 substitute module('com.github.gundy:semver4j:0.16.4') with module('com.vdurmont:semver4j:0.16.4') // 如果官方版本也有问题,可尝试替换为带依赖的版本: // substitute module('com.github.gundy:semver4j:0.16.4') with module('com.github.gundy:semver4j:0.16.4') because { // artifact name: 'semver4j', type: 'jar', extension: 'jar' // } } } } // 原有的repositories和dependencies配置... }
这样Gradle会优先使用指定的替代依赖,绕开找不到的nodeps版本。
2. 清理CircleCI的依赖缓存
CircleCI的缓存可能保留了旧的依赖索引或损坏的缓存文件,导致依赖解析异常。可以在CircleCI的配置文件(.circleci/config.yml)中添加缓存清理步骤,或者直接在构建前执行:
./gradlew cleanBuildCache --refresh-dependencies
同时,可以调整CircleCI的缓存策略,只缓存必要的目录(比如~/.gradle/caches/modules-2),避免缓存无效的依赖数据。
3. 显式声明semver4j依赖
在buildscript的dependencies块中显式添加semver4j的依赖,让Gradle提前解析到可用的版本:
buildscript { dependencies { // 显式添加,优先于Kotlin插件的传递依赖 classpath 'com.vdurmont:semver4j:0.16.4' // 原有的Kotlin插件等依赖... classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0" } }
这样Gradle会先下载这个依赖,避免Kotlin插件触发的传递依赖找不到问题。
4. 确认Kotlin插件版本完全升级
确保所有模块的Kotlin插件版本都统一为1.7.0,并且CircleCI构建环境中没有残留旧版本的插件缓存。可以在构建脚本中打印Kotlin版本来验证:
./gradlew -v
检查输出中的Kotlin插件版本是否为1.7.0,如果不是,需要确认项目中所有ext.kotlin_version的配置都已更新,并且清理Gradle缓存。
内容的提问来源于stack exchange,提问作者Mark McClelland

