本文翻译自:What is Gradle in Android Studio?
Gradle is a bit confusing to me, and also for any new android developer. Gradle让我有些困惑,对于任何新的android开发人员也是如此。 Can anyone explain what Gradle in Android Studio is and what its purpose is? 谁能解释Android Studio中的Gradle是什么,它的目的是什么? Why is it included in Android Studio? 为什么它包含在Android Studio中?
#1楼
参考:https://stackoom.com/question/18IeB/Android-Studio中的Gradle是什么
#2楼
Gradle is to the Groovy JVM language what ant is to Java. 对于Groovy JVM语言,Gradle对Java来说就是蚂蚁。 Basically, it's Groovy's build tool. 基本上,它是Groovy的构建工具。 Unlike Ant, it's based on the full Groovy language. 与Ant不同,它基于完整的Groovy语言。 You can, for example, write Groovy script code in the Gradle script to do something rather than relying on a specific domain language. 例如,您可以在Gradle脚本中编写Groovy脚本代码,以执行某些操作,而不是依赖特定的领域语言。
I don't know IntelliJ's specific integration, but imagine you could "extend" Groovy such that you could write specific "build" language primitives and they just became part of the Groovy language. 我不知道IntelliJ的特定集成,但是想象一下您可以“扩展” Groovy,以便您可以编写特定的“构建”语言原语,而它们只是成为Groovy语言的一部分。 (Groovy's metaprogramming is a whole discussion unto itself.) IntelliJ/Google could use Gradle to build a very high-level build language, yet, it's a language build on an expandable, open standard. (Groovy的元编程本身是一个完整的讨论。)IntelliJ / Google可以使用Gradle构建非常高级的构建语言,但它是基于可扩展的开放标准的语言。
#3楼
It's the new build tool that Google wants to use for Android. 这是Google希望用于Android的新构建工具。 It's being used due to it being more extensible, and useful than ant. 之所以使用它,是因为它比ant更可扩展且更有用。 It is meant to enhance developer experience. 它旨在增强开发人员的体验。
You can view a talk by Xavier Ducrohet from the Android Developer Team on Google I/O here. 您可以在此处查看Google I / O上Android开发人员团队的Xavier Ducrohet的演讲。
There is also another talk on Android Studio by Xavier and Tor Norbye, also during Google I/O here . Xavier和Tor Norbye在Android Studio上也有另一个演讲,也在Google I / O上进行 。
#4楼
Gradle is one type of build tool that builds the source code of the program. Gradle是一种构建工具 ,用于构建程序的源代码。 So it's an important part of Android Studio, and needs to be installed before starting developing your application. 因此,它是Android Studio的重要组成部分,需要在开始开发应用程序之前进行安装。
We do not have to install it separately, because the Android Studio does it for us, when we make our first project. 我们不必单独安装它,因为当我们制作第一个项目时,Android Studio会为我们完成安装。
#5楼
Gradle is an advanced build toolkit for android that manages dependencies and allows you to define custom build logic. Gradle是用于android的高级构建工具包,可管理依赖项并允许您定义自定义构建逻辑。 features are like 功能就像
Customize, configure, and extend the build process. 自定义,配置和扩展构建过程。
Create multiple APKs for your app with different features using the same project. 使用同一项目为您的应用创建多个具有不同功能的APK。
Reuse code and resources. 重用代码和资源。
#6楼
Short Answer 简短答案
Gradle is a build system. Gradle是一个构建系统。
Long Answer 长答案
Before Android Studio you were using Eclipse for your development purposes, and, chances are, you didn't know how to build your Android APK without Eclipse. 在使用Android Studio之前,您出于开发目的而使用Eclipse,而且很可能,您不知道如何在没有Eclipse的情况下构建Android APK。
You can do this on the command line, but you have to learn what each tool (dx, aapt) does in the SDK. 您可以在命令行上执行此操作,但是您必须了解SDK中每个工具(dx,aapt)的作用。 Eclipse saved us all from these low level but important, fundamental details by giving us their own build system. Eclipse通过提供给我们自己的构建系统,使我们所有人从这些低级但重要的基本细节中解脱出来。
Now, have you ever wondered why the res
folder is in the same directory as your src
folder? 现在,您是否想知道res
文件夹为什么与src
文件夹位于同一目录?
This is where the build system enters the picture. 这是构建系统输入图片的地方。 The build system automatically takes all the source files ( .java
or .xml
), then applies the appropriate tool (eg takes java
class files and converts them to dex
files), and groups all of them into one compressed file, our beloved APK. 构建系统会自动获取所有源文件( .java
或.xml
),然后应用适当的工具(例如,获取java
类文件并将其转换为dex
文件),并将它们全部分组为一个压缩文件,即我们心爱的APK。
This build system uses some conventions: an example of one is to specify the directory containing the source files (in Eclipse it is \\src
folder) or resources files (in Eclipse it is \\res
folder). 此构建系统使用一些约定:一个示例是指定包含源文件(在Eclipse中是\\src
文件夹)或资源文件(在Eclipse中是\\res
文件夹)的目录。
Now, in order to automate all these tasks, there has to be a script; 现在,为了使所有这些任务自动化,必须有一个脚本。 you can write your own build system using shell scripting in linux or batch files syntax in windows. 您可以使用Linux中的Shell脚本或Windows中的批处理文件语法编写自己的构建系统。 Got it? 得到它了?
Gradle is another build system that takes the best features from other build systems and combines them into one. Gradle是另一个构建系统 ,它吸收了其他构建系统中的最佳功能,并将它们组合为一个。 It is improved based off of their shortcomings. 由于它们的缺点而进行了改进。 It is a JVM based build system , what that means is that you can write your own script in Java, which Android Studio makes use of. 这是一个基于JVM的构建系统 ,这意味着您可以用Java编写自己的脚本,而Android Studio会利用该脚本。
One cool thing about gradle is that it is a plugin based system . 关于gradle的一件很酷的事情是它是一个基于插件的系统 。 This means if you have your own programming language and you want to automate the task of building some package (output like a JAR for Java) from sources then you can write a complete plugin in Java or Groovy(or Kotlin, see here ), and distribute it to rest of world. 这意味着,如果您拥有自己的编程语言,并且想要自动化从源代码构建某些程序包(如Java的JAR的输出)的任务,则可以用Java或Groovy(或Kotlin,请参见此处 )编写完整的插件,并且分发到世界其他地方。
Why did Google use it? Google为什么使用它?
Google saw one of the most advanced build systems on the market and realized that you could write scripts of your own with little to no learning curve, and without learning Groovy or any other new language. Google看到了市场上最先进的构建系统之一,并意识到您无需编写Groovy或任何其他新语言,几乎不需要学习就可以编写自己的脚本。 So they wrote the Android plugin for Gradle. 因此,他们为Gradle编写了Android插件。
You must have seen build.gradle
file(s) in your project. 您必须在项目中看到过build.gradle
文件。 That is where you can write scripts to automate your tasks. 在这里您可以编写脚本来自动执行任务。 The code you saw in these files is Groovy code. 您在这些文件中看到的代码是Groovy代码。 If you write System.out.println("Hello Gradle!");
如果您编写System.out.println("Hello Gradle!");
then it will print on your console. 然后它将在您的控制台上打印。
What can you do in a build script? 您可以在构建脚本中做什么?
A simple example is that you have to copy some files from one directory to another before the actual build process happens. 一个简单的示例是,在实际的构建过程发生之前,您必须将一些文件从一个目录复制到另一个目录。 A Gradle build script can do this. Gradle构建脚本可以做到这一点。