在使用AS进行开发时,时常会遇到下载环境失败的一堆报错信息,我所遇到的解决方法如下:


第一种:

在目录C:\Users\xiaobei\.gradle,在该目录下新建一个文件为init.gradle内容为下方代码。

在gradle官网下载最新版程序,然后安装到电脑。

在根目录下新建一个文件为init.gradle内容为下方代码。

目录xiaobei为自己电脑的用户名

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中更改如下设置即可 更改此设置

第二种:

  1. 打开gradle文件夹,发现wrapper文件夹。从此得知是一个使用wrapper的项目
  2. 打开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为下载到哪个目录

将下载好的压缩包文件直接放入对应目录即可