欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Studio Gradle 更换阿里云镜像的方法

程序员文章站 2022-06-22 21:22:34
使用 android studio 开发时经常遇到编译卡住的问题,原因是 gradle 下载依赖资源过慢。没办法,有长城在,还是得换镜像。同样,这是个普遍存在的问题,我们希望可以对它进行全局配置。在...

使用 android studio 开发时经常遇到编译卡住的问题,原因是 gradle 下载依赖资源过慢。没办法,有长城在,还是得换镜像。

同样,这是个普遍存在的问题,我们希望可以对它进行全局配置。在 .gradle (路径参考 c:\users\username\.gradle )目录下新增 init.gradle 文件,内容如下:

allprojects{
  repositories {
    def aliyun_repository_url = 'http://maven.aliyun.com/nexus/content/groups/public'
    def aliyun_jcenter_url = 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
    all { artifactrepository repo ->
      if(repo instanceof mavenartifactrepository){
        def url = repo.url.tostring()
        if (url.startswith('https://repo1.maven.org/maven2') || url.startswith('http://repo1.maven.org/maven2')) {
          project.logger.lifecycle "repository ${repo.url} replaced by $aliyun_repository_url."
          remove repo
        }
        if (url.startswith('https://jcenter.bintray.com/') || url.startswith('http://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
    }
  }


  buildscript{
    repositories {
      def aliyun_repository_url = 'http://maven.aliyun.com/nexus/content/groups/public'
      def aliyun_jcenter_url = 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
      all { artifactrepository repo ->
        if(repo instanceof mavenartifactrepository){
          def url = repo.url.tostring()
          if (url.startswith('https://repo1.maven.org/maven2') || url.startswith('http://repo1.maven.org/maven2')) {
            project.logger.lifecycle "repository ${repo.url} replaced by $aliyun_repository_url."
            remove repo
          }
          if (url.startswith('https://jcenter.bintray.com/') || url.startswith('http://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
      }
    }
  }
}

如只需对单个项目进行配置,可以在项目根目录下的 build.gradle 文件中添加如下代码:

maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }

搞定,下载速度飞起~

到此这篇关于android studio gradle 更换阿里云镜像的方法的文章就介绍到这了,更多相关android studio gradle阿里云内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!