状况:
国内网络似乎对国外仓库访问不太友好,为了解决访问仓库的问题各种方案层出不穷。现总结一种较为简洁省事的方案以供参考。
分析:
Gradle生命周期中有一个Initialization的过程,在此过程中我们可以一些全局配置的设置。
解决方案:
复制以下代码到init.gradle文件中,并且保存到.gradle根目录。(点我下载)
gredle_speed_up.pngimport java.util.concurrent.TimeUnit
allprojects { Project project ->
def repoArr = ["https://maven.aliyun.com/repository/public", "https://jitpack.io"] as String[]
def removeRepoArr = ["https://jcenter.bintray.com/"] as String[]
buildscript {
repositories {
all { ArtifactRepository repo ->
if (repo instanceof MavenArtifactRepository) {
def url = repo.url.toString()
for (removeRepo in removeRepoArr) {
if (url.startsWith(removeRepo)) {
remove repo
}
}
}
}
for (item in repoArr) {
maven { setUrl(item) }
}
}
}
allprojects {
repositories {
all { ArtifactRepository repo ->
if (repo instanceof MavenArtifactRepository) {
def url = repo.url.toString()
for (removeRepo in removeRepoArr) {
if (url.startsWith(removeRepo)) {
remove repo
}
}
}
}
for (item in repoArr) {
maven { setUrl(item) }
}
}
}
}
参考:
- [gradle自定义扩展-初始化脚本] https://docs.gradle.org/current/userguide/init_scripts.html
- [Groovy 语言快速入门] https://www.jianshu.com/p/e8dec95c4326
网友评论