The original article was first published in the wechat public number: Jzm-blog, welcome to pay attention to the exchange!

I have been learning Android for some time. I often use Gradle in development, but I don’t know how Gradle builds projects. I plan to spend some time learning Gradle related knowledge. Gradle is an excellent project building tool. Its DSL(Domain-specific Language) is implemented in Groovy. Most of its functionality is implemented through plug-ins. You can also customize Gradle plug-ins.

  • First introduction to Gradle series
  • Gradle: Groovy Basics
  • Gradle series build script basics
  • Gradle series understanding Gradle tasks
  • Gradle plugin for Gradle series
  • Gradle series of Java Gradle plug-ins
  • Gradle series of Android Gradle plugin
  • Gradle series Android Gradle base configuration
  • Android Gradle advanced Configuration
  • The Gradle series builds Maven private server libraries from scratch

Here is the first installment of the Gradle series:

  1. Configure the Gradle environment
  2. Hello World Gradle version
  3. Gradle Wrapper
  4. Gradle log
  5. Gradle command line
  6. conclusion

Configure the Gradle environment

First, ensure that JAVA_HOME is configured in the environment variable. Run the following command to check whether JAVA_HOME is configured:

java -version
Copy the code

The execution logs are as follows:

Prepare a Gradle version, download it and unzip it.

Bin: Gradle batch file docs: documentation init.d: script file for initialization lib: library media: icon resource samples: case SRC: source file geting-started. HTML: Getting started Link LICENSE NOTICECopy the code

Then configure GRADLE_HOME in the environment variable, where Gradle decompresses:

Add GRADLE_HOME\bin to Path as follows:

Run the gradle -v command to check gradle version information. If the gradle version number, Groovy version number, AND JVM information are displayed correctly, the Gradle environment has been configured successfully. Gradle -v run the following command:

PS E:\Gradle\study> gradle -v
------------------------------------------------------------
Gradle 4.1
------------------------------------------------------------

Build time:   2017- 08 -07 14:38:48 UTC
Revision:     941559e020f6c357ebb08d5c67acdb858a3defc2

Groovy:       2.411.
Ant:          Apache Ant(TM)Version 1.9.6 Compiled on June 29 2015 JVM: 1.8.0_91 (Oracle Corporation 25.91-b14)
OS:           Windows 10 10.0 amd64
Copy the code

At this point, the Gradle build environment on your Window is set up.

Hello World Gradle version

Gradle: Hello World Gradle: build. Gradle: Hello World Gradle: build. Gradle

task hello{
	doLast{
		println 'Hello world'}}Copy the code

Run the gradle -q hello command and the result is as follows:

PS E:\Gradle\study> gradle -q hello
Hello world
Copy the code

Gradle is gradle’s default build script. When running commands, the build.gradle script in the current directory is loaded by default. This build script defines a Task named Hello. DoLast is an Action that calls back to doLast when the Task is finished. Gradle -q specifies the level of gradle log output.

Gradle Wrapper

Gradle has a Wrapper around Gradle that allows you to manage Gradle versions in a unified way. The Wrapper is usually used in project development, so you don’t need to configure Gradle build environment with the Wrapper. When Gradle is enabled with Wrapper, the Wrapper will check if Gradle is associated with a download, and if not, it will be downloaded from the configured address and built, which makes it somewhat easier for developers to build projects.

To generate the Wrapper

Gradle provides a built-in Wrapper Task to generate a directory file for wrappers. Run the Gradle Wrapper command in the corresponding directory to generate the Wrapper file:

PS E:\Gradle\study> gradle wrapper

BUILD SUCCESSFUL in 3s
1 actionable task: 1 executed
PS E:\Gradle\study> cd wrapper
Copy the code

The following directories are generated using gradle Wrapper:

│─gradlew │ └─ gradlew. Bat ├ ─gradle ├ ─wrapper gradle-wrapperCopy the code

Gradlew and gradlew. Bat are executable scripts for Linux and Windows respectively. Gradle-wrapper. jar is a jar package implemented according to the specific business. Gradlew ultimately uses this jar package to perform gradle-related operations. Gradle-wrapper. properties is used to configure which gradle version to use for the build operations.

Wrapper configuration

When generating files using gradle Wrapper, you can specify the version to be used for the wrapper and the gradle download address as follows:

// Specify the Gradle version to use
gradle wrapper --gradle-version 3.3
// Specify where to download Gradle
gradle wrapper --gradle-distribution-url ...
Copy the code

Gradle default Gradle version is the current Gradle version.

https\:/ / services.gradle.org/distributions/gradle-4.1-all.zip
Copy the code

Gradle configuration file gradle-wrapper.properties

distributionBase // The home directory where the downloaded Gradle package is stored after decompression
distributionPath // Relative to the path of the decompressed distributionBase package
zipStoreBase // Place the Gradle archive relative to distributionBase
zipStorePath // Stores the Gradle archive relative to distributionPath
distributionUrl //Gradle download address, usually official website address
Copy the code

