创建新项目之后,Gradle Sync和Build都很慢,因为下载Gradle和Maven等工具。
代码默认配置
settings.gradle.kts
pluginManagement {
repositories {
google {
content {
includeGroupByRegex("com\\.android.*")
includeGroupByRegex("com\\.google.*")
includeGroupByRegex("androidx.*")
}
}
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
gradle-wrapper.properties
原本是gradle官网的
#Thu May 23 21:59:57 CST 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
解决方案
1、首先把gradle的地址替换成国内镜像源,会很快下载完gradle
gradle ->wrapper-> gradle-wrapper.properties配置文件
- 原始:https\://services.gradle.org/distributions/gradle-8.6-bin.zip
- 替换成 https\://mirrors.aliyun.com/macports/distfiles/gradle/gradle-8.6-bin.zip
#Thu May 23 21:59:57 CST 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
#distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
distributionUrl=https\://mirrors.aliyun.com/macports/distfiles/gradle/gradle-8.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
-
阿里云镜像 Gradle下载地址(新):macports-distfiles-gradle安装包下载_开源镜像站-阿里云
可以在上述网站复制对应版本的链接,替换到settings.gradle中的distributionUrl。
但还是存在maven很慢的问题,是因为settings.gradle中仓库源配置问题。
2、修改settings.gradle.kts仓库
添加maven在前面
maven { url=uri( "https://maven.aliyun.com/repository/central") }
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/gradle-plugin") }
Note:修改配置后环境依然不太干净会影响实际效果,可以File -> Invalidate Caches... 清除缓存重启
问题过程记录
软件版本
Android Studio Jellyfish | 2023.3.1
新建Kotlin项目
New Project -> Empty Activity
填写项目信息选择kt语言后Finish,同步项目环境配置。当时 Mininum SDK选了34,因为以为是最高版本用...
Gradle Sync 很慢
同步了好久
两小时都没好
最后还是停掉了,可以看到Maven一直很耗时。
修改配置后依然报错很慢
新增url但注释原本的配置会报错
Plugin [id: 'com.android.application', version: '8.4.0', apply: false] was not found in any of the following sources:
* Exception is:
org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'com.android.application', version: '8.4.0', apply: false] was not found in any of the following sources:- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.android.application:com.android.application.gradle.plugin:8.4.0')
Searched in the following repositories:
maven(https://maven.aliyun.com/repository/central)
然后去掉原本配置注释,还是会用到service.gradle.org下载很慢
清理缓存重启
而File -> Invalidate Caches...第二天电脑重启之后,就sync没问题了,直接都能build成功,就是ide会显示,看着是本地环境路径gradle问题,gradle-wrapper多了很多红线,开机也看到说在C盘User下载gradle之类的。
【问题解决】Android Studio项目settings.gradle文件提示Duplicate property key
gradle/wrapper/gradle-wrapper.properties
为什么重启之后,gradle自动下载后这个文件标红线了?
从提示上看是重复的属性值,是否是本地环境中给全部项目配置了统一的值?
解决方案:再File -> Invalidate Caches...后重启就没事了....