什么是 Android Studio 中的 Gradle?

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

转载于:https://stackoverflow.com/questions/16754643/what-is-gradle-in-android-studio

csdnceshi62
csdnceshi62 May look at concise and Nice explanation here Introducing gradle. Quite helpful
大约 4 年之前 回复
csdnceshi51
旧行李 Exactly my thoughts. I am still a little confused, is gradle just the background process that compiles the code I have written? Also, after looking at lots of other answers, they mention dependencies. What are those? THanks so much!
4 年多之前 回复
csdnceshi75
衫裤跑路 When someone asks a question on here that yes, does have a lot of links in Google about it, what they are really asking is for someone to answer the question with plain language and to put it in context, such as why do we need it, who will use it most, do we need to know it to build Android apps in Android Studio, etc. Because most often the links you find about it on Google are in "tech-speak" and for a beginner this is confusing.
6 年多之前 回复
csdnceshi73
喵-见缝插针 To answer why you would want a build tool such as Gradle, check out this post from Jeff Atwood.
7 年多之前 回复
csdnceshi61
derek5. See gradle.org
7 年多之前 回复

22个回答

Short Answer

Gradle is a build system.

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.

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

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

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.

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

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 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, what that means is that you can write your own script in Java, which Android Studio makes use of.

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

Why did Google use it?

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.

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.

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.

csdnceshi57
perhaps? I think Gradle is a '.jar' handler for developers - Or tries to be. What does that have to do with building APK files. Eclipse could not single handedly export to APK, but Gradle could? - That seems to be the only point of substance you give in this answer.
接近 3 年之前 回复
csdnceshi55
~Onlooker Is it not possible to work in android studio without using gradle ?
大约 3 年之前 回复
weixin_41568134
MAO-EYE Excellent Information... cleared the concepts... But, What is the reason behind the sloth behaviour of gradle during the building process??? And are there any factors that can speed up the gradle build?
接近 4 年之前 回复
weixin_41568110
七度&光 Gradle is a terrible build system. In using Android Studio I want it to take care of the build. I don't understand why adding some classes forces Gradle to do a sync before it can be useful. Working with Visual Studio by comparison is awesome (or even X-Code) - no crappy extras needed. The IDE builds the project and solution without the need to tinker around in random files for settings that the Android build process needs at times or through updates of old projects. Gradle is the worst part of the Android experience.
接近 4 年之前 回复
weixin_41568183
零零乙 Yes gradle is another background process which compiles code.Dependency is nothing but a jar file path or source code path which you wish to use in your project in simple format.
4 年多之前 回复
csdnceshi66
必承其重 | 欲带皇冠 So is gradle just the background process that compiles the code I have written? Also, after looking at lots of other answers, they mention dependencies. What are those? THanks so much Gagandeep!
4 年多之前 回复
weixin_41568174
from.. Another key advantage of gradle over the pre Android studio build system(s) is dependency management. Previously to use an Android library in a project, one would have to download the source code, add it to your project and compile it with your project. Instead, with gradle one can add a single line to the build.gradle and gradle will then download the compiled library from a public repository and add it to your project. Some examples: blog.teamtreehouse.com/android-libraries-use-every-project This also works for standard Java jar files instead of putting them in the lib directory.
接近 5 年之前 回复
weixin_41568183
零零乙 You can write your own plugin in Java Or Groovy
大约 5 年之前 回复
csdnceshi77
狐狸.fox "This means if you have your own programming language ...you can write a complete plugin in Java or Groovy". There's no such thing as "your own programming language". You have to use Groovy.
大约 5 年之前 回复
csdnceshi59
ℙℕℤℝ I thought IntelliJ already knew how to build Android projects without the need for another build system.
大约 5 年之前 回复
csdnceshi64
游.程 build.gradle prints "Hello Gradle." This was awesome.
大约 5 年之前 回复
csdnceshi63
elliott.david Quote from the comment above by: Gagandeep Singh "The code you saw in these files is groovy code." Groovy is a Java extension: en.wikipedia.org/wiki/Groovy_%28programming_language%29
5 年多之前 回复
weixin_41568184
叼花硬汉 In which language is build.gradle written?
5 年多之前 回复
weixin_41568183
零零乙 : To generate simple apk android studio is provided over gradle so if you ask me then there is no need to go into internals of gradle as far as simple apk generation is cencerned. Gradle is build system and helps to achieve complex,dependent tasks in one go and automated way.
5 年多之前 回复
csdnceshi73
喵-见缝插针 What I don't understand is why gradle tries to connect to Internet for every single project that I make or even every single time I want to compile
5 年多之前 回复
weixin_41568127
?yb? I have to say that Gradle was giving me issues when taking an AndroidStudio project from one location to another. Mismatches of versions, missing modules, whatnot... it was giving me hard time. I ended up re-creating the project skeleton and cutting/pasting the parts of code that were from the old project to the new one. And this is under Windows for both sources and destination. I am going to move it now under a Linux-based lab.
5 年多之前 回复
weixin_41568183
零零乙 Gradle is coolest thing if we use it , example I had built system using gradle which based on task run picks dev or prod version of code and auto commits apk in svn and then emails that apk and message to email ids configured in gradle itself. So my manual tasks are over.
5 年多之前 回复

