Question 1 The parsing order of gradle: setting.gradle of rootproject, then build.gradle of rootproject, and then each subproject. Therefore, the build.gradle under the project will precede the build.gradle under the app. Question 2 In build.gradle, we can introduce plugins through apply plugin:, or we can introduce function definitions or tasks in other gradle scripts through apply from .gradle Question 3 You The check and clean mentioned are actually tasks. Generally, hooks refer to the life cycle of gradle:
After parsing setting.gradle, before starting to parse build.gradle, if you want to do something here (change the content of build.gradle), you can write it in beforeEvaluatebeforeEvaluate 举个例子,我们将我们的一个subproject中的apply plugin改掉,原来是一个library工程,我们希望它被当作application处理:
project.beforeEvaluate {
// Change android plugin from `lib' to `application' dynamically
// FIXME: Any better way without edit file?
if (mBakBuildFile.exists()) {
// With `tidyUp', should not reach here
throw new Exception("Conflict buildFile, please delete file $mBakBuildFile or " +
"${project.buildFile}")
}
def text = project.buildFile.text.replaceAll(
'com\.android\.library', 'com.android.application')
project.buildFile.renameTo(mBakBuildFile)
project.buildFile.write(text)
}
在所有build.gradle解析完成后,开始执行task之前,此时所有的脚本已经解析完成,task,plugins等所有信息可以获取,task的依赖关系也已经生成,如果此时需要做一些事情,可以写在afterEvaluateFor example, we Change the apply plugin in one of our subprojects. It turns out to be a library project. We want it to be treated as an application:
project.afterEvaluate {
// Set application id
def manifest = new XmlParser().parse(project.android.sourceSets.main.manifestFile)
project.android.defaultConfig.applicationId = manifest.@package
}
After all build.gradle parsing is completed and before task execution starts, all scripts have been parsed. All information such as tasks and plugins can be obtained. Task dependencies have also been generated. If you need to do something at this time Things can be written in afterEvaluate
Each task can define doFirst, doLast, which is used to define the code to be executed before or after the execution of this task
rrreee
Question 4
The language based on gradle is groovy, which is also a programming framework: 🎜🎜Official documentation🎜: https://docs.gradle.org/current/dsl/🎜🎜Android plug-in documentation🎜: https://github .com/google/android-gradle...🎜Finally, you should look at gradle from a programming perspective and solve problems by consulting the documentation: http://m.blog.csdn.net/article/details?i...🎜
My point is that gradle tasks have an execution order, such as 3 tasks, assemble, check, and build. These three tasks all have dependencies. According to the execution order of gradle, the dependent ones are executed first. It is recommended to read the official documentation of gradle android plugin and the documentation of gradle
The .gradle file seen in AS should actually be regarded as a configuration file, so the order of execution cannot be strictly stated. In fact, we just configured the compilation process in the .gradle file (most of the regular processes such as build, assemble, etc. AS have been defined. At this time, the parameters we configure in .gradle are actually the parameters used by these process tasks. Of course, we can also configure them ourselves. Define tasks, all gradle tasks can be viewed through the Gradle panel). Looking back, when we write gradle files, including when sync with gradle, we are actually just declaring our compilation tasks, so all tasks will be declared at this time, but it does not mean compilation These tasks will be executed every time. Which tasks are executed depends on the calls between tasks.
I agree with what @Youming said, .gradle files do not follow any specific order. .gradle belongs to configuration files, large projects, sub-projects, large environments, and sub-environments. There will be a sequence during execution, environment first and then tasks. This is the basic rule.
It is recommended to read "gradle for android". There is an electronic version online. I read it in about one day and basically have a clear understanding of the android-plugin
Question 1
The parsing order of gradle: setting.gradle of rootproject, then build.gradle of rootproject, and then each subproject. Therefore, the build.gradle under the project will precede the build.gradle under the app.
Question 2
In build.gradle, we can introduce plugins through apply plugin:, or we can introduce function definitions or tasks in other gradle scripts through apply from .gradle
Question 3
You The check and clean mentioned are actually tasks. Generally, hooks refer to the life cycle of gradle:
The language based on gradle is groovy, which is also a programming framework: 🎜🎜Official documentation🎜: https://docs.gradle.org/current/dsl/🎜🎜Android plug-in documentation🎜: https://github .com/google/android-gradle...🎜Finally, you should look at gradle from a programming perspective and solve problems by consulting the documentation: http://m.blog.csdn.net/article/details?i...🎜Each task can define doFirst, doLast, which is used to define the code to be executed before or after the execution of this task
rrreee Question 4
My point is that gradle tasks have an execution order, such as 3 tasks, assemble, check, and build. These three tasks all have dependencies. According to the execution order of gradle, the dependent ones are executed first. It is recommended to read the official documentation of gradle android plugin and the documentation of gradle
The .gradle file seen in AS should actually be regarded as a configuration file, so the order of execution cannot be strictly stated. In fact, we just configured the compilation process in the .gradle file (most of the regular processes such as build, assemble, etc. AS have been defined. At this time, the parameters we configure in .gradle are actually the parameters used by these process tasks. Of course, we can also configure them ourselves. Define tasks, all gradle tasks can be viewed through the Gradle panel).
Looking back, when we write gradle files, including when sync with gradle, we are actually just declaring our compilation tasks, so all tasks will be declared at this time, but it does not mean compilation These tasks will be executed every time. Which tasks are executed depends on the calls between tasks.
I agree with what @Youming said, .gradle files do not follow any specific order. .gradle belongs to configuration files, large projects, sub-projects, large environments, and sub-environments. There will be a sequence during execution, environment first and then tasks. This is the basic rule.
It is recommended to read "gradle for android". There is an electronic version online. I read it in about one day and basically have a clear understanding of the android-plugin
Share a very good gradle learning article: http://blog.csdn.net/u0108184...