[Gradle-1] Getting started with Gradle, a must read beforehand

1. Why learn Gradle

  1. Gradle is the default build tool for Android development, and you will use it every time you compile.
  2. The recruitment requirements have changed from the previous familiarity points to the current necessary skills, which shows the importance of Gradle.

After doing development for so long, do you love and hate Gradle? Do you know little about Gradle configuration? Is the result of each compilation like opening a blind box? Want to know how Gradle works behind the scenes? Do you also want to write Plugin by hand? Wait, wait, if you want, then this column can give you the answer, thoroughly understand Gradle, help you improve efficiency, free your hands, and harvest Offers.

What you can gain from this column:

  1. Familiar with the principle and related configuration of Gradle.
  2. Improve the ability to locate and resolve compilation failures.
  3. Understand and be able to write Gradle plugins by hand.
  4. Improve compilation speed with Gradle.
  5. Facilitate interviews and harvest Offers.

2. What is Gradle

Gradleis an open source automated build tool focused on flexibility and performance.

This sentence, when most people read it, they are swept away, in fact, I am too, but if someone asks me, why use Gradle? What problem does Gradle solve? I don't seem to be able to give a satisfactory answer, because I haven't thought about it deeply, and I may answer this subconsciously, because I use Android Studio to develop Android, and then there are these gradle configurations by default when creating a project. In other words, gradle is the default build tool of Android Studio, and runit can be compiled and run to the mobile phone with one click.

Is there any question in my answer? It seems that there is no problem. But when I think about it, I seem to be just reporting something that everyone already knows. If it is an interview, I will pass at most, and I will be put in the spare tire because there is no bright spot In the pool, I missed the offer in the end.

So now, let's try to break this sentence down:

  • Open source automated build tools
  • Focus on flexibility and performance

2.1, what is a build tool

What is a build tool? A build tool is 自动化to help us complete a series of compilation and packaging processes.

If there is no build tool, we need to execute commands over and over again to package. For example, to package APK, we need to use javac to compile code, then use AAPT to compile resource files, and then compile DEX to combine APK and finally sign. If you do this, it must be quite laborious, so automated build tools were born. At the same time, construction tools also help us 依赖管理. For example, before Android Studio, we used Eclipse to develop. Without construction tools, we had to rely on a third-party library. We needed to download the jar package and then put it into the project. This process It is cumbersome, and you have to repeat the operation when upgrading the jar version. If you want to use it in other projects, you have to manually copy the jar package again, but Gradle supports dependency transfer, and you can use different dependency methods. Dependency scope can be changed.

2.2. Brief history of construction

Before Gradle, the most classic one Antis the DSL used by Ant. xmlThe evolution of xml comes from the cumbersome construction of MakeFile, and the feature of xml is structured and easy to understand. This is much simpler than writing script plug-ins, so it quickly became popular .

With the rapid development of the software industry, our products have more and more functions, the business has become more and more complex, and the development team has become larger and larger. At this time, the problems of engineering management and engineering standardization have become increasingly prominent, so it was born Maven. Maven solves the dependency problem well, introduces a standard dependency library to manage the version, and makes a standardized definition of the project directory structure and construction life cycle, which greatly facilitates project management and development.

But when Maven became popular for a period of time, everyone discovered the problem again. The simple logic of xml is good, but it is too long-winded to write, and the scalability is not enough. At this time, it appeared Gradle.
Based on Maven, Gradle mainly solves two problems:

  1. Use a new DSL to make the syntax more concise and support extensions;
  2. Defines a construction life cycle that is easy to expand and does not lose standards;

In fact, since the development of Gradle, it has already surpassed the above two points, and it is still evolving, such as the birth of buildSrc, the support of kts, the evolution of KSP, and so on.

3. Features of Gradle

  1. Gradle is a build tool based on JVM, written in java;
  2. The scripting language (DSL) is written in Groovy (.gradle) and Kotlin (.gradle.kts), both of which are high-level languages ​​and object-oriented programming;
  3. The core object in Gradle is Taskthat Task is the smallest construction unit in Gradle, and Action is the smallest execution unit;
  4. The corresponding project in Gradle Projectis a tree structure, which can be traversed downward or upward, and is also used to associate Task;
  5. Gradle provides a good extension capability, and you can customize plug-ins and configurations according to your needs;
  6. 生命周期Gradle provides rich callbacks at each stage, which is very helpful for the extension of aspect processing;
  • Highly Customizable - Gradle is modeled in a way that is customizable and extensible in the most basic way.
  • Fast - Gradle completes tasks quickly by reusing outputs from previous executions, processing only changed inputs, and executing tasks in parallel.
  • Powerful — Gradle is the official build tool for Android and supports many popular languages ​​and technologies.

4. How to learn Gradle

In fact, learning Gradle is not an easy task. From the principle of Gradle, development configuration, to compilation and packaging, each point requires in-depth study and understanding. Around this main line, I also briefly listed the learning route. It will also continue to revolve around the following learning route.
insert image description here

Of course, this learning route is currently the first version, because Gradle still involves a lot of things, and the update is relatively fast, and it will continue to be iteratively supplemented later.

5. Summary

This article mainly introduces the importance of mastering Gradle, what is a construction tool and a brief history of construction, the characteristics of Gradle and the learning route of Gradle.

However, it is difficult to fully explain Gradle in just one or two articles. I hope that through a column, I can explain the difficult knowledge points of Gradle in a simple way, so I will continue to introduce Task and life in Gradle Cycle, dependency management, plug-in development, compilation speed-up and other related knowledge, if you happen to want to know more about Gradle, welcome to pay attention and keep updating!

In addition, some sample source codes about Gradle will be expanded in this Github repository: https://github.com/yechaoa/GradleX

6. Reference documents

Guess you like

Origin blog.csdn.net/yechaoa/article/details/130174456