其他
Android Studio中gradle文件下载慢的解决方法
The following article is from 卓码空间 Author QDroid88888
在Android Studio创建工程的时候,经常碰到同步gradle很慢或者直接卡住的情况。由于自带的Maven源地址是国外的,该Maven源在国内的访问速度是很慢的。可以通过替换替换下载源为阿里云的方式解决。方法如下:
一、只修改当前工程配置
Android Studio加载工程之后,在工程根目录有一个build.gradle文件。在"repositories"节点中都添加阿里云的maven,修改之后如下:
buildscript {
repositories {
///ADD START 新增的
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }
///ADD END
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
///ADD START 新增的
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }
///ADD END
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
二、全局修改对所有工程生效
1.首先找到Android Studio存放gradle的目录
依次在Android Studio中操作菜单项"File->Settings...",弹出"Settings"对话框。如下所示:
在"Settings"对话框中找到并打开gradle配置项,"Gradle user home:"表示的当前存放gradle的目录,如下图所示:
从上图可知,当前我的gradle配置目录为:
C:\Users\Qiang\.gradle
2.在gradle配置目录配置阿里云下载源
在gradle配置目录"C:\Users\Qiang.gradle"中创建文件init.gradle,并将以下内容保存:
allprojects{
repositories {
def MY_ALIYUN_REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public'
def MY_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 $MY_ALIYUN_REPOSITORY_URL."
remove repo
}
if (url.startsWith('https://jcenter.bintray.com/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $MY_ALIYUN_JCENTER_URL."
remove repo
}
}
}
maven {
url MY_ALIYUN_REPOSITORY_URL
url MY_ALIYUN_JCENTER_URL
}
}
}
以下是我个人的配置情况,如下所示:
配置完成之后重启Android Studio就可以了。
大佬们留个关注再走呗,后续精彩文章不断