Gradle在AS中配置问题

Gradle是基于Apache Ant和Apache Maven概念的项目自动化构建开源工具。

AS中所有项目都基于Gradle构建,所以初始化时需要对gradle进行配置。此时会出现Gradle project sync failed的情况,决绝此问题方法可参考如下链接:
https://jingyan.baidu.com/article/597a06430710ca312b52430b.html
但此方法未必可用,因为会爆出**“unable to find valid certification path to requested target”**错误此时可参考
https://blog.csdn.net/gabriel576282253/article/details/81531746
但依然会出现非法证书的错误,因此最佳的解决方案如下:
在.gradle目录下init.gradle文件并在其中加入如下代码:

allprojects{
    repositories {
        google()
        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 }
        maven { url ALIYUN_JCENTER_URL }
        
    }
}

你可能感兴趣的:(Android)