Gradle configuration file for an Android project:

#Mon Dec 28 10:00:20 PST 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\:/ / services.gradle.org/distributions/gradle-4.1-all.zip

Copy the code

By the way, a few property values:

// indicates the user directory, under the user directory. Gradle directory
GRADLE_USER_HOME
// Represents the project directory where gradlew is located
PROJECT
Copy the code

These two values can be used to set the values of distributionBase and zipStoreBase

Customize the Wrapper Task

Gradle-wrapper. properties is a gradle-wrapper.properties file generated by a Wrapper Task, which can be used to configure gradle-wrapper.properties. Here is an example of a custom Wrapper Task,

task wrapper(type: Wrapper){
	gradleVersion = '3.3'
	distributionBase='GRADLE_USER_HOME'
	distributionPath='wrapper/dists'
	// do not write: HTTPS \://services...
	distributionUrl="https://services.gradle.org/distributions/gradle-3.3-all.zip"
}
Copy the code

This allows you to define the Gradle version that generates the Wrapper and the associated storage directory.

Gradle log

When building a project using Gradle, you can specify a log level to display the log information. Gradle has six log levels.

ERROR // Error message
QUIET // Important news
WARNING // Warning message
LIFECYCLE // Progress message
INFO // Information messages
DEBUG // Debug information
Copy the code

You can control the log display level by using the command line interface. The following are the log options that can be controlled by using the command:

- q or - quiet// QUIET and higher-i or - info// Indicates INFO and higher levelsD or -- the debug//DEBUG and higher (output all logs)
Copy the code

If not specified, LIFECYCLE and higher-level logs will be output by default.

Logs track the build process and debug errors. Here is how to output stack information during a project build and how to use log information for debugging.

Output stack information

By default, the output of stack information is turned off. You can turn it on by using the command line stack information switch. When a build fails, Gradle will output the wrong stack information to help locate and analyze the problem.

- or - s stacktrace// Outputs critical stack informationThe -s or - full - stacktrace// Displays all stack information
Copy the code

Generally, -s will do.

Debugging Log Information

The simplest way to log is to print the variable you want to see in the right place. You can use the print series of methods to print the log to the console, which is QUIET level log. You can also use the built-in Logger to control the display of different levels of log. And DEBUG generates the most complete logs, while ERROR generates the least. The usage methods are as follows:

// Log tests
task hello{
	doLast{
		println 'Hello world'
		print 'Hi'
		logger.quiet('the -quiet log')
		logger.lifecycle('lifecycle log')
		logger.error('the error log')
		logger.info('the info log')
		logger.warn('warn log')
		logger.debug('the debug log')}}Copy the code

The basic Gradle log content is described above, but it is important to practice in real projects.

Gradle command line

Use the command line to understand the build process to a certain extent. Compared to the convenience of using the command line on the IDE, you can use the command line to view the executable commands through the help command, as shown below:

gradle -h
gradle -?
gradle -help
Copy the code

You can run the preceding commands to view the commands that can be executed.

View executable Tasks

You can use./gradlew tasks to view executable tasks. The execution results are displayed in groups, one for Build Setup tasks and one for Help tasks.

PS E:\Gradle\study\wrapper> ./gradlew tasks
:tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]

Help tasks
----------
components - Displays the components produced by root project 'wrapper'. [incubating]
dependencies - Displays all dependencies declared in root project 'wrapper'.
dependencyInsight - Displays the insight into a specific dependency in root project 'wrapper'.
help - Displays a help message.
model - Displays the configuration model of root project 'wrapper'. [incubating]
projects - Displays the sub-projects of root project 'wrapper'.
properties - Displays the properties of root project 'wrapper'.
tasks - Displays the tasks runnable from root project 'wrapper'.

To see all tasks and more detail, run gradlew tasks --all

To see more detail about a task, run gradlew help --task <task>

BUILD SUCCESSFUL

Total time: 8.4 secs
Copy the code
View help for a Task

Gradle has a built-in help task that can be used to help a task.

// Command formatGradle help -- Task Task name/ / for
gradle help --task projects
Copy the code

The execution result is as follows:

PS E:\Gradle\study\wrapper> gradle help --task projects

> Task :help
Detailed task information for projects

Path
     :projects

Type
     ProjectReportTask (org.gradle.api.tasks.diagnostics.ProjectReportTask)

Description
     Displays the sub-projects of root project 'wrapper'.

Group
     help


BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
Copy the code

You can view the Task group, type, and additional parameters in the help information.

Add refresh- refresh to refresh your dependencies when you build your project. Add refresh- refresh to your dependencies when you run multiple tasks at the same time. Gradle provides abbreviated calls based on camel’s name, which can be abbreviated as follows:

//Task
newTask
/ / command
./gradlew nt
Copy the code

conclusion

This is the first article to know Gradle and related commands, which is also the foundation for learning Gradle building projects in the future. In the following articles, we can continue Gradle series, please follow the public account: jzm-blog, and communicate with you.