It's the new build tool that Google wants to use for Android. It's being used due to it being more extensible, and useful than 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.

There is also another talk on Android Studio by Xavier and Tor Norbye, also during Google I/O here.

weixin_41568174
from.. It doesn't compile the code, but it's responsible for calling the executable that does compile your code. After compiling your code, it can then run another task to package it up, another to copy it to a directory of your choosing, then maybe another to upload it to a server somewhere... It's those sorts of tasks Gradle, or any build system, will do.
2 年多之前 回复
weixin_41568134
MAO-EYE So is gradle just the background process that compiles the code I have written? Also, after looking at lots of other answers, they mention dependencies. What are those? THanks so much daniel!
4 年多之前 回复
csdnceshi71
Memor.の I thought IntelliJ already knew how to build Android projects without the need for another build system.
大约 5 年之前 回复

You can find everything you need to know about Gradle here: Gradle Plugin User Guide

Goals of the new Build System

The goals of the new build system are:

  • Make it easy to reuse code and resources
  • Make it easy to create several variants of an application, either for multi-apk distribution or for different flavors of an application
  • Make it easy to configure, extend and customize the build process
  • Good IDE integration

Why Gradle?

Gradle is an advanced build system as well as an advanced build toolkit allowing to create custom build logic through plugins.

Here are some of its features that made us choose Gradle:

  • Domain Specific Language (DSL) to describe and manipulate the build logic
  • Build files are Groovy based and allow mixing of declarative elements through the DSL and using code to manipulate the DSL elements to provide custom logic.
  • Built-in dependency management through Maven and/or Ivy.
  • Very flexible. Allows using best practices but doesn’t force its own way of doing things.
  • Plugins can expose their own DSL and their own API for build files to use.
  • Good Tooling API allowing IDE integration

In Android Studio, Gradle is a custom build tool used to build android packages (apk files) by managing dependencies and providing custom build logic.

APK file (Android Application package) is a specially formatted zip file which contains

  • Byte code
  • Resources (images, UI, xml etc)
  • Manifest file

An apk file gets signed and pushed to the device using ADB(Android Debug Bridge) where it gets executed.

Gradle is a build system running on Android Studio.

In other languages for example:

csdnceshi53
Lotus@ One of the fastest ways I've found to learn what an unknown tool does and where it falls in the pipeline, is to type into Google the name of the tool and "vs". The results will usually show "X vs Y" where Y is some other tool you might be more familiar with.
大约 2 年之前 回复
csdnceshi71
Memor.の PHING of PHP
大约 2 年之前 回复
csdnceshi65
larry*wei Simple answer !!!
大约 2 年之前 回复
csdnceshi66
必承其重 | 欲带皇冠 When you said Gradle is similar to make, I just got its concept
接近 3 年之前 回复

The Gradle build system is designed to support complex scenarios in creating Android applications:

Multi-distribution: the same application must be customized for several clients or companies

Multi-apk: supporting the creation of multiple apk for different device types while reusing parts of the code

