Skip to content

Instantly share code, notes, and snippets.

@Jacknic
Forked from bennyhuo/init.gradle.kts
Last active August 22, 2025 11:00
Show Gist options
  • Save Jacknic/61b2b576f66993149cc57c504c7ed609 to your computer and use it in GitHub Desktop.
Save Jacknic/61b2b576f66993149cc57c504c7ed609 to your computer and use it in GitHub Desktop.
How to config mirrors for repositories in Gradle without changing the source code of your project?
@file:Suppress("UnstableApiUsage")
val urlMappingsTencent = mapOf(
"https://repo.maven.apache.org/maven2/" to "https://mirrors.tencent.com/nexus/repository/maven-public/",
"https://dl.google.com/dl/android/maven2/" to "https://mirrors.tencent.com/nexus/repository/maven-public/",
"https://plugins.gradle.org/m2" to "https://mirrors.tencent.com/nexus/repository/gradle-plugins/"
)
val urlMappingsHuawei = mapOf(
"https://repo.maven.apache.org/maven2/" to "https://mirrors.huaweicloud.com/repository/maven/",
"https://dl.google.com/dl/android/maven2/" to "https://mirrors.huaweicloud.com/repository/maven/",
"https://plugins.gradle.org/m2" to "https://mirrors.tencent.com/nexus/repository/gradle-plugins/"
)
val urlMappingsAliyun = 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://plugins.gradle.org/m2" to "https://mirrors.tencent.com/nexus/repository/gradle-plugins/"
)
/**
* 启用镜像地址映射
*/
fun RepositoryHandler.enableMirror() {
all {
if (this is MavenArtifactRepository) {
val originalUrl = url.toString()
var mappingUrl = "origin"
urlMappingsHuawei[originalUrl]?.let {
mappingUrl = it
setUrl(it)
}
logger.lifecycle("Repository $name [$originalUrl] is mirrored to $mappingUrl")
}
}
}
gradle.allprojects {
buildscript {
repositories.enableMirror()
}
repositories.enableMirror()
}
gradle.beforeSettings {
pluginManagement.repositories.enableMirror()
// 6.8 及更高版本执行 DependencyResolutionManagement 配置
val pattern = "(\\d+)\\.(\\d+)(.*)?".toPattern()
val matcher = pattern.matcher(gradleVersion)
if (!matcher.matches()) {
println("Gradle=$gradleVersion version not match $settings")
return@beforeSettings
}
val majorPart = matcher.group(1)!!.toInt(10)
val minorPart = matcher.group(2)!!.toInt(10)
val gtVersion68 = majorPart > 6 || (majorPart == 6 && minorPart >= 8)
if (gtVersion68) {
val getDrm = settings.javaClass.getDeclaredMethod("getDependencyResolutionManagement")
val drm = getDrm.invoke(settings)
val getRepos = drm.javaClass.getDeclaredMethod("getRepositories")
val repos = getRepos.invoke(drm) as RepositoryHandler
repos.enableMirror()
println("Gradle ${gradle.gradleVersion} DependencyResolutionManagement Configured $settings")
} else {
println("Gradle ${gradle.gradleVersion} DependencyResolutionManagement Ignored $settings")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment