罪恶克星 发表于 2024-6-22 18:09:45

Android Studio项目gradle下载慢题目

一、题目起因

通过Android studio新建了一个Android项目,同步gradle居然耗费了30多分钟。忍不了,真的忍不了。
二、原因分析

1. 通过观察日志耗时环境

发现耗时告急会合在两方面:gradle下载和gradle依赖下载,如下图所示,gradle下载耗时24分钟,gradle依赖下载耗时7分钟
https://img-blog.csdnimg.cn/direct/f864bc5f1e8d4a74930132107453abe7.png
   PS:源耗时日志没了,上图是小编为了写博客后补的,可真是煎熬的30分钟啊,如此敬业,这你不得点个赞?
2. 检察gradle-wrapper.properties

文件路径gradle/wrapper/gradle-wrapper.properties


[*]gradle-wrapper.properties是Gradle Wrapper的配置文件,用于指定Gradle版本。通过配置这个文件,可以确保项目在不同的环境中使用雷同版本的Gradle举行构建。
[*]Gradle Wrapper的目的是办理不同机器上Gradle版本不一致的题目,通过配置gradle-wrapper.properties文件,项目可以在任何环境下主动下载并使用精确的Gradle版本举行构建。
检察后发现,默认使用的地点为https://services.gradle.org/distributions/gradle-8.2-bin.zip。
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
   提示:可能你的gradle版本与小编不同,但并无影响
通过ip查询工具,可发现services.gradle.org域名所在地理位置为美国。呵呵!国外源,慢得理所应当。
3. 检察settings.gradle.kts

文件路径根目录


[*]settings.gradle.kts文件是Kotlin DSL格式的settings文件,它用于配置项目的模块结构。在这个文件中,你可以声明项目中包罗哪些模块、这些模块之间的依赖关系,以及全局的构建逻辑。
[*]在新版本gradle中,settings.gradle.kts文件还承担了堆栈配置的工作,而这也是我们关注的重点
通过Android studio新建的项目,gradle堆栈配置默认如下
pluginManagement {
repositories {
   google()
   mavenCentral()
   gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
   google()
   mavenCentral()
}
}

rootProject.name = "Demo"
include(":app")
通过ip查询工具,分别查询地理位置,默认源同样均在国外
配置地点地点位置google()https://maven.google.com/美国mavenCentral()https://repo1.maven.org/maven2/瑞典gradlePluginPortal()https://plugins.gradle.org/m2/美国 行吧,国外源,网络环境不理想,下载速率相对较慢,慢得有理有据。
三、办理方式

对于上述题目,常见的办理方式无非走VPN署理或通过国内镜像源加速,由于合规的VPN署理必要申请,本文告急解说通过国内镜像源加速的方式。有合规VPN署理的观众老爷们可以撤了。
1. gradle下载慢办理方案

常见的办理方案包括:使用gradle离线下载 或 通过gradle国内镜像源加速
个人觉得,gradle离线下载相对麻烦,不睁开解说。感兴趣的同砚可自行百度gradle离线下载
对比gradle离线下载,通过gradle国内镜像源相对方便些,国内着名的gradle镜像源,包罗阿里云和腾讯云


[*]阿里云gradle镜像源:https://mirrors.aliyun.com/gradle
[*]腾讯云gradle镜像源:https://mirrors.cloud.tencent.com/gradle/
可惜的是,阿里云gradle镜像源截至2019年就不再更新,gradle版本也停留在了gradle-5.6.2,若你使用的是gradle-5.6.2以上版本,更推荐你使用腾讯云gradle镜像源,使用方式如下:
替换https\://services.gradle.org/distributions为https\://mirrors.cloud.tencent.com/gradle
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-8.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2. gradle依赖下载慢办理方案

为加速gradle依赖下载,我们可以通过国内镜像源加速,这里强烈安利 阿里云镜像源
源库阿里云的镜像地点google()https://maven.aliyun.com/repository/googlemavenCentral()https://maven.aliyun.com/repository/centralgradlePluginPortal()https://maven.aliyun.com/repository/gradle-plugin   提示:上面仅列出我们使用到的镜像源,全量镜像源请见附录1:阿里云全量镜像源。
使用方式如下:
修改settings.gradle.kts,将对应阿里云镜像堆栈添加到google()和mavenCentral()上方,优先从国内源下载,如果没有再去原网站下。
pluginManagement {
    repositories {
      // 使用阿里镜像源
      maven(url = "https://maven.aliyun.com/repository/google")
      maven(url = "https://maven.aliyun.com/repository/central")
      maven(url = "https://maven.aliyun.com/repository/gradle-plugin")
      google()
      mavenCentral()
      gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
      // 使用阿里镜像源
      maven(url = "https://maven.aliyun.com/repository/google")
      maven(url = "https://maven.aliyun.com/repository/central")
      google()
      mavenCentral()
    }
}

