Gradle Tutorial: The first section, installing "translation"

Source: Internet
Author: User
Tags list of attributes

Original address: http://rominirani.com/2014/07/28/gradle-tutorial-part-1-installation-setup/

In this tutorial, we will focus on how to install, configure, the original version of Gradle on our own system, and learn some basic Gradle command line! Android Studio itself integrates a built-in version of Gradle, which is automatically installed when installing Android Studio, which is not helpful for our small white understanding of gradle. So we're going to install Gradle original version separately and configure it independently so that we understand the Gradle inside Android studio.

Before you start, if you want to know why Gradle is a good tool for compiling, I suggest you read this article, although you can read the following without reading it, but reading it may help to open up your mind and understand why many companies and developers are researching and applying gradle!

The following series of steps will teach you how to configure gradle! on your own computer Gradle needs the JDK, so I assume you're a Java developer and know how to install Java jdk! on your PC.

Download Gradle

At the time of writing, Gradle has been released to 2.0 and can be downloaded here.

First download the Gradle-2.0-all.zip file to your computer, and unzip it, I unzip it to my computer's D:\Program files\gradle-2.0-all. These files are available after decompression

You might ask: Why install an original version of Gradle? Android Studio itself is simply an integrated gradle, and when you use Android Studio to create apps, Android Studio calls Gradle commands to do something while Android studio How are you doing these things? If you install this standalone version of Gradle and use its command line to do it, it's easy to know how Android Studio is doing it, otherwise you'll mistakenly assume that Android studio is playing magic with Gradle.

Setting environment variables

The \ Bin folder contains the Gradle script file (both UNIX and Windows have), and if you want to use this script, you need the following steps:

1. Create an environment variable gradle_home, and point to the installation path of your grdle on my computer, so add:

2. Add%gradle_home%\bin to your PATH environment variable so you can use the GRADLE command line in CMD!

Check Configuration

To check if the variable you just set is successful, you open cmd and execute the following command:

Gradle–v

If the configuration succeeds, the following results will be available:

If you don't see anything like this, check that the path is correct.

About Groovy

As long as there is gradle there should be groovy, leaving groovy to discuss Gradle is not complete! Groovy is a very popular and powerful language on the JVM.

The most popular features of Gradle use are:

Simple

Flexible

Can be freely configured in the way you think.

Groovy plays a very important role in implementing these features, essentially groovy providing the implementation of these features. Ant and Maven use XML files at compile time, while the groovy language is much more flexible than an XML file, because it not only makes the file more concise in content, it provides a powerful programmability to modify or label, but also supports the advanced features of the latest programming languages such as closures. And can be compiled in real time!

So here's the question, and you might ask, "Do I need to learn groovy?"

I try to answer you, no need! Unless you really need to do what you need in order to do what you want, you may need to learn groovy, because the elasticity and flexibility of gradle is here, and you can do everything the way you want. But for the most part, you just need to use a variety of build.gradle templates (including Groovy DSL), and you don't need to learn groovy!

So, to understand that Gradle doesn't need you to learn groovy well, of course, if you want to understand all of groovy's powerful features, you can learn it yourself. It's definitely going to be good!

Groovy has installed a built-in version when Gradle is installed by default, so you don't need to go to the next standalone version of groovy!

Basic Gradle command

We haven't written any source code or compiled files (Gradle), so we'll write about them in a minute. Now, let's try the following command line:

Gradle-q Help

This command is used to display Gradle's help information.

-Q: Used to represent the exit information in a DOS window, this command is useful when you only need to see some output

Gradle–q Tesks

This shows a list of tasks you can use today

Gradle Properties

This will show a list of attributes, which are gradle already scheduled, and you can modify most of the implementation in your own compilation file. These attributes give you a hint: Gradle will do a lot of heavy work to configure your project before performing your task!

We haven't compiled our own Java project yet, and this will be done in a later series, and we'll first understand how groovy gives Gradle a whole set of powerful programming capabilities!

Let's discuss the Build.gradle file first. The file name of this standard is our own compiled files. This file contains all the commands you need to Gradle to execute. After completing the entire tutorial series, we will be able to use this file very effectively to create or use a variety of sequential plugins, tasks to complete our own compilation, testing, running Java program work!

Now, you come and follow me:

Select a folder to create a file inside the build.gradle

Using Notepad or another editor, add the following content to the Build.gradle file

Task Compiletask << {

System.out.println "Compiling ..."

}

Now open cmd and navigate to the folder you created (using the CD command, you should), execute the following code

Gradle–q Tasks

This will output a list of the various tasks available, and you will notice that in the output section, separated from the standard tasks, there will be one of our own created task Compiletask:

Our compilation file (Build.gradle) is a combination of some column tasks (Task), which brings out the first conceptual Task (Task), which specifies the code that the Gradle compiles the system to execute for us.

The project we are currently specifying is called Compiletask, as you can see, we use groovy code to define this task, and the task is simply to execute a System.out.println. How can you understand how groovy is so powerful now?

Now, how do we use the command to execute this compiletask ?

Before we do this, we still have a knowledge point to understand. When we execute a gradle command, the Gradle compilation system looks for a file named Build.gradle in the current directory. If you find it, use this file. In our example above, Gradle compiled the system to find this file, so the Gradle compiler will determine what kind of task is in this file and add its task to the list of executable tasks in the Gradle compilation system.

So, what happens when we execute a gradle command with no arguments, and do not specify any tasks to be executed? Now try it, execute it:

Gradle

You should see the following output:

This output obviously tells you that when you execute gradle, you need to specify a task name. For example: Gradle <task>. So let's just try to use our own task to what effect it will be:

Gradle–q Compiletask

There is an output message

Now let's add some other tasks to the Build.gradle file, such as:

Task Compiletask << {

System.out.println "Compiling ..."

}

Task Buildtask <<{

System.out.println "Building ..."

}

Now execute the following command:

Gradle–q Tasks

You should see two listings in Othertasks:

Now you can do it.

Gradle Compiletask

Or

Gradle Buildtask

So what if we want to create a task that is executed by default, so that I don't need to make a task name?

To do the above requirements, modify the Build.gradle file

Defaulttasks ' Buildtask '

Task Complietask << {
System.out.println "Compling ..."
}
Task Buildtask <<{
System.out.println "Building ..."
}

Now, if we simply execute the GRADLE-Q, we will have the following output:

The last part to be discussed is the interdependence between tasks. The problem is this: if Buildtask executes, then Compiletask must be executed before buildtask, which means Buildtask relies on compiletask. To do so, we need to modify the Build.gradle file as follows:

Defaulttasks ' Buildtask '

Task Compiletask << {
System.out.println "Compling ..."
}
Task Buildtask (Dependson:compiletask) <<{
System.out.println "Building ..."
}

Once you execute gradle-q again, you should have the following output:

The gradle we discussed in this article has only some very basic functions. Think of your compiled file as a list of tasks that need to be performed. If you are a Java program ape, and have some experience in compiling processing (ant,maven), then you should know that to compile tasks, we have to compile, build, run test cases, packaging and so on, but in Gradle there is no such, only a variety of tasks.

We do not need to write any tasks manually by using the Gradle Conventions and plugin programs. You can define some of the steps of a column to build a project between civilizations!

There are some plugins that have Java plugin, war Plugin,appengine plugin, Android plugin and so on, those that have defined some of the tasks you can use, and we'll see more in the next lesson! Keep improving ...

Gradle Tutorial: The first section, installing "translation"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.