Gradle 设置全局镜像源,支持 Android 项目,解决镜像源缺失 POM 或 JAR 文件,支持直接替换 URL
👉【教程】Android 安卓开发环境配置:https://www.only4.work/blog/?id=576
配置此文件:%USERPROFILE%\.gradle\init.gradle.kts(如果没有就创建一个)
// https://gist.github.com/mkckr0/97ec5b0d99feede4c19ee6f905d5e722 val repoMirrorMap = mapOf( "https://repo.maven.apache.org/maven2" to "https://maven.aliyun.com/repository/central", "https://dl.google.com/dl/android/maven2" to "https://maven.aliyun.com/repository/google", "https://plugins.gradle.org/m2" to "https://maven.aliyun.com/repository/gradle-plugin", "https://jcenter.bintray.com" to "https://maven.aliyun.com/repository/jcenter", ) val repoReplaceMap = mapOf( "https://maven.google.com" to "https://dl.google.com/dl/android/maven2" ) fun RepositoryHandler.setMirrors() { all { if (this is MavenArtifactRepository && !name.endsWith("Origin")) { val originName = name var originUrl = url.toString().trimEnd('/') // do replace repoReplaceMap[originUrl]?.let { newUrl -> originUrl = newUrl setUrl(originUrl) } // do mirror repoMirrorMap[originUrl]?.let { newUrl -> // replace into mirror repo setUrl(newUrl) // add origin repo to find missing jars artifactUrls(originUrl) // keep origin repo to find missing POM maven(originUrl) { name = "$originName Origin" } } } } printRepos() } fun RepositoryHandler.printRepos() { all { if (this is MavenArtifactRepository) { println("Maven Repo: name=\"$name\", url=$url, artifacts=${artifactUrls}") } } } settingsEvaluated { pluginManagement { repositories { setMirrors() } } dependencyResolutionManagement { @Suppress("UnstableApiUsage") repositories { setMirrors() } } } allprojects { buildscript { repositories { setMirrors() } } repositories { setMirrors() } }
配置参考:https://gist.github.com/mkckr0/97ec5b0d99feede4c19ee6f905d5e722
本站文章除注明转载/出处外,均为原创,若要转载请务必注明出处。转载后请将转载链接通过邮件告知我站,谢谢合作。本站邮箱:admin@only4.work
尊重他人劳动成果,共创和谐网络环境。点击版权声明查看本站相关条款。