Android Studio构建时报gradle相关错误的解决办法

坑1:Connection timed out: connect. If you are behind an HTTP proxy, please configure the proxy settings either in IDE or Gradle.

https://services.gradle.org/distributions/gradle-6.5.1-all.zip这个链接直接发在浏览器输入下载,但Adroid Studio不能下载。

这是因为加了代理,而代理无效导致。在setting中把代理去掉即可。如下图。

 

坑2:在studio构建gradle的时候无法下载gradle插件

            - https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/6.5.1/gradle-6.5.1.pom
            - https://jcenter.bintray.com/com/android/tools/build/gradle/6.5.1/gradle-6.5.1.pom

这是因为在国内google和jcenter都访问不了,所以无法下载。需要添加阿里云的下载地址,具体如下:

在Project的build.gradle里的allprojects里添加:

但有时会发现,这样修改了之后,即像Android Studio重启后还是无法下载。从日志看,还是走了代理。但明明代理已经取消了,选用了No Proxy。那这是怎么回事呢。请看坑3.

坑3:明明已经取消代理了,但从日志找印看,还是走了代理。

原来是用户目录下的gradle.properties文件中还记录有代理,需要注释掉。

C:\Users\LZ\.gradle\gradle.properties

## For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
#
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx1024m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
#
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#Sat Jul 04 11:50:15 CST 2020
#systemProp.http.proxyHost=mirrors.dormforce.net
#systemProp.http.proxyPort=80
#systemProp.https.proxyHost=mirrors.dormforce.net
#systemProp.https.proxyPort=80

 

坑4:Could not get resource 'http://maven.aliyun.com/nexus/content/groups/public/com/android/tools/build/gradle/6.5.1/gradle-6.5.1.pom'

已经没有用代理了,但还是无法获取到插件。

           Gradle sync failed: Could not find com.android.tools.build:gradle:6.5.1.
            Searched in the following locations:
            - https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/6.5.1/gradle-6.5.1.pom
            - https://jcenter.bintray.com/com/android/tools/build/gradle/6.5.1/gradle-6.5.1.pom
            - http://maven.aliyun.com/nexus/content/groups/public/com/android/tools/build/gradle/6.5.1/gradle-6.5.1.pom

这是因为把gradle和gradle插件当作一个东西了,所以在project structure里把这两个的版本号都配成6.5.1了。所以下载完gradle之后,再下插件发现找不到就报错了。

build.gralde里的classpath 'com.android.tools.build:gradle:6.5.1'关联的是上图中的Android gradle Plugin Version,而gradle-wrapper.properties里的distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-all.zip关联的是上图中的Gradle Version!

Gradle是开源的自动化构建工具,而Gradle插件是google开发的在Android Studio中使用的Gradle的插件。

所以现在Gradle插件最新的版本只到3.6.0,并没有6.5.1!所以他是真的没有!真的没有!

developer.android.google.cn查询之后,将gradle version修改成5.4.1,插件版本修改成3.5.0,如上图。

插件版本 所需的 Gradle 版本
1.0.0 - 1.1.3 2.2.1 - 2.3
1.2.0 - 1.3.1 2.2.1 - 2.9
1.5.0 2.2.1 - 2.13
2.0.0 - 2.1.2 2.10 - 2.13
2.1.3 - 2.2.3 2.14.1+
2.3.0+ 3.3+
3.0.0+ 4.1+
3.1.0+ 4.4+
3.2.0 - 3.2.1 4.6+
3.3.0 - 3.3.2 4.10.1+
3.4.0 - 3.4.1 5.1.1+
3.5.0-3.5.3 5.4.1+
3.6.0+ 5.6.4+

坑5:解决坑4之后,还是找不到文件

直接云阿里云上搜信息

https://help.aliyun.com/document_detail/102512.html?spm=a2c40.aliyun_maven_repo.0.0.361830549jTnxB

获取到如下信息:

maven.aliyun.com代理了很多公共的maven仓库。使用maven.aliyun.com中的仓库地址作为下载源,速度更快更稳定。

代理的仓库列表

仓库名称 代理源地址 使用地址
central https://repo1.maven.org/maven2/ https://maven.aliyun.com/repository/central 或 https://maven.aliyun.com/nexus/content/repositories/central
jcenter http://jcenter.bintray.com/ https://maven.aliyun.com/repository/jcenter 或 https://maven.aliyun.com/nexus/content/repositories/jcenter
public central仓和jcenter仓的聚合仓 https://maven.aliyun.com/repository/public 或https://maven.aliyun.com/nexus/content/groups/public
google https://maven.google.com/ https://maven.aliyun.com/repository/google 或 https://maven.aliyun.com/nexus/content/repositories/google
gradle-plugin https://plugins.gradle.org/m2/ https://maven.aliyun.com/repository/gradle-plugin 或 https://maven.aliyun.com/nexus/content/repositories/gradle-plugin
spring http://repo.spring.io/libs-milestone/ https://maven.aliyun.com/repository/spring 或 https://maven.aliyun.com/nexus/content/repositories/spring
spring-plugin http://repo.spring.io/plugins-release/ https://maven.aliyun.com/repository/spring-plugin 或 https://maven.aliyun.com/nexus/content/repositories/spring-plugin
grails-core https://repo.grails.org/grails/core https://maven.aliyun.com/repository/grails-core 或 https://maven.aliyun.com/nexus/content/repositories/grails-core
apache snapshots https://repository.apache.org/snapshots/ https://maven.aliyun.com/repository/apache-snapshots 或 https://maven.aliyun.com/nexus/content/repositories/apache-snapshots

