This is the 9th day of my participation in Gwen Challenge

Preface:

The gradle build file is composed of two apis: Project and Task. If you want to view the API online, you can use the gradle build file to create a Project. We can use debugApi (” com. Android. View the build: gradle: 4.1.2 “) to rely on to the project, so that we can see the source code in the quick jump

Project

A build.gradle file is simply a Project. You can use the API provided by the Project interface to interact with the built file.

In our new Gradle build Project, will be in the root directory is a Settting Gradle/Setting. Gradle. KTS (if using kotlin), script execution according to the Setting. When Gradle configure and create Project object, such as:

include(":app")
rootProject.name = "android-samples"
Copy the code

Representing Root Project ‘Android-samples’ contains a Sub Project named ‘app’ (each module has a build.gradle, SubProject scripts are executed in the same order as include scripts. A Project is essentially a collection of tasks, and each Task is a minimal unit of execution, such as compiling a class, running test, Register

(“clean”) {Delete (rootProject.builddir)} add Task:

  • Use the Create method of TaskContainer to addTaskContainer.create(String)Use getByName on TaskContainer to locate the task, such as TaskCollection.getByName(String).
TaskContainer API

TaskContainer inherits from TaskCollection so TaskContainer is a collection that manages tasks

We can get the TaskContainer instance by calling project.gettasks () or using the Tasks attribute in the build script.

Method Description
Create (String name) and overload methods Create a task and add it to TaskContainer
Register (String name) and overload methods Define a new task and set it inNeed to be(Note the difference with create)
replace​(String name) Create a Task with the given name and add it to TaskContainer, replacing any existing Task with the same name
findByPath​(String path) ‘path’ is the Task name, relative path, or absolute path. The relative path is relative to the TaskContainer. This method returns NULL if no task exists with the given path
getByPath​(String path) ‘path’ is the Task name, relative path, or absolute path. The relative path is relative to the TaskContainer. If there is no task with a given path,This method throws an exception(Note the difference with findByPath)
Script API
Method Description
apply​(Closure closure) Refer to the script
afterEvaluate​(Closure closure) Execute the script after it is executed
allprojects​(Closure configureClosure) Configure this project and each of its subprojects
beforeEvaluate​(Closure closure) Execute the script before executing it
buildscript​(Closure configureClosure) Configure the resources required by gradle scripts themselves
copy​(Closure closure) Copy files and directories
delete​(Object… paths) Delete files and directories
dependencies​(Closure configureClosure) Configure dependencies for this project
mkdir​(Object path) Create a directory and return a file pointing to it

Apply (Closure Closure) Closure for ObjectConfigurationAction delegate type

Apply from(path) // HTTP services can be referenced for local scripts or using urIs

Apply Plugin ((String pluginId)// The ID of the plugin configuration

apply plugin(Class<? extends Plugin> pluginClass)

The buildScript closure delegate type is ScriptHandler

Dependencies The delegate type of the closure is DependencyHandler

  • Add (String configurationName, Object dependencyNotation) function

ConfigurationName is the name of the configuration such as implementation, API, debugApi and dependencyNotation is the dependencyNotation that we need to configure

DependencyNotation is a string consisting of group,name version, or classes implementing the Dependency interface that essentially define group,name, and version

We get the Dependencies object under the Project object and add the dependencies

Configure configurations {implementation.configure {exclude("com.gavindon")}} dependencies. Add ( "implementation", "org.jetbrains.kotlin:kotlin-stdlib:${parent? .extra? .get("kotlin_version")}") {// Exclude specifies the group or module as appropriate Exclude ("com.stxx")} // Exclude ("com.stxx") Exclude,include dependencies. Add ("implementation", org.gradle.api.internal.artifacts.dependencies.DefaultClientModule( "androidx.constraintlayout", "constraintlayout", "2.0.4")) {exclude("com.stxx") // Exclude ("com.stxx") // Whether to pass dependencies (simply, whether to download other libraries that depend on the current dependency library) isTransitive = false} // Add files dependencies.add( "implementation", Run {implementation(" Androidx.core :core-ktx:1.3.2") Implementation (" androidx appcompat: appcompat: 1.2.0 ") implementation (" com. Google. Android. Material: material: 1.2.1 ") TestImplementation androidTestImplementation (" junit: junit: 4.13.2 ") (" androidx. Test. Ext: junit: 1.1.2 ") AndroidTestImplementation (" androidx. Test. Espresso: espresso - core: 3.3.0 ") debugApi (" com. Android. View the build: gradle: 4.1.2 ") Implementation (/ / add a file directory fileTree (Pair (" dir ", "libs"), the Pair (" include ", "*. Jar"), the Pair (" include ", "*. Aar")))}Copy the code

We can manage dependencies uniformly in buildSrc, define library addresses, and add dynamically. Gitee source code address