Skip to main content

Gradle : New Generation Build Automation tool . How to use Gradle ?

 

Gradle : New Generation Build Automation tool . How to use Gradle ?

 

What is Gradle?

 Gradle is a next-generation JVM-based build tool that streamlines Gradle's knowledge for Android development. Gradle is currently used in several Android-developed technology systems, such as building systems, plug-ins, hot fixes, componentization, and more.

Gradle is a build tool for project automation, a programmable tool that lets you control the build process and ultimately generate deliverable software. Build tools can help you create a repetitive, reliable, manual, build that doesn't depend on a specific operating system or IDE. 

An open source project automated build tool, built on the basis of apache ant and maven, and introduces a Groovy-based domain-specific language (DSL), and no longer uses xml to manage build scripts.

 

The development stage of the build tool: Ant --> Maven --> Gradle

 

Ready to work

 

① Install gradle

 

② Configure environment variables, create new key as GRADLE_HOME, value as gradle path, and then configure the bin directory under gradle to path path

 

 

What is Groovy?

 

    Groovy is an agile dynamic language for the Java virtual machine. It is a mature object-oriented programming language. It can be used for object-oriented programming or as a pure scripting language. It is not necessary to write in this language. Too much code, but also has closures and other features in dynamic languages.

 

Comparison with Java

 

    ● Groovy is fully compatible with Java syntax

 

    ● The semicolon is optional

 

    ● The class/method is public by default

 

    ● The compiler automatically adds getter/setter methods to attributes

 

    ● Attributes can be obtained directly with dots

 

    ● The value of the last expression will be used as the return value (return may not be written)

 

    ● == is equivalent to equal(), no null pointer exception will be reported

 

Efficient Groovy features

 

    ● assert statement

 

    ● Optional type definition

 

    ● Optional brackets

 

    ● String

 

    ● Collection API

 

    ● Closure

1. What is a build tool

Everyone knows that Eclipse is an IDE (Integrated Development Environment), originally used for Java development, and Android is based on the Java language, so initially Google still hopes that Android can be developed on Eclipse . In order to meet this demand, Google developed a called ADT ( AndroidDeveloper Tools things), and it is because of ADT , we only need to code is good code, and then directly in the Eclipse carried on to compile, run, signature, packaging and a series of processes. In a sense, ADT is our build tool.   

Since Google launched Android Studio , it has announced that it will use Gradle as a build tool by default , and then abandon updating ADT .   

Generally speaking, in addition to the above mentioned compilation, running, signing, packaging, etc., the build tool also has the function of dependency management . If you need to use a third-party library, you usually add the jar file to the libs directory. But assuming that the third-party library is updated, you need to download the latest Jar file. If there are many third-party libraries referenced, there is no management at all.   

Now Gradle refers to third-party libraries like this:     

compile 'com.android.support:support-v4:24.0.1'



You can see the source address directly. If you upgrade, you can just change the version number . This is called dependency management . Therefore, the build tool is a collection of a series of functions such as compiling, running, signing, packaging, and dependency management of the project . Traditional build tools include Make , Ant , Maven , Ivy, etc., and Gradle is a new generation of automated build tools. And it is an independent project, has nothing to do with AS , Android , official website: https://gradle.org/ , similar to Ant , Maven and other build tools are described based on xml , very bloated, and Gradle uses It is a language called Groovy . The syntax is very similar to Java syntax, but it is a dynamic language, and many improvements have been made on the basis of Java . It is more concise and flexible to use, and Gradle is fully compatible with MavenIvy , this basically announced that Maven and Ivy can be abandoned. Gradle 's launch is mainly based on Java applications. Of course, it currently supports Android , C , and C++ . Google chose Gradle as the build tool when it launched AS , and made an AS plugin called Android Gradle Plugin , so we can use Gradle on AS completely because of this plugin. There is a build.gradle file in the root directory of the project with such a code:

classpath 'com.android.tools.build:gradle:2.1.2'  

This is the code that relies on the Gradle plugin. The version number behind represents the version of the android gradle plugin , not the version of Gradle . This is set by Google and has nothing to do with Gradle .



2.Gradle Wrapper          

Now create a new project by default, and then click Run, Gradle Wrapper will be installed by default .

