In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the gradle need to know things, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.
Preface
Definition of gradle (from Wikipedia)
Gradle is a project automation construction tool based on the concepts of Apache Ant and Apache Maven. It uses a domain-specific language based on Groovy to declare project settings, rather than the traditional XML. Currently, the languages it supports are limited to Java, Groovy and Scala, and it is planned to support more languages in the future.
Popular understanding: gradle is a construction tool that we can use to manage multiple projects (dependency, packaging, deployment, release, difference management in various channels).
Sometimes, we will have some personalized build requirements, such as we introduce third-party libraries, or we want to do something else in the general build process, then we have to make some changes to the default build rules of the system. At this point we have to give orders to Gradle ourselves, and at this time we need to use words that Gradle can understand, that is, Groovy.
We mentioned at the beginning that "Gradle is a build tool." In fact, when we want a more flexible build process, Gradle becomes a programming framework-- we can program the build process the way we want it to. In other words, when we use Gradle as a build tool, we only need to master the basic way to write its configuration script OK, and when we need to highly customize the build process, we must have knowledge such as Groovy.
Problems encountered
We often encounter the following problems when building real-time multi-projects:
1. Depending on different versions of a library at the same time, a duplicate class error occurred during compilation
2. Different versions of gradle api reported an error
3. I can't write gradle configuration, can't read gradle syntax, and don't know where to start.
4. There is no way to start with the error report of gradle in the compilation process.
Wait...
Next, we will learn these things about gradle step by step from the actual project. The purpose of this article is to learn the ideas of gradle, and the in-depth details will be ignored.
Unveil Gradle
1. Understand the packing command gradle clean assembleDebug/assembleRelease
The above command can be divided into three parts, gradle,clean, assembleDebug; is actually the same as we execute the script, gradle is the executor, and clean and assembleDebug are input parameters, here they represent different task, just like gradle task1 task2.
What is task?
Write on build.gradle
Task task1 {println "= = > task1"} task task2 {println "= = > task2"}
This defines two task;. When we execute gradle task1 task2-Q (- Q sets the log level), we will theoretically see the log output:
= > task 1
= = > task 2
The relationship of task includes dependsOn,mustRunAfter and so on. We will skip this part first because there is less use in the project.
Here we briefly talk about the concept of closures:
A closure is an open, anonymous block of code in the context of the code in groovy. It can access its external variables or methods
Please google yourself for more details
However, when we execute gradle task1 task2-Q in the project, we find that the output looks like this:
SeeyouClient git: (SeeyouClient-dev) ✗ gradle task1 task2-Q
DoPackage value:False
Configuration 'compile' in project': app' is deprecated. Use 'implementation' instead.
= anna apply start=
Configuration do:
Include
* * onClick
* * onItemClick
* * onCheckedChanged
* * onItemSelected
* * onSwitchButtonCheck
* * onItemLongClick
* * onLongClick
* * onPullRefresh
* * OnRefresh
Configuration do:
Exclude
Org/conscrypt/
Configuration do:
Exceptions
Java/lang/Exception
Java/lang/NullPointerException
Configuration do:
Switch
Custom
Need inject=false
= anna apply end=
Configuration 'provided' in project': app' is deprecated. Use 'compileOnly' instead.
Configuration 'debugCompile' in project': app' is deprecated. Use 'debugImplementation' instead.
= > task 1
= = > task 2
DexKnife: Processing Variant
DexKnife: processSplitDex true
DexKnife: processing Task
-- tinker build warning--
Tinker auto operation:
Excluding annotation processor and source template from app packaging. Enable dx jumboMode to reduce package size.
Enable dx jumboMode to reduce package size.
Disable preDexLibraries to prevent ClassDefNotFoundException when your app is booting.
Disable archive dex mode so far for keeping dex apply.
Tinker will change your build configs:
We will add TINKER_ID=117 in your build output manifest file build/intermediates/manifests/full/*
If minifyEnabled is true
You will find the gen proguard rule file at build/intermediates/tinker_intermediates/tinker_proguard.pro
And we will help you to put it in the proguardFiles.
If multiDexEnabled is true
You will find the gen multiDexKeepProguard file at build/intermediates/tinker_intermediates/tinker_multidexkeep.pro
And we will help you to put it in the MultiDexKeepProguardFile.
If applyResourceMapping file is exist
We will build app apk with resource R.txt file
If resources.arsc has changed, you should use applyResource mode to build the new apk!
-
Task spend time:
Why is that? The reason is that gradle has its own lifecycle:
Initialization phase: responsible for determining how many Projects participate in the construction:
Execute settings.gradle first
Configuration phase: responsible for configuring the Projects created in the initialization phase:
For example, if you add Task and modify the behavior of Task, the contents of closures will be executed, and the contents of build.gradle will be executed.
Execution phase: perform tasks according to the configuration of the configuration phase:
Execute the corresponding content of task, such as doLast,doFirst
So the output log of gradle task1 task2-Q is understandable. In fact, according to task1 and task2, executing gradle task1 task2-Q and gradle-Q actually have the same effect.
3. What exactly did gradle clean assmebleDebug do? (source code tracking and dependency analysis to figure out the compilation process)
1. Open the gradle-4.5.1/bin/gradle file and you can see that the code has been executed:
Eval set-- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\ "- classpath"\ "$CLASSPATH\"org.gradle.launcher.GradleMain" $APP_ARGS "
Finally call exec "$JAVACMD"$@" to execute; so the entry is: org.gradle.launcher.GradleMain
For details, please refer to: https://blog.csdn.net/yanbober/article/details/60584621
2. The DefaultGradleLauncher will eventually be called, and we can clearly see its life cycle:
The most important thing to note here is that when we only execute gradle-Q, we actually get to the stage of TaskGraph every time; that is, all the tasks has been combed out.
Public class DefaultGradleLauncher implements GradleLauncher {/ / (version 4.5.1 here, life cycle is more detailed) private enum Stage {Load, LoadBuild, Configure, TaskGraph, Build, Finished} / / 2.14.1 is: private enum Stage {Load, Configure, Build} / / Core method private void doBuildStages (Stage upTo) {try {loadSettings (); if (upTo = = Stage.Load) {return;} configureBuild () If (upTo = = Stage.Configure) {return;} constructTaskGraph (); if (upTo = = Stage.TaskGraph) {return;} runTasks (); finishBuild ();} catch (Throwable t) {Throwable failure = exceptionAnalyser.transform (t); finishBuild (new BuildResult (upTo.name (), gradle, failure); throw new ReportedException (failure);}} / / call timing @ Override public SettingsInternal getLoadedSettings () {doBuildStages (Stage.Load); return settings } @ Override public GradleInternal getConfiguredBuild () {doBuildStages (Stage.Configure); return gradle;} public GradleInternal executeTasks () {doBuildStages (Stage.Build); return gradle;}
Fourth, what is the use of knowing the compilation process?
1. We often see code like this in app/build.gradle:
Project.afterEvaluate {...} android.applicationVariants.all {...} gradle.addListener (new TaskListener ()) apply from'.. / mvn.gradle'...
Here we introduce a few concepts:
Project
The Project.java corresponding to the gradle source code (hold down control and click project will automatically jump). It provides some external methods, such as afterEvalute,beforeEvalue;, can flexibly use these api after understanding the compilation process.
Android
Corresponding to the AppExtension.java file of the gradle plug-in, some external parameters and methods are provided. We can use android.xxx to access any parameters and methods in app/build.gradle.
Gradle
The Gradle.java object in the corresponding gradle source code also provides a series of methods for external use.
Then suppose we have a requirement: find a task called cleanBuildCache, find it, add an action, and print a line of words; to achieve this requirement, first how do we traverse all the task of this app:
There are many ways to write it:
Gradle.getTaskGraph () .whenReady {project.tasks.each {task- > println "taskName:" + task.getName ()} project.afterEvaluate {project.tasks.each {task- > println "taskName:" + task.getName ()}}
Execute gradle-Q and feel it.
Let's take a look at how to add action
Project.afterEvaluate {project.tasks.each {task- > / / println "taskName:" + task.getName () if (task.getName () .equals ("cleanBuildCache")) {println "find cleanBuildCache!" List
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.