众所周知,在开发中我们需要从maven中央仓库下载依赖库,但是因为某种原因,导致这些仓库无法访问,幸运的是国内很多大公司都搭建的镜像库供我们使用,比如阿里的maven镜像。android现在都是通过gradle要管理依赖库,你发现直接添加maven{url: url}的方式是无效的。这里提供下一个有效的方案。

allprojects{
    repositories {
        def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/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
        }
    }
}