简体   繁体   English

Android Studio 中的 gradle sync 是什么?

[英]What is gradle sync in Android Studio?

What is it?它是什么? And what does it do?它有什么作用? I'm working on an enterprise that has a proxy, and it fails trying to connect to somewhere.我在一家拥有代理的企业工作,但它无法尝试连接到某个地方。 Why does it needs internet connection?为什么需要互联网连接? What ports does it use?它使用什么端口?

EDIT: The answer of the user Caleb was perfect.编辑:用户 Caleb 的回答是完美的。 I would like to add that the proxy should be configured too in the gradle.properties.我想补充一点,代理也应该在 gradle.properties 中配置。 Something like this:像这样:

systemProp.http.proxyHost=*proxyAddress*

systemProp.http.proxyPort=*portNumber*

systemProp.https.proxyHost=*proxyAddress*

systemProp.https.proxyPort=*portNumber*

What is it?它是什么? And what does it do?它有什么作用?

Gradle sync is a gradle task that looks through all of your dependencies listed in your build.gradle files and tries to download the specified version. Gradle sync是一项 gradle 任务,它会查看 build.gradle 文件中列出的所有依赖项并尝试下载指定版本。

dependencies {
     compile '...your dependency...'
}

Why does it needs internet connection?为什么需要互联网连接? What ports does it use?它使用什么端口?

It requires an internet connection because it is usually downloading these dependencies from a remote location.它需要互联网连接,因为它通常从远程位置下载这些依赖项。 You can define what ports it uses by changing your gradle.properties.您可以通过更改 gradle.properties 来定义它使用的端口。 (see below) (见下文)

I'm working on an enterprise that has a proxy, and it fails trying to connect to somewhere.我在一家拥有代理的企业工作,但它无法尝试连接到某个地方。

Your work proxy may be blocking this and you'll need to add your proxy configuration to solve your issues.您的工作代理可能会阻止此操作,您需要添加代理配置来解决您的问题。

Go into:进去:

File-->Settings--> Android Studio Preferences --> Appearance & Behavior / System Settings/ HTTP Proxy文件-->设置-->Android Studio首选项-->外观和行为/系统设置/HTTP代理

and update your proxy configuration url to your work proxy.并将您的代理配置 url 更新到您的工作代理。 (automatic or manual depending on your setup). (自动或手动取决于您的设置)。

NOTE : If you are using the command line to run your gradle build, you will probably need to update the proxy settings via your gradle.properties file.注意:如果您使用命令行运行 gradle 构建,您可能需要通过 gradle.properties 文件更新代理设置。

Global Properties File Location : ~/.gradle/gradle.properties (or use your local project file if you have one)全局属性文件位置~/.gradle/gradle.properties (如果有本地项目文件,也可以使用)

Add proxy settings to this file:将代理设置添加到此文件:

HTTPS HTTPS

systemProp.https.proxyHost=<proxy host>
systemProp.https.proxyPort=<your proxy port>
systemProp.https.nonProxyHosts=<your non-proxy host>
systemProp.https.proxyPassword=<your pw>

HTTP HTTP

systemProp.http.proxyHost=<proxy host>
systemProp.http.proxyPort=<your proxy port>
systemProp.http.nonProxyHosts=<your non-proxy host>
systemProp.http.proxyPassword=<your pw>

If you absolutely cannot get an internet connection via gradle you'll need to download the dependencies another way and reference them locally on your computer or local network.如果您绝对无法通过 gradle 获得互联网连接,您将需要以另一种方式下载依赖项,并在您的计算机或本地网络上本地引用它们。

(See this guide for using local jars) (有关使用本地罐子的信息,请参阅指南)

What is it?它是什么?

I believe it's an IntelliJ/Android Studio term for these Gradle tasks:我相信这是这些 Gradle 任务的 IntelliJ/Android Studio 术语:

  1. dependencies
  2. build

Gradle itself doesn't have "sync" task. Gradle 本身没有“同步”任务。

I use the term "believe" since there's no explanation from Gradle/IntelliJ/Android Studio official documentation我使用“相信”这个词是因为 Gradle/IntelliJ/Android Studio 官方文档没有解释

And what does it do?它有什么作用? Sync is a phase where the IDE preparing everything, including downloading your dependencies.同步是 IDE 准备一切的阶段,包括下载您的依赖项。

When the sync is finished, the user can start coding.同步完成后,用户可以开始编码。

When the sync fails, the IDE is not working properly.当同步失败时,IDE 无法正常工作。 The user need to spend time to fix the configuration first.用户需要先花时间修复配置。

After Gradle finished syncing, the IDE start another task, like indexing. Gradle 完成同步后,IDE 开始另一个任务,比如索引。

Gradle: Build...
Gradle: Configure projects...
Indexing...

I'm working on an enterprise that has a proxy, and it fails trying to connect to somewhere.我在一家拥有代理的企业工作,但它无法尝试连接到某个地方。 Why does it needs internet connection?为什么需要互联网连接? What ports does it use?它使用什么端口?

Answered above.上面回答了。

Bonus: How to speed up sync奖励:如何加速同步

Before opening the project using Android Studio/IntelliJ, open the project using terminal and run the gradle tasks.在使用 Android Studio/IntelliJ 打开项目之前,使用终端打开项目并运行 gradle 任务。

cd myproject
./gradlew app:dependencies module:dependencies
./gradlew build

After that, open the project.之后,打开项目。 The sync should be much faster now (since you've downloaded the dependencies in terminal).现在同步应该快得多(因为您已经在终端中下载了依赖项)。

It needs internet connection to download dependencies它需要互联网连接来下载依赖项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM