[toc]

I. Purpose of custom plugin

A plugin is a Gradle extension. Why do you need a custom plugin when you have a Task?

Here I see custom plug-ins as a way to combine a series of tasks to provide a higher degree of granularity for reuse.

Gradle Plugin packages can also be published in jars or AAR to remote repositories

Steps for customizing plug-ins

2.1 Methods of Customizing plug-ins

There are two ways to create a plugin module: publish the plugin independently and simplify the plugin.

  • The simplified plug-in definition takes advantage of Gradle’s ability to automatically recognize the buildSrc directory and place custom plug-ins in the buildSrc directory, which has the disadvantage of not being able to publish to be reused remotely.
  • The standalone plug-in approach is basically the same as the simplified approach, but can be published to a remote Maven repository

2.1.1 Simplified way to define plug-ins

Create a new folder named buildSrc and create build.gradle under this directory. Note that the name and case must be exactly the same. Gradle automatically scans the buildSrc directory during the configuration phase and configates buildSrc.

Create two new directories under buildSrc

  • src/main/groovy
  • src/main/java

Then add the corresponding plugin to build.gradle

buildscript { repositories { google() jcenter() mavenCentral() } dependencies { classpath 'com. Android. Tools. Build: gradle: 4.1.2'}} / / identify groovy plugin apply plugin: "groovy" apply the plugin: "java" repositories { mavenCentral() } dependencies { }Copy the code

Among them:

apply plugin : "groovy"
apply plugin: "java"
Copy the code

When you add these two lines, you’ll notice that Android Studio will change the Groovy directory and Java directory to blue to indicate that they’ve been identified, or you can add just one such as Groovy or Java directory. In this way, the language in which we implement the plugin will be converted according to the plugin of our choice. Groovy is generally recommended to be more efficient and concise.

2.1.1 Independent distribution defines plug-ins

Create a new Java module and name it plugins. Create a new directory SRC /main/groovy and add build.gradle to your plugins directory.

plugins { id 'groovy' } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) // We need to implement Gradle Plugin<T> interface, so we rely on gradle API implementation gradleApi() When writing custom plug-ins, groovy is faster. implementation localGroovy() }Copy the code

The difference is that you need to add dependencies under Dependencies

implementation gradleApi()
implementation localGroovy()
Copy the code

2.2 Declare the configuration plugin

The purpose of this step is to declare the name of the defined plugin so that we can rely on the plugin.

Create a new resources/ meta-INF /gradle-plugins directory in the SRC directory and create a new xxx.xxx.xxx.properties file in that directory. The XXX represents the full package path of the plug-in without the class name. For example, BsDiffPlugin under com.hch.bsdiff, the generated properties file is com.hchbsdiff.properties, and the content is

implementation-class=com.hch.plugin.BsDiffPlugin
Copy the code

2.3 Custom Plugin

Custom plugins are where the real work is. Typically, a plugin corresponds to a task and a plug-in is defined by inheriting from the plugin

Class BsDiffPlugin Plugin<Project>{@override void apply(Project Project) {project.afterevaluate { Will not execute, /gradlew bsDiff project.task(type:BsDiffTask, "bsDiff"){ inputs.files "${project.rootDir.absolutePath}/temp/testold.txt","${project.rootDir.absolutePath}/temp/testnew.txt" outputs.files "${project.rootDir.absolutePath}/temp/testdiff.patch" } } } }Copy the code

In the gradle configuration phase, plugin’s Apply method is executed. Normally, in this method, we associate a task and pass in the parameters of the associated task. From the project parameter, we can get all the information related to the build process.

Here we define a BsDiffPlugin and generate a bsDiffTask.

2.4 Define tasks associated with plug-ins

BsDiffTask

Class BsDiffTask extends DefaultTask{// When an external task is generated, Will modify the project. Task (type: com. HCH. Plugin. BsDiffTask, "BsDiffWrapper ") BsDiffTask(){group" difference task "description "bsDiff"} @taskAction void diff(){println "start diff...." File fileOld = inputs.files.files[0]; File fileNew = inputs.files.files[1]; File diff = outputs.files.files[0]; BSDiff.bsdiff(fileOld , fileNew , diff); println "end diff...." }}Copy the code

2.5 Using customized plug-ins

2.5.1 Simplify plug-in dependencies

For simplified plug-ins, we only need to use custom plug-ins in build.gradle in our project app in two ways

Plugins {id 'com.hch.plugin.bsDiff'} // Plugins {id 'com.hch.plugin.bsDiff'} // Plugins {id 'com.hch.plugin.bsDiff'Copy the code

The ID here is the name of the properties

2.5.2 Independent Plug-in Dependency

If it is a standalone plug-in, you also need to add plugin dependencies

Add to build.gradle in the root directory

buildscript{ repositories{ //... Maven {url "XXXXX"}} dependencies{//... classpath('com.hch.bsdiff:BsDiffPlugin') } }Copy the code

It also needs to be added to build. Gradle in app

Plugins {id 'com.hch.plugin.bsDiff'} // Plugins {id 'com.hch.plugin.bsDiff'} // Plugins {id 'com.hch.plugin.bsDiff'Copy the code

2.5.3 Using tasks in plug-ins

Once the plug-in is dependent on the project, we can use the tasks in the plug-in

afterEvaluate {
    println "assembleRelease === ${assembleRelease}"
    assembleRelease.finalizedBy 'bsDiff'
}
Copy the code