配置国内阿里云 Gradle 仓库镜像

选择一:项目级配置(推荐)

在项目根目录的build.gradle文件中添加以下内容

buildscript {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/google/' }
        maven { url 'https://maven.aliyun.com/repository/jcenter/' }
    }
}

allprojects {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/google/' }
        maven { url 'https://maven.aliyun.com/repository/jcenter/' }
    }
}

选择二:全局配置(操作系统的当前用户)

注意:该方案经过测试,不好用,仅做记录。

在操作系统当前用户的 HOME 目录,找到 .gradle 文件夹,在这个文件夹下面创建一个文本文件 init.gradle,完整的文件路径可能如下:
C:\Users\<june>\.gradle\init.gradle。
在此文件中加入如下代码片段:

buildscript{
    repositories {
        def ALIYUN_GOOGLE_URL = 'https://maven.aliyun.com/repository/google/'
        def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/repository/jcenter/'
        all { ArtifactRepository repo ->
            if(repo instanceof MavenArtifactRepository){
                def url = repo.url.toString()
                if (url.startsWith('https://dl.google.com/dl/android/maven2/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_GOOGLE_URL."
                    remove repo
                }
                if (url.startsWith('https://jcenter.bintray.com/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
                    remove repo
                }
            }
        }
        maven {
            url ALIYUN_GOOGLE_URL
            url ALIYUN_JCENTER_URL
        }
    }
}

allprojects{
    repositories {
        def ALIYUN_GOOGLE_URL = 'https://maven.aliyun.com/repository/google/'
        def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/repository/jcenter/'
        all { ArtifactRepository repo ->
            if(repo instanceof MavenArtifactRepository){
                def url = repo.url.toString()
                if (url.startsWith('https://dl.google.com/dl/android/maven2/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_GOOGLE_URL."
                    remove repo
                }
                if (url.startsWith('https://jcenter.bintray.com/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
                    remove repo
                }
            }
        }
        maven {
            url ALIYUN_GOOGLE_URL
            url ALIYUN_JCENTER_URL
        }
    }
}

参考链接:

阿里云仓库

Gradle配置国内镜像

gradle 配置国内镜像

发表评论