In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly analyzes why Gradle is so popular, the content is detailed and easy to understand, the operation details are reasonable, and it has certain reference value. If you are interested, you may wish to follow Xiao Bian to have a look. Let's follow Xiao Bian to learn more about "why Gradle is so popular".
After Android Studio took over the market, Gradle, a building tool, has no place like it, and we need to learn and use it to create value for us.
Before we dive into Gradle, it's worth understanding why it's so popular.
Android app building process
The Android app building process is complex, as shown in the figure:
There are mainly the following steps:
The main resource files (layout, values, etc.) are compiled by aapt and referenced in an R file
Java code is compiled into JVM bytecode (.class files) by the Java compiler
JVM bytecode is then converted to dalvik bytecode (.dex file) by dex tool
These.dex files, compiled resource files, and other resource files (such as images) are then packaged into an apk
The apk file is signed by debug/release key file before installation
mounted to the apparatus
A few caveats.
Note that the *** step in the above steps is the main resource file, and some special resource files will not be compiled, such as files under the assets directory, files under the raw directory, and pictures, will not be compiled. Only files under raw will generate id in R file
If the apk is formally signed, you also need to use the zipalign tool to align the apk, which has the advantage of reducing memory overhead when the application runs
As can be seen from the above introduction, such a complex construction process would be too cruel if it required manual labor to complete, so there were various construction tools.
Android builds old favorites: Ant, Maven
Prior to Gradle, Apache Ant and Maven were widely used.
Ant
Ant was released in 2000 and quickly became a build tool for Java projects.
The advantages of Ant are:
Simple, easy to learn, no special preparation required
Process-based programming makes building flexible
Later plug-ins were supported.
The downside is the use of XML as the script configuration format, which can quickly become unmanageable unless it's a very small project.
Maven
Maven was released in 2004. The goal is to solve some of the problems associated with using Ant.
Maven also uses XML as a file format for build configuration, but the file structure has changed dramatically:
Ant requires developers to list all the commands needed to execute a task
Maven relies on conventions and provides ready-made callable targets
Not only that, but one of Maven's more important advances was the ability to automatically download dependencies from the web (and Ant later through Ivy), which revolutionized the way we develop software.
The disadvantages of Maven are:
Dependency management does not handle conflicts between versions of the same library file well (Ivy is better at this)
XML as a configuration file format has strict structure hierarchy and standards, it is difficult to customize the target
Maven mainly solves dependency management problems, but errors in using XML make it repeat itself. In fact, it is difficult to write complex and customized build scripts with Maven. In large projects, it often has hundreds of lines of code before doing something "special", even worse than Ant.
After reflecting on the mistakes of the first two building tools, the predecessors proposed the concept of DSL (Domain Special Language), with the goal of designing a language that can solve specific domain problems. One success story for DSL in building this is Gradle.
Android's new love: Gradle
Gradle, released in 2012, deeply learned from the experiences and lessons of its predecessors and made many improvements.
Gradle has a convention over configuration principle, which provides default values for settings and properties, which makes it easier to get started than Ant or Maven.
Gradle build scripts no longer use xml, but are written using Groovy DSL (Groovy is a dynamic language based on Java Virtual Machine, so Android developers will be familiar with it), making build scripts clearer and simpler than those written in Ant and Maven. Here is a comparison of Maven code and Gradle code to achieve the same goal:
Gradle combines Ant's power and flexibility with Maven's dependency management, plug-in support, and ease of use. As a result, it gained widespread attention as soon as it was launched, and was selected by Google as the default build tool for Android Studio in 2013.
Gradle is designed in a way that makes it easy to extend builds and plug into existing build processes, and Google promoted Gradle and Android Studio with the goal of making code reuse, build variants, configuration, and custom build processes easier.
Some people may worry about whether they need to learn Groovy, a new language, specifically mentioned in Gradle for Android:
Groovy code is very readable, and if you've studied Java, the curve for learning Groovy isn't steep. A deeper understanding of Groovy is especially important if you want to build your own tasks and plugins. But because Groovy is based on the Java Virtual Machine, you can write your custom plug-ins in Java or any other JVM-based language.
Gradle Key Concepts and Construction Life Cycle
There are two key concepts in Gradle: projects and tasks.
Each build.gradle build script file represents a project:
The task is defined in the build script:
Each build includes at least one project, and each project includes at least one task.
Building Life Cycle
A Gradle build usually consists of the following three phases:
1. initialization
The project instance will be created at this point, if the project has multiple modules, or depends on multiple libraries, and they all have corresponding build.gradle files, multiple project instances will be created.
2. configured
At this stage the build script is executed and tasks are created and configured for each project instance
3. implementation
At this stage Gradle will decide which tasks will be performed based on the configuration of the build script
There may be interdependencies between different tasks, so Gradle produces a dependency graph during the configuration phase, and tasks without dependencies are usually executed first, and then other tasks are executed one by one. Once a task has been performed, it cannot be performed again.
Gradle Wrapper
Gradle is constantly evolving, and there may be cases where new versions are incompatible with older versions, so the official Gradle Wrapper is provided for us to be compatible and adaptable to different versions to ensure that the build is repeatable.
The normal Gradle script command is:
gradle ...
The Gradle Wrapper command simply adds a w after the gradle:
gradlew ...
The Mac version is slightly different:
./ gradlew ...
Developers or automated build systems can use wrappers to run gradle commands, which solve compatibility problems, etc. So we don't even need to install gradle, just use gradle wrapper, as recommended in Android Studio:
I didn't use the wrapper above because when you run gradle w, gradle wrapper automatically downloads the version of gradle you need but don't have and uses it. This is probably because someone else modified the gradle version, uploaded it to git, and then caused you to download half a day when running the command.
However, the official still recommends using wrappers, after all, this is more stable.
About "why Gradle is so popular" on the introduction to this, more related content can be searched for previous articles, I hope to help you answer questions, please support the site!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.