AndroidStudio maven jcenter google 等仓库配置国内镜像

前言

最近在使用Androidstudio从jcenter,google,maven等仓库下载依赖jar的时候,出现下载不了,偶尔能下载,但是很慢;这里推荐使用国内阿里的镜像仓库。

阿里的镜像仓库:
https://maven.aliyun.com/mvn/view

官方使用指南:
https://help.aliyun.com/document_detail/102512.html?spm=a2c40.aliyun_maven_repo.0.0.361830549jTnxB

一,单个项目生效

在项目级别的build.gradle中添加阿里的Maven地址

buildscript {
    repositories {
        maven{ url'http://maven.aliyun.com/nexus/content/groups/public' }
       
//        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
 
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
 
allprojects {
    repositories {
        maven{ url'http://maven.aliyun.com/nexus/content/groups/public' }
       
//        jcenter()
    }
}
 
task clean(type: Delete) {
    delete rootProject.buildDir
}

如果想使用maven.aliyun.com提供的其它代理仓,例如google仓库

buildscript {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/public' }
        maven { url 'https://maven.aliyun.com/repository/google' }
//        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
 
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
 
allprojects {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/public' }
        maven { url 'https://maven.aliyun.com/repository/google' }
//        jcenter()
    }
}
 
task clean(type: Delete) {
    delete rootProject.buildDir
}

注意:

https://maven.aliyun.com/repository/public和http://maven.aliyun.com/nexus/content/groups/public指向同一个存储库,只是两种写法而已;

二,对所有项目生效

C:\Users\用户名\.gradle(例如:C:\Users\Administrator\.gradle)文件夹下新建文件init.gradle文件

allprojects{
    repositories {
        def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/public'
        def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/repository/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 $ALIYUN_REPOSITORY_URL."
                    remove repo
                }
                if (url.startsWith('https://jcenter.bintray.com/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
                    remove repo
                }
            }
        }
        maven {
            url ALIYUN_REPOSITORY_URL
            url ALIYUN_JCENTER_URL
        }
    }
}

你可能感兴趣的:(AndroidStudio maven jcenter google 等仓库配置国内镜像)