Here is a detailed explanation about what Gradle is and how to use it in Android Studio.

Exploring the Gradle Files

  1. Whenever you create a project in Android Studio, the build system automatically generates all the necessary Gradle build files.

Gradle Build Files

  1. Gradle build files use a Domain Specific Language or DSL to define custom build logic and to interact with the Android-specific elements of the Android plugin for Gradle.

  2. Android Studio projects consists of 1 or more modules, which are components that you can build, test, and debug independently. Each module has its own build file, so every Android Studio project contains 2 kinds of Gradle build files.

  3. Top-Level Build File: This is where you'll find the configuration options that are common to all the modules that make up your project.

  4. Module-Level Build File: Each module has its own Gradle build file that contains module-specific build settings. You'll spend most of your time editing module-level build file(s) rather than your project's top-level build file.

To take a look at these build.gradle files, open Android Studio's Project panel (by selecting the Project tab) and expand the Gradle Scripts folder. The first two items in the Gradle Scripts folder are the project-level and module-level Gradle build files

Top-Level Gradle Build File

Every Android Studio project contains a single, top-level Gradle build file. This build.gradle file is the first item that appears in the Gradle Scripts folder and is clearly marked Project.

Most of the time, you won't need to make any changes to this file, but it's still useful to understand its contents and the role it plays within your project.

Module-Level Gradle Build Files

In addition to the project-level Gradle build file, each module has a Gradle build file of its own. Below is an annotated version of a basic, module-level Gradle build file.

Other Gradle Files

In addition to the build.gradle files, your Gradle Scripts folder contains some other Gradle files. Most of the time you won't have to manually edit these files as they'll update automatically when you make any relevant changes to your project. However, it's a good idea to understand the role these files play within your project.

gradle-wrapper.properties (Gradle Version)

This file allows other people to build your code, even if they don't have Gradle installed on their machine. This file checks whether the correct version of Gradle is installed and downloads the necessary version if necessary.

settings.gradle

This file references all the modules that make up your project.

gradle.properties (Project Properties)

This file contains configuration information for your entire project. It's empty by default, but you can apply a wide range of properties to your project by adding them to this file.

local.properties (SDK Location)

This file tells the Android Gradle plugin where it can find your Android SDK installation.

Note: local.properties contains information that's specific to the local installation of the Android SDK. This means that you shouldn't keep this file under source control.

Suggested reading - Tutsplus Tutorial

I got clear understanding of gradle from this.

csdnceshi61
derek5. Note that link-only answers are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference.
接近 5 年之前 回复

Gradle is a build system. Build systems are software tools designed to automate the process of program compilation. Build systems come in various forms, and are used for a variety of software build tasks. While their primary goal is to efficiently create executables.

Another related term is Build automation which is the process of automating the creation of a software build and the associated processes including: compiling computer source code into binary code, packaging binary code, and running automated tests.

Few similar build system for other languages are (see complete list here):

  1. Apache Ant & Apache Maven - Java
  2. sbt (Simple Build Tool) - for Scala (Play framework etc)
  3. A-A-P - Python based build tool
  4. Rake (Apache Builder) - Ruby
  5. Leiningen for Clojure

Gradle is an advanced build system as well as an advanced build toolkit allowing to create custom build logic through plugins!

Advantages:

  • Dsl - Domain specific language, based on groovy
  • DAG - Directed Acyclic Graph
  • Incremental builds
  • Extensible domain model
  • Gradle is always up to date
  • Before a task is being execute, Gradle takes a snapshot of its task’s input and output.
  • In case the snapshot has changed or it doesn’t exists, Gralde will re- execute this task.

Manifest entries

Through the DSL it is possible to configure the following manifest entries:

Build variant

By default, the Android plugin automatically sets up the project to build both a debug and a release version of the application.

Dependencies

  1. Local Dependencies:

If you have binary archives in your local filesystem that a module depends on, such as JAR files, you can declare these dependencies in the build file for that module.

  1. Remote Dependencies:

First the repository must be added to the list, and then the dependency must be declared in a way that Maven or Ivy declare their artifacts.

