Android Studio 不能获取远程依赖包的解决方法(配置阿里镜像加速依赖)

  • Post author:
  • Post category:其他


一、

Google 的Maven代码

依赖构建,需注意Gradle的版本是否低于4.1

请在Project顶级

build.gradle

文件中包含 Google 的 Maven 代码库:

allprojects {


repositories {


google()

// 如果您使用的是低于 4.1 的 Gradle 版本,则必须改为使用:

// maven {


//     url ‘https://maven.google.com’

// }

// An alternative URL is ‘https://dl.google.com/dl/android/maven2/’

}

}

二、配置国内镜像地址,加速依赖下载(解决依赖失败,推荐阿里云镜像)



  • 阿里云镜像

1. 对指定的项目生效,在项目中的顶级build.gradle修改内容:

buildscript {


repositories {


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’ }

}

dependencies {


classpath ‘com.android.tools.build:gradle:3.4.1’

}

}

allprojects {


repositories {


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’ }

}

}

2. 对所有项目生效,在User/.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’)) {


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

}

}

}



  • 华为镜像

allprojects {
     repositories {
          maven {
              url 'https://repo.huaweicloud.com/repository/maven/'
                }
        jcenter()
    }
}



版权声明:本文为u010217055原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。