Gradle Offline Mode – Gradle离线模式

最后修改: 2021年 8月 26日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Gradle is the build tool of choice for millions of developers around the globe and is the official build tool for Android applications.

Gradle是全球数百万开发者的首选构建工具,是Android应用程序的官方构建工具。

We usually use Gradle to download dependencies from the network, but sometimes we can’t access the network. In these situations, Gradle’s offline mode will be useful.

我们通常使用Gradle从网络上下载依赖项,但有时我们无法访问网络。在这种情况下,Gradle的离线模式会很有用。

In this short tutorial, we’ll talk about how to achieve offline mode in Gradle.

在这个简短的教程中,我们将谈论如何在Gradle中实现离线模式。

2. Prepare

2.准备

Before going for the offline mode, we need to install Gradle first. And then we need to build our applications to download all their dependencies, otherwise, we will fail when we try using the offline mode.

在进行离线模式之前,我们需要先安装Gradle。然后我们需要构建我们的应用程序以下载所有的依赖,否则,当我们尝试使用离线模式时,我们会失败。

3. Offline Mode

3.脱机模式

We usually use Gradle in command-line tools or IDEs (like JetBrains IntelliJ IDEA and Eclipse), so we mainly learn how to use offline mode in these tools.

我们通常在命令行工具或IDE(如JetBrains IntelliJ IDEA和Eclipse)中使用Gradle,所以我们主要学习如何在这些工具中使用离线模式。

3.1. Command-Line

3.1 命令行

Once we’ve installed Gradle in our system, we can download dependencies and build our applications:

一旦我们在系统中安装了Gradle,我们就可以下载依赖项并构建我们的应用程序。

gradle build

And now, we can achieve the offline mode by adding the –offline option:

而现在,我们可以通过添加-offline选项实现离线模式。

gradle --offline build

3.2. JetBrains IntelliJ IDEA

3.2.JetBrains IntelliJ IDEA

When we use IntelliJ, we can integrate and configure Gradle with it, and then we’ll see the Gradle window.

当我们使用IntelliJ时,我们可以用它来集成和配置Gradle,然后我们会看到Gradle窗口。

If we need to use the offline mode, just go to the Gradle window and click the Toggle Offline Mode button:

如果我们需要使用离线模式,只需进入Gradle窗口,点击Toggle Offline Mode按钮。

After we click the button to enable offline mode, we can reload all dependencies and find that offline mode works.

在我们点击按钮启用脱机模式后,我们可以重新加载所有的依赖关系,并发现脱机模式可以工作。

3.3. Eclipse

3.3.日蚀

Finally, let’s see how to achieve the offline mode in Eclipse. We can find the Gradle configuration in Eclipse by navigating to the Preferences -> Gradle section. We can see the Offline Mode config and toggle it off:

最后,让我们看看如何在Eclipse中实现脱机模式。我们可以通过导航到Preferences -> Gradle部分找到Eclipse中的Gradle配置。我们可以看到Offline Mode配置,并把它关掉。

As a result, we’ll find that the offline mode works in Eclipse.

因此,我们会发现,离线模式在Eclipse中是可行的。

4. Conclusion

4.总结

In this quick tutorial, we talked about the offline mode in Gradle. We learned how to enable offline mode from the command line, as well as from two popular IDEs: Eclipse and IntelliJ.

在这个快速教程中,我们谈到了Gradle的离线模式。我们学习了如何从命令行以及两个流行的IDE中启用离线模式。Eclipse和IntelliJ。