Android Studio 中的 Gradle 是什么? [英] What is Gradle in Android Studio?

查看:30
本文介绍了Android Studio 中的 Gradle 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Gradle 对我来说有点困惑,对任何新的 Android 开发者也是如此.谁能解释一下 Android Studio 中的 Gradle 是什么以及它的目的是什么?为什么它包含在 Android Studio 中?

Gradle is a bit confusing to me, and also for any new Android developer. Can anyone explain what Gradle in Android Studio is and what its purpose is? Why is it included in Android Studio?

推荐答案

简答

Gradle 是一个构建系统.

Short Answer

Gradle is a build system.

在 Android Studio 之前,您使用 Eclipse 进行开发,并且,很有可能,您不知道如何在没有 Eclipse 的情况下构建您的 Android APK.

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.

您可以在命令行上执行此操作,但您必须了解每个工具(dxAAPT) 在 SDK 中.Eclipse 为我们提供了自己的构建系统,让我们免于这些低级但重要的基本细节.

You can do this on the command line, but you have to learn what each tool (dx and AAPT) does in the SDK. Eclipse saved us all from these low-level, but important, fundamental details by giving us their own build system.

现在,您有没有想过为什么 res 文件夹与您的 src 文件夹在同一目录中?

Now, have you ever wondered why the res folder is in the same directory as your src folder?

这是构建系统发挥作用的地方.构建系统会自动获取所有源文件(.java.xml),然后应用适当的工具(例如,获取 .java 类文件并将它们转换为 .dex 文件),并将它们组合成一个压缩文件 - 我们心爱的 APK.

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 (e.g., takes .java class files and converts them to .dex files), and groups all of them into one compressed file - our beloved APK.

这个构建系统使用了一些约定:一个例子是指定包含源文件的目录(在 Eclipse 中它是 \src 文件夹)或资源文件(在 Eclipse 中它是 \res 文件夹).

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).

现在,为了自动化所有这些任务,必须有一个脚本;您可以使用 Linux 中的 shell 脚本或 Windows 中的批处理文件语法编写自己的构建系统.明白了吗?

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. Got it?

Gradle 是另一个构建系统,它从其他构建系统中汲取了最佳功能并将它们组合在一起.它是根据他们的缺点进行改进的.它是一个基于 JVM 的构建系统.这意味着您可以使用 Android Studio 使用的 Java 编写自己的脚本.

Gradle is another build system that takes the best features from other build systems and combines them into one. It is improved based off of their shortcomings. It is a JVM-based build system. That means you can write your own script in Java, which Android Studio makes use of.

Gradle 的一个很酷的地方是它是一个基于插件的系统.这意味着,如果您有自己的编程语言,并且想要自动化构建某个包的任务(输出如 Java 的 JAR 文件),然后您可以用 Java 或 Groovy(或 Kotlin,参见 此处),并将其分发到世界其他地方.

One cool thing about Gradle is that it is a plugin-based system. This means if you have your own programming language and you want to automate the task of building some package (output like a JAR file for Java) from sources, then you can write a complete plugin in Java or Groovy (or Kotlin, see here), and distribute it to the rest of the world.

Google 看到了市场上最先进的构建系统之一,并意识到您无需学习 Groovy 或任何其他新语言即可编写自己的脚本,几乎不需要学习曲线.所以他们为 Gradle 编写了 Android 插件.

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. So they wrote the Android plugin for Gradle.

您必须在项目中看到 build.gradle 文件.在这里您可以编写脚本来自动化您的任务.您在这些文件中看到的代码是 Groovy 代码.如果您编写 System.out.println("Hello Gradle!"); 那么它将打印在您的控制台上.

You must have seen build.gradle file(s) in your project. That is where you can write scripts to automate your tasks. The code you saw in these files is Groovy code. If you write System.out.println("Hello Gradle!"); then it will print on your console.

一个简单的例子是,在实际构建过程发生之前,您必须将一些文件从一个目录复制到另一个目录.Gradle 构建脚本可以做到这一点.

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.

这篇关于Android Studio 中的 Gradle 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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