最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Android Studio中给Gradle添加设置代理

Android crifan 69514浏览 0评论
折腾:
【记录】Android Studio根据提示升级到最新4.0.1版本和更新其他内容
期间,在升级了最新版 4.0.1的Android Studio后,弹框提示关于Gradle的代理设置:
Android Studio is configured to use HTTP proxy
Gradle may need these HTTP proxy settings to access the Internet(e.g. for downloading dependencies)
Do you want to store the following HTTP settings into the global gradle.properties file?
Note: You can manually set passwords in the gradle.properties file at your own risk

For more details, please refer to the Android Studio Documentation
当时考虑到:由于自己之前已经更新换了gradle的源,所以无需代理
所以取消勾选
另外根据提示:
去更新gradle:
To take advantage of the latest features, improvements, and security fixes, we strongly recommend that you update the Android Gradle plugin from the current version 3.5.4 to version 4.0.1 and Gradle to version 6.1.1. Release notes
Android Gradle 插件版本说明  |  Android 开发者  |  Android Developers
点击Update
Download https://services.gradle.org/distributions/gradle-6.1.1-all.zip (138.58 MB)
[  3%] [ =>                                                     ] 5.25 MB
结果下载很慢
之前提示给gradle加代理 没加 这下速度太慢了。
可以安装之前方法,去手动下载,重试,即可。
不过也还是去给gradle加代理去
取消
Sync finish event has not been received
然后去找配置gradle的代理的地方
然后期间去:
【已解决】Android Studio中给AS本身设置代理
回头看之前弹框,意思是:
Android Studio is configured to use HTTP proxy
Gradle may need these HTTP proxy settings to access the Internet(e.g. for downloading dependencies)
Do you want to store the following HTTP settings into the global gradle.properties file?
看起来是:
可以把 此处Android Studio的代理配置,设置加到项目的全局的gradle.properties文件中
搜:
Do you want to store the following HTTP settings into the global gradle.properties file
java – Global gradle proxy settings? – Stack Overflow
看起来是,把配置加到当前用户下的gradle中
Build Environment
Gradle provides multiple mechanisms for configuring behavior of Gradle itself and specific projects. The following is a reference for using these mechanisms.
When configuring Gradle behavior you can use these methods, listed in order of highest to lowest precedence (first one wins):
  • * Command-line flags such as –build-cache. These have precedence over properties and environment variables.
  • * System properties such as systemProp.http.proxyHost=somehost.org stored in a gradle.properties file.
  • * Gradle properties such as org.gradle.caching=true that are typically stored in a gradle.properties file in a project root directory or GRADLE_USER_HOME environment variable.
  • * Environment variables such as GRADLE_OPTS sourced by the environment that executes Gradle.
此处很明显就是:
system Properties系统属性,放在全局的,当前用户的下面的
Accessing the web through a HTTP proxy
“Accessing the web through a HTTP proxy
Configuring an HTTP or HTTPS proxy (for downloading dependencies, for example) is done via standard JVM system properties. These properties can be set directly in the build script; for example, setting the HTTP proxy host would be done with System.setProperty(‘http.proxyHost’, ‘www.somehost.org’). Alternatively, the properties can be specified in gradle.properties.
Configuring an HTTP proxy using gradle.properties
systemProp.http.proxyHost=www.somehost.org
systemProp.http.proxyPort=8080
systemProp.http.proxyUser=userid
systemProp.http.proxyPassword=password
systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost
There are separate settings for HTTPS.
Configuring an HTTPS proxy using gradle.properties
systemProp.https.proxyHost=www.somehost.org
systemProp.https.proxyPort=8080
systemProp.https.proxyUser=userid
systemProp.https.proxyPassword=password
systemProp.https.nonProxyHosts=*.nonproxyrepos.com|localhost
目前对于后续gradle内容的下载,都是https,包括上面的:
https://services.gradle.org/distributions/gradle-6.1.1-all.zip
所以,此处暂时只去设置https的代理即可。
但是,不知道对于:
systemProp.https.proxyHost
如何去设置使用socks的代理,比如:
export HTTP_PROXY=http://127.0.0.1:58591; export HTTPS_PROXY=http://127.0.0.1:58591; export ALL_PROXY=socks5://127.0.0.1:51837
中的:
ALL_PROXY=socks5://127.0.0.1:51837 
如果实在不行,那只能去改为:
http和https,都用本地127.0.0.1的58591的
systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=58591
systemProp.https.proxyHost=127.0.0.1
systemProp.https.proxyPort=58591
然后如果只是单个gradle项目,则放到gradle.properties即可
不过发现此rcsjta中并没有gradle.properties
所以,改去找当前用户下面看看
确认的确有~/.gradle/gradle.properties:
~  ll ~/.gradle/gradle.properties
-rw-r--r--  1 xxx  CORP\Domain Users   775B  8  5 15:21 /Users/xxx/.gradle/gradle.properties
然后再去打开看看
/Users/xxx/.gradle/gradle.properties
systemProp.http.proxyHost=
systemProp.http.proxyPort=80
然后去加上:
# systemProp.http.proxyHost=
# systemProp.http.proxyPort=80
systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=58591
systemProp.https.proxyHost=127.0.0.1
systemProp.https.proxyPort=58591
看看效果:
然后再去继续下载gradle的zip文件试试
为了确保生效,重启Android Studio,结果:
下载速度明显快很多:感觉 > 1MB/s
【总结】
此处,升级了Android Studio,弹框问是否要设置Gradle的代理:
其中的:
  • HTTP
    • host
    • No proxy for
    • Proxy authentication
      • Login
      • Password
  • HTTPS
    • host
    • No proxy for
    • Proxy authentication
      • Login
      • Password
等内容,其实就是对应着:
gradle官网
Accessing the web through a HTTP proxy
中提到的:
  • Configuring an HTTP proxy using gradle.properties
systemProp.http.proxyHost=www.somehost.org
systemProp.http.proxyPort=8080
systemProp.http.proxyUser=userid
systemProp.http.proxyPassword=password
systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost
和:
  • Configuring an HTTPS proxy using gradle.properties
systemProp.https.proxyHost=www.somehost.org
systemProp.https.proxyPort=8080
systemProp.https.proxyUser=userid
systemProp.https.proxyPassword=password
systemProp.https.nonProxyHosts=*.nonproxyrepos.com|localhost
系统属性systemProp
中的http和https的
proxyHost
proxyPort
如果要登录认证,还要:
proxyUser
proxyPassword
以及有哪些特殊的url需要被排除掉,可以加到:
nonProxyHosts
而此处,想要全局的gradle都用上代理,可以去:
把这部分代理配置,加到gradle的全局配置文件
即:当前用户下面的gradle.properties中
即:
  • Linux系(Linux/Mac等)
    • $HOME/.gradle/gradle.properties
  • Win系
    • %userprofile%\.gradle\gradle.properties
在其中加上对应配置,比如:
systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=58591
systemProp.https.proxyHost=127.0.0.1
systemProp.https.proxyPort=58591
即可。
其中此处的proxyHost和proxyPort,是我此处Trojan的代理配置。
(可以在Trojan-QT5的界面中看到,或 从菜单 复制终端代理命令 中拷贝出来)
注:
更新了
java – Global gradle proxy settings? – Stack Overflow
帖子的回复。

转载请注明:在路上 » 【已解决】Android Studio中给Gradle添加设置代理

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
93 queries in 0.250 seconds, using 23.40MB memory