rootProject.name = "Demo"
include(":app")
   建议:请不要随意改变堆栈位置。各个堆栈的列出序次决定了 Gradle 在这些堆栈中搜索各个项目依赖项的序次。例如,如果从堆栈 A 和 B 均可获得某个依赖项,而您先列出了堆栈 A,则 Gradle 会从堆栈 A 下载该依赖项。
提示:若你使用的groovy,非kotlin,请转附录2:gradle依赖下载加速-groovy版
四、总结(省流版)

1. gradle下载慢办理方案

替换https\://services.gradle.org/distributions为https\://mirrors.cloud.tencent.com/gradle
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-8.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2. gradle依赖下载慢办理方案

修改settings.gradle.kts,将对应阿里云镜像堆栈添加到google()和mavenCentral()上方,优先从国内源下载,如果没有再去原网站下。
pluginManagement {
repositories {
   // 使用阿里镜像源
   maven(url = "https://maven.aliyun.com/repository/google")
   maven(url = "https://maven.aliyun.com/repository/central")
   maven(url = "https://maven.aliyun.com/repository/gradle-plugin")
   google()
   mavenCentral()
   gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
   // 使用阿里镜像源
   maven(url = "https://maven.aliyun.com/repository/google")
   maven(url = "https://maven.aliyun.com/repository/central")
   google()
   mavenCentral()
}
}

rootProject.name = "Demo"
include(":app")
   建议:请不要随意改变堆栈位置。各个堆栈的列出序次决定了 Gradle 在这些堆栈中搜索各个项目依赖项的序次。例如,如果从堆栈 A 和 B 均可获得某个依赖项,而您先列出了堆栈 A,则 Gradle 会从堆栈 A 下载该依赖项。
提示:若你使用的groovy,非kotlin,请转附录2:gradle依赖下载加速-groovy版
附录

附录1:阿里云全量镜像源

阿里云常见镜像库:https://developer.aliyun.com/mvn/guide
堆栈名称阿里云堆栈地点阿里云堆栈地点(老版)源地点centralhttps://maven.aliyun.com/repository/centralhttps://maven.aliyun.com/nexus/content/repositories/centralhttps://repo1.maven.org/maven2/jcenterhttps://maven.aliyun.com/repository/publichttps://maven.aliyun.com/nexus/content/repositories/jcenterhttp://jcenter.bintray.com/publichttps://maven.aliyun.com/repository/publichttps://maven.aliyun.com/nexus/content/groups/publiccentral仓和jcenter仓的聚合仓googlehttps://maven.aliyun.com/repository/googlehttps://maven.aliyun.com/nexus/content/repositories/googlehttps://maven.google.com/gradle-pluginhttps://maven.aliyun.com/repository/gradle-pluginhttps://maven.aliyun.com/nexus/content/repositories/gradle-pluginhttps://plugins.gradle.org/m2/springhttps://maven.aliyun.com/repository/springhttps://maven.aliyun.com/nexus/content/repositories/springhttp://repo.spring.io/libs-milestone/spring-pluginhttps://maven.aliyun.com/repository/spring-pluginhttps://maven.aliyun.com/nexus/content/repositories/spring-pluginhttp://repo.spring.io/plugins-release/grails-corehttps://maven.aliyun.com/repository/grails-corehttps://maven.aliyun.com/nexus/content/repositories/grails-corehttps://repo.grails.org/grails/coreapache snapshotshttps://maven.aliyun.com/repository/apache-snapshotshttps://maven.aliyun.com/nexus/content/repositories/apache-snapshotshttps://repository.apache.org/snapshots/   提示:由于阿里云官网更新,各人查到的数据可能比上面少。大部门库未在常见镜像库中展示,如google()库,全量堆栈请通过https://developer.aliyun.com/mvn/view检察
附录2:gradle依赖下载加速-groovy版

修改settings.gradle,将对应阿里云镜像堆栈添加到google()和mavenCentral()上方,优先从国内源下载,如果没有再去原网站下。
pluginManagement {
    repositories {
      // 使用阿里镜像源
      maven { url 'https://maven.aliyun.com/repository/google' }
      maven { url 'https://maven.aliyun.com/repository/central' }
      maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
      google()
      mavenCentral()
      gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
      // 使用阿里镜像源
      maven { url 'https://maven.aliyun.com/repository/google' }
      maven { url 'https://maven.aliyun.com/repository/central' }
      google()
      mavenCentral()
    }
}

rootProject.name = "Demo"
include ':app'
   建议:请不要随意改变堆栈位置。各个堆栈的列出序次决定了 Gradle 在这些堆栈中搜索各个项目依赖项的序次。例如,如果从堆栈 A 和 B 均可获得某个依赖项,而您先列出了堆栈 A,则 Gradle 会从堆栈 A 下载该依赖项。
参考文档

添加 build 依赖项 | Android Studio | Android Developers (google.cn)
阿里云堆栈服务 (aliyun.com)
阿里巴巴开源镜像站
Android Studio 配置国内镜像源、HTTP署理

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Android Studio项目gradle下载慢题目