在使用AS进行开发时,时常会遇到下载环境失败的一堆报错信息,我所遇到的解决方法如下:
第一种:
在目录C:\Users\xiaobei\.gradle
,在该目录下新建一个文件为init.gradle
内容为下方代码。
在gradle官网下载最新版程序,然后安装到电脑。
在根目录下新建一个文件为init.gradle
内容为下方代码。
目录xiaobei
为自己电脑的用户名
properties 代码:allprojects{
repositories {
def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/central/'
def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/repository/public/'
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
url 'https://maven.aliyun.com/repository/google/'
url 'https://maven.aliyun.com/repository/gradle-plugin/'
}
}
buildscript{
repositories {
def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/central/'
def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/repository/public/'
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
url 'https://maven.aliyun.com/repository/google/'
url 'https://maven.aliyun.com/repository/gradle-plugin/'
}
}
}
}
然后在Android studio中更改如下设置即可
第二种:
- 打开
gradle
文件夹,发现wrapper
文件夹。从此得知是一个使用wrapper
的项目 - 打开
gradle-wrapper.properties
配置文件,代码内容如下:
#Thu Aug 10 14:18:43 CST 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl为告诉你去哪个链接下载。
distributionPath为下载到哪个目录
将下载好的压缩包文件直接放入对应目录即可
第三种
打开
properties 代码:gradle-wrapper.properties
将distributionUrl=
更改为以下内容distributionUrl=https://mirrors.cloud.tencent.com/gradle/
在
gradle 代码:build.gradle.kts
文件中添加以下内容buildscript { repositories { // 改为阿里云的镜像地址 maven { url = uri("https://maven.aliyun.com/repository/public/") } maven { url = uri("https://maven.aliyun.com/repository/google/") } maven { url = uri("https://maven.aliyun.com/repository/jcenter/") } maven { url = uri("https://maven.aliyun.com/repository/central/") } // google() // jcenter() } } allprojects { repositories { } }