1 Make one as fast as you can Gradle plug-in unit
As a whole , branch 4 Step
- Definition
plugin
- Define extension
- Introducing extensions into plug-ins
apply
Reference plug-ins
1.1 Definition plugin
Be careful : The following can be defined in any module build.gradle in .
// groovy How to write it
class FreeCoderPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
}
}
Copy code
1.2 Define extension
What is extension , The following article will talk about .
// groovy How to write it
class FreeCoderExtension {
def name = "perry"
}
Copy code
1.3 Introducing extensions into plug-ins
// groovy How to write it
class FreeCoderPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
// 1
def extension = project.extensions.create('freecoder', FreeCoderExtension)
// 2
project.afterEvaluate {
println("hello $extension.name")
}
}
}
Copy code
Mark 1 It's about , There are several points to explain :
project
Indicates which project the current plug-in is referenced by , So it means which project , For example, I am inapp
Modulargradle
This plug-in is referenced in , Then the printed value is “:app
”.freecoder
Naming is arbitrary , It is an extended property , You can assign values to attributes in extension classes in plug-ins .
Mark 2 It's about :
project.afterEvaluate
Indicates that the internal printing method is added to the queue of the execution stage , Wait for the whole thing build.gradle
After the file definition phase , Output and print at the execution stage .
freecoder {
name 'zp'
}
Copy code
This freecoder
Namely FreeCoderExtension
Another name for .
1.4 apply quote
apply plugin: FreeCoderPlugin
Copy code
FreeCoderPlugin
At the plug-in definition , Defined plug-in class name . Then at the end of the project terminal
In the implementation of ./gradlew
(mac In computer ).
[email protected] MVVMDemo % ./gradlew
> Configure project :app
hello zp
> Task :help
Welcome to Gradle 6.5.
Copy code
All the above are in a build.gradle
Simple definition and use in the file , But actually ,gradle
It provides us with a user plug-in project buildSrc
, Various custom plug-ins can be configured through this project , Then for use in the project .
2 To configure buildSrc
buildSrc
Is a special file project , It's for gradle
For plug-in projects , stay AndroidStudio
By creating a named buildSrc
Of Java
perhaps Android Library
Of module
, Then remove setting.gradle
Configuration of this item in , It can run normally .
include ':app'
include ':buildSrc' // Remove this line
Copy code
2.1 About buildSrc
Because it is a plug-in project , Therefore, the execution order will take precedence over any sub module , stay setting.gradle
After execution . The structure of the whole project is as follows :
2.2 Be careful
About the writing of plug-ins , have access to java、kotlin、groovy
, Personal habits , Here I am. java For example . The only thing to note here is resources The contents under the folder , The directory structure is as follows ( Not one letter can be wrong ):
resources/META-INF/gradle-plugins
Copy code
Finally xxx.properties
As a configuration file , Put the corresponding plugin
Expose , Here it means FreeCoderPlugin
This plugin , as follows :
implementation-class=com.plugin.FreeCoderPlugin
Copy code
among com.plugin.FreeCoderPlugin
It refers to the full class name of the location where the plug-in is located . file name xxx.properties
Medium xxx, Exposed for plug-ins ID
name , As in build.gradle
The way of reference in the file .
plugins {
id 'com.free.coder'
}
freecoder {
name "zp"
}
Copy code
Put the other two java
The complete code of the file is pasted
public class FreeCoderExtension {
public String name = "perry";
public void setName(String name) {
this.name = name;
}
}
Copy code
public class FreeCoderPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
final FreeCoderExtension extension = project.getExtensions()
.create("freecoder", FreeCoderExtension.class);
project.afterEvaluate(innerProject -> System.out.println("extension: " + extension.name));
}
}
Copy code
such , When again in the project terminal
In the implementation of ./gradlew
when , Will print out extension: zp
. Here we are , A complete customization gradle
Even if the plug-in is completed .
There's another question above : Is what extension is , So this is actually where , We can understand this for the time being , This extension is to define a data entity and set the entry of data attributes , As for where it will be used , It depends on the specific function of the plug-in .