Gradle 出现 Could not resolve gradle
Gradle 在進(jìn)行 sync 的時(shí)候會(huì)出現(xiàn)
Caused by: org.gradle.internal.resolve.ModuleVersionResolveException: Could not resolve gradle:gradle:8.2.
查看異常信息發(fā)現(xiàn) Gradle 無(wú)法下載 https://services.gradle.org/distributions/gradle-8.2-src.zip,這個(gè)鏈接重定向到 https://github.com/gradle/gradle-distributions/releases/download/v8.2.0/gradle-8.2-src.zip,Github 很難連上。
在 gradle-wrapper.properties 中 distributionUrl 設(shè)置為 https\://mirror.nju.edu.cn/gradle/gradle-8.2-bin.zip ,Gradle 仍然會(huì)下載 https://services.gradle.org/distributions/gradle-8.2-src.zip。為什么 Gradle 不使用鏡像源呢?翻了一下 Gradle 的源碼,發(fā)現(xiàn)這個(gè)鏈接是寫(xiě)死的。
private
fun createSourceRepository() = ivy {
val repoName = repositoryNameFor(gradleVersion)
name = "Gradle $repoName"
setUrl("https://services.gradle.org/$repoName")
metadataSources {
artifact()
}
patternLayout {
if (isSnapshot(gradleVersion)) {
ivy("/dummy") // avoids a lookup that interferes with version listing
}
artifact("[module]-[revision](-[classifier])(.[ext])")
}
}
沒(méi)有任何方法可以直接修改這個(gè)鏈接。
要解決這個(gè)問(wèn)題,可以直接為 Gradle 設(shè)置代理進(jìn)行網(wǎng)絡(luò)加速。但是這樣會(huì)導(dǎo)致之前設(shè)置的 Maven 鏡像鏈接也會(huì)經(jīng)過(guò)代理。
繼續(xù)翻看 Gradle 源碼發(fā)現(xiàn)有這樣一段代碼
private
fun sourceRootsOf(gradleInstallation: File, sourceDistributionResolver: SourceDistributionProvider): Collection<File> =
gradleInstallationSources(gradleInstallation) ?: downloadedSources(sourceDistributionResolver)
private
fun gradleInstallationSources(gradleInstallation: File) =
File(gradleInstallation, "src").takeIf { it.exists() }?.let { subDirsOf(it) }
gradleInstallation 存在 src 目錄的時(shí)候就不會(huì)繼續(xù)下載 gradle-8.2-src.zip。繼續(xù)往上翻,發(fā)現(xiàn)這個(gè)值就是 project.gradle.gradleHomeDir
直接把這個(gè)變量在 build.gradle.kts 中打印出來(lái)就是 %USERPROFILE%\.gradle\wrapper\dists\gradle-8.2-bin\4zwrvmkltlrdjhbk3gu6ax49g\gradle-8.2。這個(gè)文件夾就是 gradle-8.2-bin.zip 解壓后的。
于是直接把gradle-wrapper.properties里 distributionUrl 的 bin 改為 all,再把 distributionSha256Sum 修改為對(duì)應(yīng)的值。也就是
distributionSha256Sum=5022b0b25fe182b0e50867e77f484501dba44feeea88f5c1f13b6b4660463640
distributionUrl=https\://mirror.nju.edu.cn/gradle/gradle-8.2-all.zip
直接 Build 通過(guò),沒(méi)有任何問(wèn)題。
gradle-8.2-all.zip 里面已經(jīng)包含了 src 目錄,Gradle 不會(huì)繼續(xù)下載 src。
可以查詢 https://gradle.org/release-checksums 找到對(duì)應(yīng)版本的 distributionSha256Sum。如果本來(lái)就沒(méi)用它,可以不改這個(gè)值。
總結(jié)
以上是生活随笔為你收集整理的Gradle 出现 Could not resolve gradle的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: STM32CubeMX教程20 SPI
- 下一篇: xshell配置隧道转移规则