解决Android Studio 下载gradle包慢问题

解决Android Studio 下载gradle包慢问题

文章目录

  • 解决Android Studio 下载gradle包慢问题
    • Android Studio下载包依赖非常慢的原因
    • 解决方法
    • 参考链接

Android Studio下载包依赖非常慢的原因

因为国内访问bintray、jcenter网站访问速度很慢。默认仓库网站如下:

  • https://jcenter.bintray.com/
  • https://dl.google.com/dl/android/maven2/

解决方法

使用阿里提供的仓库进行下载依赖包。阿里仓库网站,包含jcenter、maven、Google仓库,网站是 https://developer.aliyun.com/mvn/guide。

在用户目录/.gradle,新建init.gradle,复制以下内容到init.gradle文件。

gradle.projectsLoaded {
    rootProject.allprojects {
        buildscript {
            repositories {
                def JCENTER_URL = 'https://maven.aliyun.com/repository/public'
                def GOOGLE_URL = 'https://maven.aliyun.com/repository/google'
                def NEXUS_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://jcenter.bintray.com/')) {
                            project.logger.lifecycle "Repository ${repo.url} replaced by $JCENTER_URL."
                            println("buildscript ${repo.url} replaced by $JCENTER_URL.")
                            remove repo
                        }
                        else if (url.startsWith('https://dl.google.com/dl/android/maven2/')) {
                            project.logger.lifecycle "Repository ${repo.url} replaced by $GOOGLE_URL."
                            println("buildscript ${repo.url} replaced by $GOOGLE_URL.")
                            remove repo
                        }
                        else if (url.startsWith('https://repo1.maven.org/maven2')) {
                            project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
                            println("buildscript ${repo.url} replaced by $REPOSITORY_URL.")
                            remove repo
                        }
                    }
                }
                jcenter {
                    url JCENTER_URL
                    allowInsecureProtocol = true
                }
                google {
                    url GOOGLE_URL
                    allowInsecureProtocol = true
                }
                maven {
                    url NEXUS_URL
                    allowInsecureProtocol = true
                }
            }
        }
        repositories {
            def JCENTER_URL = 'https://maven.aliyun.com/repository/public'
            def GOOGLE_URL = 'https://maven.aliyun.com/repository/google'
            def NEXUS_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://jcenter.bintray.com/')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $JCENTER_URL."
                        println("buildscript ${repo.url} replaced by $JCENTER_URL.")
                        remove repo
                    }
                    else if (url.startsWith('https://dl.google.com/dl/android/maven2/')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $GOOGLE_URL."
                        println("buildscript ${repo.url} replaced by $GOOGLE_URL.")
                        remove repo
                    }
                    else if (url.startsWith('https://repo1.maven.org/maven2')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
                        println("buildscript ${repo.url} replaced by $REPOSITORY_URL.")
                        remove repo
                    }
                }
            }
            jcenter {
                url JCENTER_URL
                allowInsecureProtocol = true
            }
            google {
                url GOOGLE_URL
                allowInsecureProtocol = true
            }
            maven {
                url NEXUS_URL
                allowInsecureProtocol = true
            }
        }
    }
}

参考链接

  • gradle下载加速 https://pic3.zhimg.com/ec94df6ff57198c543730aec01cb5bee_xs.jpg?source=172ae18b https://zhuanlan.zhihu.com/p/26019083
  • 使用 allowInsecureProtocol 属性解决 gradle 的仓库地址不安全警告 https://blog.csdn.net/qi_ming_hao_nan/article/details/114799678

你可能感兴趣的:(工具,gradle,android,studio,android)