I refer two tutorial to write the Answer one,two

Gradel is a general purpose, declarative build tool. It is general purpose because it can be used to build pretty much anything you care to implement in the build script. It is declarative since you don't want to see lots of code in the build file, which is not readable and less maintainable. So, while Gradle provides the idea of conventions and a simple and declarative build, it also makes the tool adaptable and developers the ability to extend. It also provides an easy way to customize the default behavior and different hooks to add any third-party features.

Gradle combines the good parts of both tools and provides additional features and uses Groovy as a Domain Specific Language (DSL). It has power and flexibility of Ant tool with Maven features such as build life cycle and ease of use.

Why Gradle? Why Now?

The build tool's response is to add scripting functionality through nonstandard extension mechanisms. You end up mixing scripting code with XML or invoking external scripts from your build logic. It's easy to imagine that you'll need to add more and more custom code over time. As a result, you inevitably introduce accidental complexity, and maintainability goes out the window.

Let's say you want to copy a file to a specific location when you're building the release version of your project. To identify the version, you check a string in the metadata describing your project. If it matches a specific numbering scheme (for example, 1.0-RELEASE), you copy the file from point A to point B. From an outside perspective, this may sound like a trivial task. If you have to rely on XML, the build language of many traditional tools, expressing this simple logic becomes fairly difficult.

Evolution of Java Build Tools

Java build logic has to be described in XML. XML is great for describing hierarchical data but falls short on expressing program flow and conditional logic. As a build script grows in complexity, maintaining the building code becomes a nightmare.

In Ant, you make the JAR target depend on the compile target. Ant doesn't give any guidance on how to structure your project. Though it allows for maximum flexibility, Ant makes each build script unique and hard to understand. External libraries required by your project are usually checked into version control because there is no automated mechanism to pull them from a central location.

Maven 1, released in July 2004, tried to ease that process. It provided a standardized project and directory structure, as well as dependency management. Unfortunately, custom logic is hard to implement

Gradle fits right into that generation of build tools and satisfies many requirements of modern build tools (Figure 1). It provides an expressive DSL, a convention over configuration approach, and powerful dependency management. It makes the right move to abandon XML and introduce the dynamic language Groovy to define your build logic. Sounds compelling, doesn't it?

Gradle combines the best features of other build tools.

enter image description here

Gradle's Compelling Feature Set

enter image description here

Why Build Your Java Projects with Gradle Rather than Ant or Maven?

The default build tool for Android (and the new star of build tools on the JVM) is designed to ease scripting of complex, multi-language builds. Should you change to it, though, if you're using Ant or Maven?

The key to unlocking Gradle's power features within your build script lies in discovering and applying its domain model, as shown in below image. enter image description here

Gradle can't know all the requirements specific to your enterprise build. By exposing hooks into lifecycle phases, Gradle allows for monitoring and configuring the build script's execution behavior.

Gradle establishes a vocabulary for its model by exposing a DSL implemented in Groovy. When dealing with a complex problem domain, in this case, the task of building software, being able to use a common language to express your logic can be a powerful tool.

Another example is the way you can express dependencies to external libraries, a very common problem solved by build tools. Out-of-the-box Gradle provides you with two configuration blocks for your build script that allow you to define the dependencies and repositories that you want to retrieve them from. If the standard DSL elements don't fit your needs, you can even introduce your own vocabulary through Gradle's extension mechanism.

Integration with Other Build Tools

Gradle plays well with its predecessors' Ant, Maven, and Ivy, as shown in the image below.

enter image description here

Automating Your Project from Build to Deployment

enter image description here

In image: Stages of a deployment pipeline.

  • Compiling the code

  • Running unit and integration tests

  • Performing static code analysis and generating test coverage

  • Creating the distribution

  • Provisioning the target environment

  • Deploying the deliverable

  • Performing smoke and automated functional tests

I refer two tutorial to write the Answer one,two

csdnceshi53
Lotus@ In addition to that, You can build your Android apps from the command line on your machine or on machines where Android Studio is not installed (such as continuous integration servers).
2 年多之前 回复
共22条数据 1 3 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