Suppose we have multiple projects locally, one is an older project, the version of Gradle 1.0 is also used , and the other is a version of Gradle 2.0 , but the two projects want to run at the same time, and only Gradle 1.0 is definitely not enough, so In order to solve this problem, Google introduced the concept of Gradle Wrapper , that is, it configures a specific version of Gradle in each of your projects . It can be understood that each Android project has a small Gradle locally . Through this each project you Can support different Gradle versions to build projects. 

The build process for Apk


The process of building this APK is divided into the following steps:



  1. Package res resource files, such as Android Manifest.xml, xml layout files, and compile them into binary through AAPT (Android Asset Packaging Tool), where the files in the assets and raw folders are not compiled into binary and eventually produce R.java and resources.arsc files.

  2. The AIDL tool converts all aidl interfaces into corresponding Java interfaces.

  3. All Java code, including R.java and Java interfaces, is compiled by the Java compiler into .class files.

  4. The Dex tool compiles the next .class files, third libraries, and .class files into .dex files.

  5. The next compilation of the generated .dex file, compiled resources, resources that do not need to be compiled (such as pictures, etc.) is packaged into an APK file by the ApkBuilder tool.

  6. Use Debug Keystore or Release Keystore to sign the APK file generated in the next step.

  7. If you are officially signing an APK, you also need to use the zipalign tool to align the APK so that the application runs with less memory overhead.

As can be seen from the above steps, the APK construction process is more cumbersome, and this build process is often repeated, if there is no build tools, manually to complete the construction work, no doubt for developers is a torture, but also will produce a lot of problems, resulting in a longer project development cycle.

(1) Gradle is an automated build tool

Gradle is built automaticly by organizing a series of tasks, so tasks are the most important concept in Gradle.

(2) The Gradle script uses Groovy or Kotlin's DSL

Gradle is written with Groovy or Kotlin, but is still Groovy's most.

So what is a DSL? DSL, short for Domain Specific Language, is a computer language specifically designed to solve a certain type of task.

DSLs correspond to GPL (General-Purpose Language), such as java.

Compared to GPL, DSL is simple to use, simple to define, and implements language logic compared to configuration files.

For the Gradle script, he implements a concise definition with sufficient language logic, in the case of android , which is itself a function call and the argument is a closure, but this definition is obviously much simpler.

(3) Gradle is written based on Groovy, which is based on the Jvm language

Gradle is written with Groovy, which is based on the Jvm language, so it's essentially an object-oriented language, and object-oriented languages are characterized by everything.

So, in Gradle, the essence of the .gradle script is the definition of the class, the essence of some configuration items is method calls, and the parameters are followed by the closure.


 

Comments

Popular posts from this blog

Defination of the essential properties of operating systems

Define the essential properties of the following types of operating sys-tems:  Batch  Interactive  Time sharing  Real time  Network  Parallel  Distributed  Clustered  Handheld ANSWERS: a. Batch processing:-   Jobs with similar needs are batched together and run through the computer as a group by an operator or automatic job sequencer. Performance is increased by attempting to keep CPU and I/O devices busy at all times through buffering, off-line operation, spooling, and multi-programming. Batch is good for executing large jobs that need little interaction; it can be submitted and picked up later. b. Interactive System:-   This system is composed of many short transactions where the results of the next transaction may be unpredictable. Response time needs to be short (seconds) since the user submits and waits for the result. c. Time sharing:-   This systems uses CPU scheduling and multipro-gramming to provide economical interactive use of a system. The CPU switches rapidl

What is a Fair lock in multithreading?

  Photo by  João Jesus  from  Pexels In Java, there is a class ReentrantLock that is used for implementing Fair lock. This class accepts optional parameter fairness.  When fairness is set to true, the RenentrantLock will give access to the longest waiting thread.  The most popular use of Fair lock is in avoiding thread starvation.  Since longest waiting threads are always given priority in case of contention, no thread can starve.  The downside of Fair lock is the low throughput of the program.  Since low priority or slow threads are getting locks multiple times, it leads to slower execution of a program. The only exception to a Fair lock is tryLock() method of ReentrantLock.  This method does not honor the value of the fairness parameter.