在使用 Gradle 构建工程时,jcenter 下载慢、超时的解决方法。
方法一:
https 改成 http 协议下载,修改项目根目录下 build.gradle
文件:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
// jcenter()
jcenter(){url 'http://jcenter.bintray.com/'}
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
// jcenter()
jcenter(){url 'http://jcenter.bintray.com/'}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
方法二:
切换到国内的Maven镜像仓库。
修改项目根目录下 build.gradle
文件,将 jcenter() 或者 mavenCentral() 替换掉即可。
repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/repositories/central/'}
}
或全局修改,USER_HOME/.gradle/ 文件夹下新建 init.gradle
文件,添加如下内容:
allprojects{
repositories {
def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/repositories/central/'
all { ArtifactRepository repo ->
if(repo instanceof MavenArtifactRepository){
def url = repo.url.toString()
if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
remove repo
}
}
}
maven {
url REPOSITORY_URL
}
}
}
init.gradle
文件其实是 Gradle 的初始化脚本 (Initialization Scripts),也是运行时的全局配置。