Groovy basics of Gradle learning

1, What is Groovy

1. Official website:

http://www.groovy-lang.org/

2. Concept:

It is a dynamic language based on JVM virtual machine. It is very similar to Java in syntax and fully compatible with Java. It can be said to be a very flexible dynamic scripting language

3. Application:

Gradle's build script file is a Groovy script file, which is written in Groovy

2, Basic grammar

Since we are learning Gradle, especially Android Gradle, we don't need to master Groovy very deeply at the beginning. We will learn some grammar concepts here in order to understand some codes of relevant script files of Gradle and write simple footnotes. We will understand the advanced features of Groovy after Gradle has learned deeply.

1. How to define the end of a line of code

In Groovy, you don't have to use a semicolon at the end of each line. Just wrap the line. Of course, you can keep the Java habit and write a semicolon

2. Define identifier

def is the keyword Groovy uses to define identifiers

// Define a string variable nicholas whose value is hzf
def nicholas = "hzf";
// Define a method buildString to receive two string parameters and return the results of their splicing
def buildString(String a, String b){
	println "$a-$b"
}

3. Method

A. ) parentheses can be omitted: parentheses can be omitted when calling methods, such as #2

task methodTest {
    buildString("Nicholas","hzf") // #1
    buildString "Nicholas","hzf" // #2
}

def buildString(String a, String b){
	println "$a-$b"
}


B. ) return can not be written: if no return statement is written in the method, the value of the last sentence code in the method execution process is used as the return value

task methodTest {
    def result = chooseSmallNum 1,10
    println result
}

def chooseSmallNum(int a, int b){
	if (a <= b) {
		a
	}else {
		b
	}
}


C. ) code blocks can be passed as parameters
Code block: a code block is a closure. Generally speaking, it is a piece of code wrapped in curly braces
Evolution:
#1 is the initial writing method, which is cumbersome
#2 Groovy stipulates that if the last parameter of a method is a closure, it can be placed after the method
#3 when talking about method calling just now, parentheses can be omitted, so to sum up, the three writing methods are all OK. The most common is #3 this writing method. After understanding the conversion writing method, you will feel intuitive and concise

task methodTest3 {
    doSomething("Nicholas0","hzf",{a,b -> println "$a-$b"}) // #1
    doSomething("Nicholas1","hzf"){ a,b -> // #2
    	println "$a-$b"
    }
    doSomething "Nicholas2","hzf",{ a,b -> // #3
    	println "$a-$b"
    }
}

def doSomething(String a, String b, Closure method){ // Closure defines a closure
	method.call(a,b) // Call call closure
}

4. JavaBean

In Groovy, it is not necessary to define member variables before they can be accessed as properties of a class. As long as getter/setter methods are written, they can also be accessed as properties. If only the getter method is written, the value of this attribute cannot be modified, such as the age attribute in the example; Conversely, if only the setter method is written, this property cannot be accessed

task javaBeanTest {
    Person p = new Person()
    // The properties defined by private in Groovy can be accessed directly!!!
    p.name = "Nicholas"
    println p.info
}

class Person {
	private String name;
	public int getAge(){24}
	public setInfo(String info){}
	public String getInfo(){
		"$name-$age"
	}
}

5. String

Groovy provides three methods to represent strings: single quotation marks ('), double quotation marks ("), or three quotation marks ('). The strings enclosed in single quotation marks have no computing power. As #1 shown, the $strB template is printed as it is; the strings enclosed in three quotation marks can span multiple lines

task stringTest {
	def strA = 'Single quotation mark'
    def strB = "Double quotation mark"
    def strC = '''
---Three quotation marks---
'''
    println strA
    println strB
    println strC
    println 'Single quotation mark==$strB' // #1
    println "Double quotation mark==$strB"
    println '''Three quotation marks==$strC'''
}

6. Assembly

For lists, Groovy provides subscript index access. It is worth noting that in addition to ordinary subscript indexes, there are also negative subscript indexes and range indexes

task listTest{
	def nameList = ["Nicholas","hzf","Groovy","Java","Kotlin","Android","Gradle"]
	println nameList[0] // Positive first
	println nameList[1] // Positive second
	println nameList[-2] // second to last
	println nameList[-1] // Last but one
	println nameList[0..2] // Positive number first to third
	println nameList[-1..1] // Print backwards from the penultimate to the positive second
}

7. Closure

A. ) concept: a piece of code enclosed in braces
Both {println it} and {K, V - > println "$k - $V"} in the following example are closures

task closureTest{
	// The parameter of listEach is closure
	// That is {println it}
	listEach {
		println it // When a closure has only one parameter, it defaults to it
	}
	// The parameters of mapEach are closures
	// That is, {K, V - > println "$k - $V"}
	mapEach { k,v ->
		println "$k-$v" // When the closure has more than one parameter, you need to list the parameters one by one
	}
}
// The listEach method has only one parameter to receive a closure parameter
def listEach(closure){
	def nameList = ["Nicholas","hzf","Groovy","Java","Kotlin","Android","Gradle"]
	nameList.each {
		closure(it)
	}
}
// The method mapEach has only one parameter to receive a closure parameter
def mapEach(closure){
	def map = ["Nicholas":"hzf","Gradle":"Groovy","Android":"Kotlin"]
	map.each {
		closure(it.key,it.value)
	}
}


B. ) closure delegation

  • Three attributes of A closure: thisobject, owner, delegate. When A method is called in A closure, these three parameters determine which object is used to call the called method: for example, if method A() is executed in the closure, is this method A() in the script or method A in A class
  • thisObject constructs the context of the script, which is equal to this in the script
  • By default, delegate is equal to the owner, but delegate can be modified. After modification, it is not equal to the owner
  • Order of processing in closure method: thisobject > owner > delegate
  • In Gradle, delegate is generally specified as the current it
task closureTest2{
	createBook {
		bookName = "Android Gradle Authoritative guide"
		author = "The snow is merciless"
		pageNum = 236
		dumpBook()
	}
}

class Book{
	String bookName
	String author
	int pageNum

	def dumpBook(){
		println "in dumpBook bookName==$bookName, pageNum==$pageNum, author$author"
	}
}

def createBook(Closure<Book> closure){
	Book book = new Book()
	closure.delegate = book
	closure.setResolveStrategy(Closure.DELEGATE_FIRST)
	closure(book)
}

def dumpBook(){
	println "out dumpBook"
}

3, DSL

1. Concept:

Domain Specific Language

2. Features:

  • It lies in the specialty, not in the whole
  • By describing all activities and rules in the field, domain experts formulate a DSL in a specific field, which can facilitate ordinary programmers to realize relevant functions without special understanding of the principles of the field

3. Gradle

Gradle is a DSL based on Groovy, which is specially used to solve the problem of automatic construction. Even if programmers do not understand the principle of automatic construction, they can complete the automatic construction of the project by writing relevant script code according to gradle's syntax

Organize and learn from the Android Gradle authoritative guide and Internet materials of Feixue ruthless boss

Keywords: Java Android Gradle dsl

Added by FatalError on Tue, 04 Jan 2022 16:06:02 +0200