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

查看:1380
本文介绍了什么是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包装器命令行工具执行所有可用于Android项目的构建任务.它可以作为Windows的批处理文件(gradlew.bat)和Linux和Mac的shell脚本(gradlew)使用.sh),并且可以从您使用Android Studio创建的每个项目的根目录中访问."

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

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

在Windows上: gradlew任务名称

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 Android studio installed and it is using gradle to build my apps (so gradle is already installed in my system) ,in that case , 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包装器是Gradle构建系统的可选部分.它由四个文件组成,您可以将它们检入版本控制系统. * nix启动脚本<your root project>/gradlew<your root project>/gradlew.bat Windows启动脚本,<your root project>/gradle/wrapper/gradle-wrapper.jar(包含包装器的类文件,由启动脚本启动)和<your root project>/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 *nix 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.如果您在<root project dir>/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天全站免登陆