Skip to content

Instantly share code, notes, and snippets.

@wuseal
Last active March 16, 2024 03:22
Show Gist options
  • Save wuseal/76506684c63c5399150d9fd671cbc89b to your computer and use it in GitHub Desktop.
Save wuseal/76506684c63c5399150d9fd671cbc89b to your computer and use it in GitHub Desktop.
国内全局加速Gradle依赖下载速度配置,把这个文件放到~/.gradle目录下既可
/**
* Created by Seal.Wu on 2020/7/11
* Description: Set up mirrors for Gradle Globally
*/
val MAVEN_REPOSITORY_URL = "https://maven.aliyun.com/repository/central"
val JCENTER_REPOSITORY_URL = "https://maven.aliyun.com/repository/jcenter"
val GOOGLE_REPOSITORY_URL = "https://maven.aliyun.com/repository/google"
val GRADLE_PLUGIN_REPOSITORY_URL = "https://maven.aliyun.com/repository/gradle-plugin"
gradle.settingsEvaluated {
pluginManagement {
// Print repositories collection
println("Plugins Repositories names: " + repositories.names)
// Clear repositories collection
repositories.clear()
// Add my Artifactory mirror
repositories {
maven {
name = "Aly Gradle Plugin Repo"
url = uri(GRADLE_PLUGIN_REPOSITORY_URL)
}
}
// Print repositories collection
println("Now Plugins Repositories names : " + repositories.names)
}
}
allprojects {
repositories {
all {
if (this is MavenArtifactRepository) {
val url = url.toString()
when {
url.startsWith("https://repo1.maven.org/maven2")|| url.startsWith("https://repo.maven.apache.org/maven2/") -> {
setUrl(MAVEN_REPOSITORY_URL)
}
url.startsWith("https://jcenter.bintray.com/") -> {
setUrl(JCENTER_REPOSITORY_URL)
}
url.startsWith("https://dl.google.com/dl/android/maven2") -> {
setUrl(GOOGLE_REPOSITORY_URL)
}
}
}
}
}
buildscript {
repositories {
all {
if (this is MavenArtifactRepository) {
val url = this.url.toString()
when {
url.startsWith("https://repo1.maven.org/maven2")||url.startsWith("https://repo.maven.apache.org/maven2/") -> {
setUrl(MAVEN_REPOSITORY_URL)
}
url.startsWith("https://jcenter.bintray.com/") -> {
setUrl(JCENTER_REPOSITORY_URL)
}
url.startsWith("https://dl.google.com/dl/android/maven2") -> {
setUrl(GOOGLE_REPOSITORY_URL)
}
}
}
}
}
}
afterEvaluate {
repositories {
val lastUsedRepos = filterIsInstance<MavenArtifactRepository>().map {
it.name + "(${it.url})"
}
if (lastUsedRepos.isNotEmpty()) {
println("Use these repositories at last :\n $lastUsedRepos")
}
}
buildscript {
repositories {
val lastUsedRepos = filterIsInstance<MavenArtifactRepository>().map {
it.name + "(${it.url})"
}
if (lastUsedRepos.isNotEmpty()) {
println("Use these repositories at last in build script:\n $lastUsedRepos")
}
}
}
}
}
@Sivan757
Copy link

37和56行缺少右括号...

@tanranran
Copy link

tanranran commented Oct 15, 2021

修复后的
`/**

gradle.settingsEvaluated {
pluginManagement {
// Print repositories collection
println("Plugins Repositories names: " + repositories.names)

    // Clear repositories collection
    repositories.clear()

    // Add my Artifactory mirror
    repositories {
        maven {
            name = "Aly Gradle Plugin Repo"
            url = uri(GRADLE_PLUGIN_REPOSITORY_URL)
        }
    }

    // Print repositories collection
    println("Now Plugins Repositories names : " + repositories.names)
}

}

allprojects {
repositories {
all {
if (this is MavenArtifactRepository) {
val url = url.toString()
when {
url.startsWith("https://repo1.maven.org/maven2")|| url.startsWith("https://repo.maven.apache.org/maven2/") -> {
setUrl(MAVEN_REPOSITORY_URL)
}
url.startsWith("https://jcenter.bintray.com/") -> {
setUrl(JCENTER_REPOSITORY_URL)
}
url.startsWith("https://dl.google.com/dl/android/maven2") -> {
setUrl(GOOGLE_REPOSITORY_URL)
}
}
}
}
}
buildscript {
repositories {
all {
if (this is MavenArtifactRepository) {
val url = this.url.toString()
when {
url.startsWith("https://repo1.maven.org/maven2")||url.startsWith("https://repo.maven.apache.org/maven2/") -> {
setUrl(MAVEN_REPOSITORY_URL)
}
url.startsWith("https://jcenter.bintray.com/") -> {
setUrl(JCENTER_REPOSITORY_URL)
}
url.startsWith("https://dl.google.com/dl/android/maven2") -> {
setUrl(GOOGLE_REPOSITORY_URL)
}
}
}
}
}
}
afterEvaluate {
repositories {
val lastUsedRepos = filterIsInstance().map {
it.name + "(${it.url})"
}
if (lastUsedRepos.isNotEmpty()) {
println("Use these repositories at last :\n $lastUsedRepos")
}
}
buildscript {
repositories {
val lastUsedRepos = filterIsInstance().map {
it.name + "(${it.url})"
}
if (lastUsedRepos.isNotEmpty()) {
println("Use these repositories at last in build script:\n $lastUsedRepos")
}
}
}
}
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment