Gradle : New Generation Build Automation tool . How to use Gradle ?
What is Gradle?
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 Maven, Ivy ,
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.
Comments
Post a Comment