preface
As an Android student, I think I am familiar with “Gradle”, but I am not familiar with Gradle. A few months ago, I just stayed in such simple use.
The compile "com. Strange. Unfamiliar: 1.0"Copy the code
What made me realize its importance? Should be in the fall after the recruit, brush pull hook, see a hangzhou startup for new issues the temptation of 40 k, hurriedly threw a wave, throw shots, and then received the interview invitation, at first, feel it’s not in the CARDS, is not a partial cs, but in engineering practice, the problem, ask to gradle during the period of the OS
After making do with a few words, he continued to ask, and then the whole
After the face to the contact information of the interviewer, some communication, realized the importance of it, and then think through the project driven to learn a wave of in-depth, decided to masturbate QQ space hot repair way in the Gradle plug-in patch package, yes, is to copy nvWA.
So, prepare a series of articles to share the whole learning process. In fact, I started to write two articles last year, and then continued to delay, so I picked them up again. In this article, we will take a look at some of the features of Gradle, its importance, and some of its basics. Next, we will take a look at Gradle in Android projects, and how to use Gradle to do some custom builds in AndroidStudio. It is divided into four parts. I hope to make a summary of my own learning at the same time, I can make zero basic Gradle to follow the blog to have an understanding of Gradle, and at the same time, I can realize a simple Gradle plug-in.
What is Gradle?
Gradle is an automated project builder based on Apache Ant and Apache Maven concepts. Instead of traditional XML, it uses a Groovy-based domain-specific language to declare project Settings.
What are the advantages of Gradle over Ant and Maven?
- Automatic handling of package dependencies – a concept taken from Maven Repos
- Automatically handles deployment issues – a concept taken from Ant
- Conditional judgment writing intuition – Using the Groovy language
In the past, Java developers have used tools such as Maven and Ant to automate packaging and deployment, or both. However, the two packages have their pros and cons. Ant can be cumbersome if you change dependent package versions frequently. Gradle solves the problems that Maven and Ant have brought to developers in the past, and is now a built-in package deployment tool for Android Studio.
What can Gradle do with Android?
The Android build system compiles application resources and source code, and then packages them into an APK that you can test, deploy, sign, and distribute. Android Studio uses Gradle, an advanced build toolkit, to automate the execution and management of the build process, while also allowing you to define flexible custom build configurations. Each build configuration defines its own set of code and resources, while reusing those common to all versions of the application. Android Plugin for Gradle works with this build kit to provide processes and configurable Settings dedicated to building and testing Android applications.
Gradle and Android plug-ins run independently of Android Studio. This means that Android applications can be built within Android Studio, using a command line tool on a computer, or on a computer that does not have Android Studio installed (such as a continuous integration server). If you’re not using Android Studio, you can learn how to build and run your application from the command line. Whether you build your project from the command line, on a remote computer, or using Android Studio, the output from the build is the same.
As shown in the figure above, in a Project, in addition to our own code and resources, there will be multiple. Gradle files related to the Project construction, such as. Gradle files are used throughout the process of building projects using Gradle.
In Gradle, each Project to be compiled is called a Project. Each Project is built with a set of tasks. For example, an Android APK compilation might include: Java source compiled tasks, resource compiled tasks, JNI compiled tasks, Lint checked tasks, packaged apK-generated tasks, signed tasks, etc.
####Gradle workflow
The Gradle workflow is shown below. Before and after each workflow, we can perform some hook operations to meet our needs.
Gradle work consists of three phases:
- The first is the initialization phase. For our previous multi-project build, this was settings.gradle
- The next phase of the Initiliazation phase is the Configration phase.
- The goal of the Configration phase is to parse build.gradle in each project. For example, in the multi-Project build example, build. Gradle is parsed in each subdirectory. In between, we can add some custom hooks. This is, of course, added through the API.
- After the Configuration phase, the entire build project and internal Task relationships are determined. A Project contains many tasks, each of which has dependencies. Configuration creates a directed graph to describe the dependencies between tasks. So, we can add a HOOK that performs some operations when the Task diagram is established.
- The final stage is the execution of the mission. Of course, after the task is executed, we can also add Hook.
In short, Gradle has an initialization process, which settings. Gradle executes. During the configuration phase, each Project is parsed and its internal tasks are added to a directed graph to resolve dependencies during execution. Then there’s the execution phase. When you specify a task in Gradle XXX, Gradle will execute all the tasks in the XXX task chain in dependent order.
Gradle has three main types of objects
These three objects correspond to three different script files, which are converted into objects when gradle executes:
- Gradle object: When we execute Gradle XXX or something, Gradle constructs a Gradle object from the default configuration script. There is only one object in the entire execution. The data type of a Gradle object is Gradle. We rarely customize this default configuration script.
- Project object: Each build.gradle is converted to a Project object.
- Settings object: Obviously, every settings.gradle is converted to a Settings object.
Gradle: a “Project” is a etings. Gradle: a “build.gradle” is a etings. Configure the Project object based on the file contents.
The number of tasks in a project, depending on the number of jacks applied to it, passes
apply plugin: 'com.android.library'
Copy the code
A Task contains several actions. Therefore, Task has two functions, doFirst and doLast, to add actions that need to be executed first and actions that need to be executed last. Action is a closure. For an existing Task, we can perform a series of Hook operations before or after execution, adding operations before and after execution.
tasks.getByName("task"){
it.doLast{
println "do the task"}}Copy the code
Groovy overview
Here’s a brief introduction to the Groovy language, and with a brief introduction to the syntax, it’s a good idea to see some simple Groovy syntax for the build process that follows. A brief introduction to Gradle can help us understand some of the configuration information.
Groovy is a dynamic language that is based on and extends Java. Java programmers can seamlessly switch to developing programs using Groovy. Groovy makes writing Java programs as easy as writing scripts. Once written, it can be executed, and Groovy internally compiles it into a Java class and starts the virtual machine to execute it. The following diagram shows the relationship between Groovy and Java code and JVM.
Language overview
- Groovy supports dynamic typing, which means you can define variables without specifying their types. (Def is not required, but the def keyword is recommended for code clarity.)
def a = 5;
def b = "groovy"
Copy the code
- Function definition, we also do not need to declare the parameter type, at the same time can not declare the return value type, but need to define through the DEF field, the last line of the function as the return value.
def function1(arg1, arg2) {
arg1 + arg2
}
String function2(str1, str2) {
return str1 + str2
}
Copy the code
- A function callsupport
Parameter Name: Parameter value
Way to call
apply plugin: 'com.android.library'
Copy the code
Plugin: parameter name, ‘com.android.library’ : parameter value
- Powerful string support
// Single quotes correspond to the string str1 = in Java'this string'// double quotes, which can be translated by $x = 1 str2 ="This is $x"// The spacing of each line is str3 =' ''begin line1 line2 end'' '
Copy the code
- closure
Lexical Closure, also called Lexical Closure or function closures, are functions that refer to free variables. The referenced free variable will exist with the function, even if it has left the environment in which it was created. Therefore, there is another way to say that closures are entities composed of functions and their associated reference environments. The implementation of closures, from a functional programming point of view, is to solve the problem of one input to one output. For example, when we want to implement an addition, we have to pass two arguments to do so, but with functional programming, we can pass only one argument.
function plusAny(first) {
return function(second) {
return first + second;
}
}
var longLiveSeniorFunc = plusAny(1);
longLiveSeniorFunc(1);
Copy the code
- Definition of Closure structure
Def XXX = {paramters -> code} def XXX = {paramters -> code}Copy the code
According to the above two structures, the following are examples
def closure = {
String param ->
println "This is $param"
}
Copy the code
def closure = {
println 'This is closure'
}
Copy the code
- How do I call a closure
closure.call('Hello')
closure('Hello')
Copy the code
The closure implies a parameter of its own, IT
def closure = {
println "This is $it"
}
Copy the code
When a closure is an argument to a function
def testClosure(Closure closure) {
closure()
}
testClosure(
println 'Test'
)
Copy the code
conclusion
In this article, we will give you an overview of what Gradle is, what it does, and what it does in Android. We will give you an overview of Gradle workflow and how to use Gradle in a better way.
Refer to the article
Understanding Android (1) : Gradle details