使用aliyun的maven仓库初始化gradle

在linux中使用gradle命令行构建程序时,使用国内镜像可以大大加快我们的构建过程,节约我们的时间,下面就如何配置aliyun仓库作为gradle的默认仓库作出说明。

  1. 如果只想对单个项目生效,那么可以在项目的build.gradle中添加如下配置:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    buildscript {
    repositories {
    maven {url 'https://maven.aliyun.com/repository/google/'}
    maven {url 'https://maven.aliyun.com/repository/jcenter/'}
    }
    dependencies {
    classpath: 'com.android.tools.build:gradle:2.2.3'

    }
    }
    allprojects {
    repositories {
    maven { url 'https://maven.aliyun.com/repository/google/' }
    maven { url 'https://maven.aliyun.com/repository/jcenter/'}
    }
    }
  2. 如果是对所有项目生效,那么需要在用户的家目录下面的.gradle目录下创建init.gradle文件,内容如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    allprojects{
    repositories {
    def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/public/'
    def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/repository/jcenter/'
    def ALIYUN_GOOGLE_URL = 'https://maven.aliyun.com/repository/google/'
    def ALIYUN_GRADLE_PLUGIN_URL = 'https://maven.aliyun.com/repository/gradle-plugin/'
    all { ArtifactRepository repo ->
    if(repo instanceof MavenArtifactRepository){
    def url = repo.url.toString()
    if (url.startsWith('https://repo1.maven.org/maven2/')) {
    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
    remove repo
    }
    if (url.startsWith('https://jcenter.bintray.com/')) {
    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
    remove repo
    }
    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://plugins.gradle.org/m2/')) {
    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_GRADLE_PLUGIN_URL."
    remove repo
    }
    }
    }
    maven { url ALIYUN_REPOSITORY_URL }
    maven { url ALIYUN_JCENTER_URL }
    maven { url ALIYUN_GOOGLE_URL }
    maven { url ALIYUN_GRADLE_PLUGIN_URL }
    }
    }