什么是 gradle 包装器和 gradlew.bat 文件? [英] What is gradle wrapper and the gradlew.bat file?

查看:46
本文介绍了什么是 gradle 包装器和 gradlew.bat 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始了解 Gradle 作为构建系统的强大功能,作为一名 Android 开发人员,我想深入了解它.

Lately I came to know the power of Gradle as a build system and as an Android developer I wanna understand it deeply.

一篇文章如下:

您可以使用 Gradle 包装器 命令行工具.它可用作 Windows 的批处理文件 (gradlew.bat) 和 Linux 和 Mac 的 shell 脚本 (gradlew.sh),并且可以从每个项目的根目录访问它您使用 Android Studio 创建.

You can execute all the build tasks available to your Android project using the Gradle wrapper command line tool. It's available as a batch file for Windows (gradlew.bat) and a shell script for Linux and Mac (gradlew.sh), and it's accessible from the root of each project you create with Android Studio.

要使用包装器运行任务,请使用以下命令之一:

To run a task with the wrapper, use one of the following commands:

  • 在 Windows 上:
  • On Windows:
gradlew task-name

现在我有一些疑问,如下:

Now I have some doubts which goes as follow:

  1. 什么是 Gradle Wrapper 和 gradlew.bat?
  2. 如果我已经安装了 Android studio 并且它正在使用 gradle 来构建我的应用程序(所以 gradle 已经安装在我的系统上),我是否仍然需要从命令行安装 gradle 以进行构建?当我在命令行上写任何像 gradle, gradlew 这样的推荐时,我收到错误消息,说 gradlew 不被识别为内部或外部命令(其他命令的错误相同).我可能在错误的路径上使用它,请帮助我在什么路径上使用 Gradle 相关命令?
  3. 如果我需要下载并安装它,如何以及在哪里可以找到该文件?其他流程呢?
  1. What is Gradle Wrapper and gradlew.bat?
  2. If I've got Android studio installed and it is using gradle to build my apps (so gradle is already installed on my system), do I still need to install gradle for build purpose from command line? As when i write any commend like gradle, gradlew on my command line I get error saying gradlew is not recognized as internal or external command (the same error for other commands). I may be using it on wrong path, help me on what path do I need to use Gradle related command?
  3. If I need to download and install it, how and where can I find the file? And the other processes?

为此我使用的是 Windows 机器.

I am using a Windows machine for this.

推荐答案

Gradle Wrapper 是 Gradle 构建系统的可选部分.它由您签入版本控制系统的四个文件组成.Unix启动脚本/gradlew/gradlew.bat Windows启动脚本,;/gradle/wrapper/gradle-wrapper.jar 包含包装器的类文件,由启动脚本和 /gradle/wrapper/gradle-wrapper.properties 启动 包含包装器的一些配置,例如用于构建项目的 Gradle 版本.

The Gradle Wrapper is an optional part of the Gradle build system. It consists of four files that you check into version control system. The Unix start script <your root project>/gradlew, the <your root project>/gradlew.bat Windows start script, <your root project>/gradle/wrapper/gradle-wrapper.jar which contains the class files for the wrapper and is started by the start scripts and <your root project>/gradle/wrapper/gradle-wrapper.properties which contains some configuration for the wrapper, for example which Gradle version to use to build the project.

在我看来,每一个 Gradle 项目,即使是最小的,都应该使用 Gradle 包装器.

In my opinion, each and every Gradle project, even the tiniest, should make use of the Gradle wrapper.

Gradle 包装器可确保您的构建始终使用相同的 Gradle 版本运行,无论是谁执行构建,也无论 Gradle 安装在何处或未安装,只要使用 Gradle 包装器构建项目即可.这意味着您可以针对该 Gradle 版本设计构建,并确保构建不会因为有人使用不同版本的 Gradle 而失败,因此这也是构建可再现性的重要一步.

The Gradle wrapper makes sure your build is always run with the same Gradle version, no matter who executes the build and where or that Gradle is installed or not, as long as the one uses the Gradle wrapper to build the project. This means you can design your build for that Gradle version and be sure that the build will not fail, just because someone is using a different version of Gradle and thus is also an important step in build reproducibility.

此外,希望构建您的项目的人只需要安装 Java,仅此而已.他不需要安装任何 Gradle 版本.实际上任何已经安装的 Gradle 版本都会被忽略.Gradle 包装器检查在 ~/.gradle/ 中是否已经存在构建所需的版本,因为任何项目的一些 Gradle 包装器已经将它放在那里.如果已经存在,则使用,否则自动下载.

Also, someone wishing to build your project only needs to have Java installed and that's it. He does not need to have any Gradle version installed. Actually any already installed Gradle version is ignored. The Gradle wrapper checks whether in ~/.gradle/ the version that is necessary for the build is already present, because some Gradle wrapper of any project put it there already. If it is present already, it is used, otherwise it is automatically downloaded.

如果你在命令行输入 gradlew 并且没有找到命令,那意味着你没有把你的根项目路径放到 PATH 环境变量中(我也不建议这样做),您当前不在根项目目录中.要运行 Gradle 构建,您必须在项目内的任何位置调用 Gradle 或 Gradle 包装器.但是就像任何不在路径上的可执行文件一样,您当然必须提供其路径.所以如果你在你的根项目目录中,你可以简单地做 gradlew.如果你在 /foo/bar/,你需要调用 ../../gradlew.

If you type gradlew on the commandline and the command is not found, that means you didn't put your root projects path to the PATH environment variable (I wouldn't recommend doing that either), not are you currently in your root projects directory. To run a Gradle build, you have to be anywhere inside your project and call Gradle or the Gradle wrapper. But like with any executable file that is not on the path, you have to provide its path of course. So if you are in your root project directory, you can simply do gradlew. if you are in <root project dir>/foo/bar/, you would need to call ../../gradlew.

Gradle Wrapper 文件由隐式可用的 Gradle 任务 wrapper 生成,然后被签入相关项目的 VCS.如果项目不存在这四个文件,则它不使用 Gradle 包装器,您应该向项目提交改进请求以添加它.

The Gradle Wrapper files are generated by the implicitly available Gradle task wrapper and then get checked into the VCS of the project in question. If those four files are not present for a project, it does not use the Gradle wrapper and you should post an improvement request to the project to add it.

如果某些项目不使用 Gradle 包装器,而是使用 Gradle 构建,您可以安装 Gradle 并使用 gradle 代替 gradlew,或者您甚至可以调用您在磁盘上可用的任何其他项目的 Gradle 包装器.然后构建将使用包装器或 Gradle 安装正在使用的 Gradle 版本运行,因此可能不会按预期运行,这就是为什么如果每个项目使用 Gradle,实际上每个项目都应该使用包装器.

If some project does not use the Gradle wrapper, but builds with Gradle, you can either install Gradle and use gradle instead of gradlew, or you can even call the Gradle wrapper of any other project that you have available on disk. The build will then be run with the Gradle version that wrapper or Gradle installation is using and thus might not behave as expected, which is why really each and any project should use the wrapper if it uses Gradle.

这篇关于什么是 gradle 包装器和 gradlew.bat 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