androidstudio源换成阿里云(转载)

元气小坏坏 提交于 2020-02-26 23:48:06

平时安卓项目编译较慢,其中一个原因就是要下载依赖资源包比较慢。为了解决这个问题,可以配置阿里云镜像,加速下载。

配置方法:

1. 对特定项目生效,在项目中的build.gradle修改内容

 1 buildscript {
 2     repositories {
 3         maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
 4         maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
 5         maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
 6         maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }
 7     }
 8     dependencies {
 9         classpath 'com.android.tools.build:gradle:3.2.1'
10     }
11 }
12  
13 allprojects {
14     repositories {
15         maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
16         maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
17         maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
18         maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }
19     }
20 }

2. 对所有项目生效

在用户/.gradle/下创建init.gradle文件,内容如下:

allprojects{
    repositories {
        def ALIYUN_REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public'
        def ALIYUN_JCENTER_URL = 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
        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
                }
            }
        }
        maven {
                url ALIYUN_REPOSITORY_URL
            url ALIYUN_JCENTER_URL
        }
    }
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!