JCenter下载太慢?教你修改Maven仓库地址为国内镜像

http://blog.csdn.net/biezhihua/article/details/49668605

转载自:http://www.yrom.net/blog/2015/02/07/change-gradle-maven-repo-url/

近来迁移了一些项目到Android Studio,采用Gradle构建确实比原来的Ant方便许多。但是编译时下载依赖的网速又着实令人蛋疼不已。

如果能切换到国内的Maven镜像仓库,如开源中国的Maven库,又或者是换成自建的Maven私服,那想必是极好的。

一个简单的办法,修改项目根目录下的build.gradle,将jcenter()或者mavenCentral()替换掉即可:

allprojects {
    repositories {
        maven{ url 'http://maven.oschina.net/content/groups/public/'}
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

但是架不住项目多,难不成每个都改一遍么? 
自然是有省事的办法,将下面这段Copy到名为init.gradle文件中,并保存到USER_HOME/.gradle/文件夹下即可。

allprojects{
    repositories {
        def REPOSITORY_URL = 'http://maven.oschina.net/content/groups/public'
        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
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

init.gradle文件其实是Gradle的初始化脚本(Initialization Scripts),也是运行时的全局配置。

如果碰到如下错误,多尝试几次就好了:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'fresco'.
> Could not resolve all dependencies for configuration ':classpath'.
   > Could not download httpcore.jar (org.apache.httpcomponents:httpcore:4.1)
      > Could not get resource 'https://jcenter.bintray.com/org/apache/httpcomponents/httpcore/4.1/httpcore-4.1.jar'.
         > SSL peer shut down incorrectly

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED
posted @ 2017-07-17 18:50  mydddfly  阅读(3406)  评论(0编辑  收藏  举报