Current location - Quotes Website - Personality signature - How to understand Grader in a popular way?
How to understand Grader in a popular way?
From Zhihu:

How to understand Gradle in a popular way?

I used to develop Android projects with Eclipse and ADT, and I didn't have a deep grasp of its basic principles, so I started developing directly without thinking too much. Now I have a new project to use Android Studio. I have heard of AS for a long time, but because I am a little ignorant, I can't understand Gradle in the process of learning AS these days. I found a lot of information, but all of them are professional descriptions, so I want to ask you if there is a more vivid and easy-to-understand explanation to describe Gradle, what it is and what it is used for. Compared with the mechanism of compiling Android programs with ADT in Eclipse, he has.

ghui?

Thank you for your invitation.

Gradle is an automatic project building tool based on the concepts of Apache Ant and Apache Maven. It uses Groovy-based domain-specific language to declare project settings instead of traditional XML. At present, its supported languages are limited to Java, Groovy and Scala, and more languages are planned in the future.

That's Gradle's explanation on Wikipedia. I believe that a person who has not been exposed to construction can't understand it. Me, too. The following is my popular understanding of Grad:

Software development pays attention to code reuse, which can make the project easier to maintain and less code ... Developers can achieve different degrees of code reuse through inheritance, combination and functional modules. But I don't know if you have ever thought that software development is also an engineering operation, not only writing code, but also involving various management of the project (dependence, packaging, deployment, release, differentiated management of various channels ...).

take for example

I am an Android developer. Do you know that there are n Android markets, n mobile phone brands and n mobile phone sizes in China ... Generally, companies will separately contract for different markets to count the downloads of different channels, and may need to do some special processing (brand, size and other hardware information) for different mobile phones. At this time, you can build a separate project for different situations, or better yet, you can control it through some variables, like this.

If(is moto){ do something} else if(is Huawei){ do something} ...

Difference management

But these two solutions have their own shortcomings, especially the former has great code duplication. The latter is slightly better, but the difference of this method is runtime, not static. The processing logic on moto mobile phone has no influence on Huawei mobile phone at all, but this processing logic for moto mobile phone has also been installed on Huawei mobile phone. Through Gradle's productFlavor and buildtype, static level difference control can be realized. You can refer to how to realize a set of codes through gradle to develop APK Byghui with different characteristics.

Speaking of the multi-channel problem mentioned above, different channels generally correspond to different channel numbers. Of course, you can modify one package at a time to generate your multi-channel package, but it is said that the Android App of a domestic group buying website has more than 100 channels. What happened here? Repeated packaging is very different from the previous packaging (only the channel number is different). Just like writing code, we should reuse them. A command can print all the channel packets, and a command can print the specified channel packets. To make things more complicated, you may need different channels to correspond to different signature files, different icons and different server addresses ... This can be easily achieved through Gradle.

Dependency management:

You may need to rely on various jars and libraries for software development. Of course, you can download jar and library projects locally and copy them into your projects, but I don't know if you have heard of something called central warehouse abroad. In this warehouse, you can find all the cans, AAR ... the central repository search engine that you can think of and have never heard of? Here you can find all the dependencies you need, all you need is to specify a coordinate, as shown below:

& ltimg src = "/8883d4b758b288a74eb4c104db854c5f _ b.jpg" data-rawwidth = "330" data-rawheight = "24" class = "content _ image" width = "330" >. You don't need to care about finding, downloading and adding the rest of the dependencies to the classpath. The benefits of maintaining dependencies in this way are as follows:

You don't need to care about finding, downloading and adding the rest of the dependencies to the classpath. The benefits of maintaining dependencies in this way are as follows:

Dependencies will not enter your version control repository (they will be cached under ~/). Gradle/ default).

It is convenient to unload the loading dependency (it is only a coordinate dependency and does not need to be deleted).

Convenient version management. As shown above, 2.3.3 is the version number of Picasso. If it is changed to+,it means downloading the latest version from the central warehouse.

There will be no duplicate copies of the same dependency in different projects (only one copy exists under ~/). gradle)。

Project deployment

I don't know much about this, but as far as I know, through some plug-ins, you can automatically upload your output. Can,. apk,。 War ...) to the designated warehouse, automatic deployment. ...

Li Luo said so much, I don't know if you understand.

To sum up:

Gradle is a build tool that can help you manage differences, dependencies, compilation, packaging and deployment. .................................................................................................................................................

Gradle is not a programming language, and it can't help you realize any practical functions in the software.

Popular explanation is definitely not rigorous, and inappropriate discussion is welcome.

###

Agree with the white point of view and put out the slides shared before.

###

Up to now (1432311622353) * * has received 342 endorsements and thanks. Thank you again for your support! If you think my sharing is helpful to you, please pay attention to me! I will share more and better content with you in the future.

The agreed slides are here.

(It's a slide that I shared internally when I was in LeTV, and the content has been deleted (the part related to the specific project of the company). This slide refers to many other materials, and the internal links are worth opening. )

Edited on 20 17-02-27? 53 comments? Thanks?

unsuccessful

You said to understand it in a popular way, and you don't need to learn any theory. Just follow the popular understanding.

In layman's terms: gradle is used for packaging.

Do you think your problem has been solved? If it is not solved, it is because your question is not well asked. For example, I guess you should ask: Why do you want to package and publish? How many common methods are there for packaging and publishing? Why do you choose gradle among these common methods?

In view of my guess, the popular saying is this:

Take writing java programs with eclipse as an example.

Why do you need to pack:

When you start writing, right-click run. But after all, the program is to be published on the server or used by others. One is that eclipse cannot be installed by everyone who wants to run it, and the other is that the source code cannot be published to everyone. Therefore, you need to publish your code in binary form so that it can run in other environments. This is the packaging.

Why ants?

There are many things to do when packaging, such as the user name and password in the configuration file. Your local development is definitely inconsistent with the actual operation of the program, so you need to have two files, one for development and the other for actual operation. So you have to do these things over and over again every time you pack. Ant allows you to write these things into xml scripts and then run them automatically.

Why maven?

Your project needs many jar packages. For example, you should use log4j for logs and connectors for databases. I'm embarrassed to go out these days without writing programs in spring. Open the downloaded spring package, dozens of cans, I don't know which one to use or not. Moreover, even if you put all these jar packages in, it is very likely that they will not run because they depend on other jar packages. If one day you want to upgrade a small version of spring, all jar packages will be repeated, and you can't remember which jar packages were imported from spring.

So with maven, all you have to do is configure, say I want to use spring 3, all the jar packages have been downloaded for you, and you can just run them directly. If you want to upgrade the version, just change 3 to 4 and download the new jar package for you.

And then what?

It is convenient to pack with maven and jar, but there are still many things to do when I pack, and then you will find that maven really doesn't know how to do these things, so you begin to miss ant.

Gradle can do both maven's work and ant's work, and write scripts in groove language, which is more expressive.

That's what I'm saying.

===

I've only met Greer for two days, and I don't know much about it. The simplified version is as follows:

Ant can automatically package logic.

Maven can also be packaged automatically. Compared with ant, it does more to help you download jar packages.

But maven's packaging logic is too rigid, and customization is too troublesome, so it is not as easy to use as ant. Gradle is a thing that can automatically download jar packages and write your own scripts, and the scripts are easier to write than ant.