以及一些配置指南。按配置指南检查,都没有问题。

最后直接云central仓的代理地址https://repo1.maven.org/maven2/查看,找到了gradle插件库:

https://repo1.maven.org/maven2/com/android/tools/build/gradle/

原来是这个maven库中gradle插件只更新到了2017年的2.3.0.终于知道为什么找不到了。于是只能把插件版本修改成2.3.0,再尝试sync。该问题终于消失了,gradle插件终于下载下来了。

 

---------------------------------------------------------------

https://developer.android.google.cn/studio/releases/gradle-plugin.html#updating-gradle

Android Gradle 插件版本说明

Android Studio 构建系统以 Gradle 为基础,并且 Android Gradle 插件添加了几项专用于构建 Android 应用的功能。虽然 Android 插件通常会与 Android Studio 的更新步调保持一致,但插件(以及 Gradle 系统的其余部分)可独立于 Android Studio 运行并单独更新。

本页将介绍如何让 Gradle 工具保持最新状态,以及最近的更新都包含哪些内容。

如需详细了解如何使用 Gradle 配置 Android build,请参阅以下页面:

要详细了解 Gradle 构建系统,请参阅 Gradle 用户指南

更新 Android Gradle 插件

在更新 Android Studio 时,您可能会收到将 Android Gradle 插件自动更新为最新可用版本的提示。您可以选择接受该更新,也可以根据项目的构建要求手动指定版本。

您可以在 Android Studio 的 File > Project Structure > Project 菜单中指定插件版本,也可以在顶级 build.gradle 文件中进行指定。该插件版本适用于在相应 Android Studio 项目中构建的所有模块。以下示例从 build.gradle 文件中将插件的版本设置为 3.6.0:

    buildscript {
        repositories {
            // Gradle 4.1 and higher include support for Google's Maven repo using
            // the google() method. And you need to include this repo to download
            // Android Gradle plugin 3.0.0 or higher.
            google()
            ...
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.6.0'
        }
    }
    

 

注意:请勿在版本号中采用动态依赖项(例如 'com.android.tools.build:gradle:2.+'),否则可能会导致意外的版本更新和难以解析的版本差异。

如果指定的插件版本尚未下载,则 Gradle 会在您下次构建项目时进行下载;或者,您也可以在 Android Studio 菜单栏中依次点击 Tools > Android > Sync Project with Gradle Files 进行下载。

更新 Gradle

在更新 Android Studio 时,您可能会收到一并将 Gradle 更新为最新可用版本的提示。您可以选择接受该更新,也可以根据项目的构建要求手动指定版本。

下表列出了各个 Android Gradle 插件版本所需的 Gradle 版本。要获得最佳性能,您应该使用 Gradle 和插件这两者的最新版本。

············ 所需的 Gradle 版本
1.0.0 - 1.1.3 2.2.1 - 2.3
1.2.0 - 1.3.1 2.2.1 - 2.9
1.5.0 2.2.1 - 2.13
2.0.0 - 2.1.2 2.10 - 2.13
2.1.3 - 2.2.3 2.14.1+
2.3.0+ 3.3+
3.0.0+ 4.1+
3.1.0+ 4.4+
3.2.0 - 3.2.1 4.6+
3.3.0 - 3.3.2 4.10.1+
3.4.0 - 3.4.1 5.1.1+
3.5.0-3.5.3 5.4.1+
3.6.0+ 5.6.4+

您可以在 Android Studio 的 File > Project Structure > Project 菜单中指定 Gradle 版本,也可以通过在 gradle/wrapper/gradle-wrapper.properties 文件中修改 Gradle 分发引用来指定。以下示例在 gradle-wrapper.properties 文件中将 Gradle 的版本设置为 5.4.1。

    ...
    distributionUrl = https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
    ...
    

 

3.6.0(2020 年 2 月)

此版本的 Android 插件需要使用以下工具:

新功能

此版本的 Android Gradle 插件包含以下新功能。

视图绑定

在代码中引用视图时,视图绑定可确保编译时安全性。您现在可以将 findViewById() 替换为自动生成的绑定类引用。如需开始使用视图绑定,请在每个模块的 build.gradle 文件中添加以下代码:

android {
        viewBinding.enabled = true
    }
    </
  • 7
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值