Like attention, no more lost, your support means a lot to me!

🔥 Hi, I’m Chouchou. GitHub · Android-Notebook has been included in this article. Welcome to grow up with Chouchou Peng. (Contact information at GitHub)


directory


1. Pre-knowledge

The content of this article will involve the following pre/related knowledge, dear I have prepared for you, please enjoy ~

  • Gradle preconditions: “Gradle” | an article (concept & Groovy & configuration & command)

  • Gradle advanced: “Gradle” | advanced article (Project & Task & building life cycle)

  • Developing Custom Gradle Plugins


2. Gradle plugin overview

2.1 What is Gradle? What is a Gradle plugin?

The essence of Gradle plug-ins is to extract highly modular logic for more efficient reuse. Gradle and Gradle plugins are two different concepts. In previous articles, you have seen how Gradle works at its core. In short, Gradle simply provides a build process. Other reusable tasks (such as compiling Java projects, compiling Android projects, etc.) are obtained by applying Gradle plug-ins.

  • Gradle: Build tool that provides the core build process
  • Gradle plug-ins: Are essentially reusable tasks that depend on the Gradle environment

2.2 Plug-in Types

Gradle plug-ins fall into two broad categories: script plug-ins and object plug-ins.

  • Script plug-ins: A script plug-in is no different from a normal build.gradle file. Although simple, it is the foundation of modularity. The script can be stored locally or on the network. You only need to provide the relative path or URL of the script, for example:
apply other.gradle
Copy the code
  • Object plug-in:The minimum requirement for writing an object plug-in is to provide an implementation class for the org.gradle.api.plugin interface, which requires only onePlugin#apply(Project)Methods.

Plugin.Java

package org.gradle.api;

public interface Plugin<T> {
    void apply(T var1);
}
Copy the code

2.3 Application Plug-ins

In the build.gradle file, you can apply the plug-in using the Apply method, which has two syntax:

  • Grammar 1:
apply plugin 'groovy'
Copy the code
  • Syntax 2:
plugins {
    id 'groovy'
    id 'maven'
}
Copy the code

The ID here is the short name of the plug-in, determined by the name of the resources/ meta-INF /gradle-plugins/[id].properties file in the plug-in.

Note: Do not use both syntaxes in a build.gradle.


3. Customize plug-ins

In this section, we begin to discuss the details of custom plug-ins. As mentioned above, Gradle plug-ins are divided into script plug-ins and object plug-ins. Because script plug-ins are relatively simple and rarely used, we mainly discuss object plug-ins.

[rootProjet. Name =’ project name ‘] [rootProjet. Name =’ project name ‘] [rootProjet. Name =’ project name ‘]

An exception occurred applying Plugin request [id ‘hello’] > Failed to apply plugin [id ‘hello’] > No such property: project for class HelloPlugin

Rootprojet. name=’ project name ‘; rootProjet.name=’ project name ‘

3.1 Step 1: Create a plug-in project

First, we need to create a separate Module to hold the plug-in code. Module names can be arbitrary, but there is a special Module name — “buildSrc”. The main points are as follows:

  • 1. This directory is automatically identified as Module and does not need to be included in setting.gradle.
  • 2. Automatically compile and package at build time and add to classpath in buildScript, no need to manually publish plug-ins;
  • BuildSrc build time prior to project build time.

3.2 Step 2: Initialize the plug-in project

We do the following for the new plug-in project:

  • 1. (optional, if your project doesn’t use Java code) Remove the Java folder;
  • 2. (Mandatory) Add a Groovy folder for plug-in code;
  • 3. (Mandatory) Add the Resources folder to configure the plug-in descriptor;
  • 4. (Mandatory) Modify the build.gradle content.

Here are steps 3 and 4:

3.2.1 Configuring the Plug-in Descriptor

You will need to create a *.properties file in the following directory:

├─ Groovy ├─ Resources ├─ Meta-INF ├─ Gradle-Plugins ├─.propertiesCopy the code

Tip: You can set the location of the Resources folder using the sourceSets {} closure method.

Just two key points to keep in mind here:

  • 1. The *.properties file name is the clipped name (ID) of the plug-in used to apply the plug-in. Such as:
com.xurui.plugin.properties 
Copy the code
  • The *.properties file is configured with the mapping of the plug-in implementation class. You need to use implementation-class to specify the fully qualified class name of the plug-in practice class. Such as:
implementation-class=com.xurui.plugin.HelloPlugin 
Copy the code

3.2.2 Modifying the build.gradle content

The plug-in build. Gradle

Plugins {id 'groovy' // (mandatory) for implementing plugin id 'maven' // for publishing plugin} dependencies {implementation gradleApi() implementation LocalGroovy ()} // Set sourceSets {main {groovy {srcDir 'SRC /main/groovy'} resources {srcDir 'src/main/resources' } } }Copy the code

Apply the Groovy plug-in and add the Gradle API as a compile-time dependency. Generate three JAR files in External Libraries:

3.3 Step 3: Implement the plug-in

Implement the plugin’s theme logic in the Groovy folder, for example:

com.xurui.plugin.HelloPlugin.groovy

Implements Plugin<Project > {@override void apply(Project target) {println "implements Plugin"}}Copy the code

Tip: use the file suffix.groovy.

3.4 Step 4: Apply plug-ins

In the build.gradle file, you can apply the plug-in using the Apply method, which has two syntax. Such as:

  • Grammar 1:
apply plugin 'com.xurui.plugin'
apply plugin 'com.android.application'
Copy the code
  • Syntax 2:
plugins {
    id 'com.xurui.plugin'
    id 'com.android.application'
}
Copy the code

After we apply the plug-in, we can see from the Build Output that our plug-in is working:

Executing tasks: [:app:generateDebugSources] in project E:\workspace\... Configure Project :app custom plug-in. Configure project: APP custom plug-in. Configure project: App custom plug-in. 1864ms > Task :app:preBuild UP-TO-DATE > Task :app:preDebugBuild UP-TO-DATE > Task :app:checkDebugManifest UP-TO-DATE > Task :app:compileDebugAidl NO-SOURCE ... > Task :app:assembleDebug 【 Execution end 】 total time: 141ms BUILD SUCCESSFUL in 2s 4 actionable tasks: 4 up-to-dateCopy the code

4. Publish & rely on plug-ins

There are two options for publishing plug-ins, which are all about publishing to different addresses:

  • Publish to local repository;
  • Publish to a remote repository.

4.1 Plug-in properties

Published plug-ins need to be configured with three properties that together form a unique representation of the plug-in:

Plug-in properties describe
groupId Name of organization/company
artifactId Project/module name
version The current version number of the project/module

4.2 Configuring Plug-in Properties

We used the mavenDeployer plugin to publish the repository, you need to add the following configuration to the plugin build.gradle:

The plug-in build. Gradle

Plugins {id 'groovy' // (required) for implementing plugin id 'maven' // (mavenDeployer plugin) for publishing plugin}... uploadArchives { repositories { mavenDeployer { repository(url: uri('.. GroupId = 'xurui_groupId' pom.artifactid = 'xurui_artifactId' pom.version = '1.0.0'}}Copy the code

To publish to a remote repository, simply change the repository URL, for example:

repository(url: RELEASE_URL) {
    authentication(userName: "xurui", password: "xxx")
}
Copy the code

4.3 Publishing Plug-ins

Then, execute the gradlew uploadArchives command (note the relative path) to execute the uploadArchives Task and publish the plug-in. You can also use Gradle Window to execute quickly:

The plugin file will be generated in the specified path after publication:

4.4 Dependent Plug-ins

Tip: If you implement the plug-in in the buildSrc directory, don’t manually add dependencies. Gradle automatically adds buildSrc builds to the classpath.

In projects that rely on plug-ins, we need to add plug-ins to the classpath. Such as:

Project root build. Gradle

buildscript { repositories { google() jcenter() maven { url uri('./repo') } } dependencies { classpath 'com. Android. Tools. Build: gradle: 3.5.3' classpath 'xurui_groupId: xurui_artifactId: 1.0.0 allprojects {repositories {}}  google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }Copy the code

Where xurui_groupId is group, xurui_artifactId is artifactId, 1.0.0 is the version number, you can also use this syntax:

Dependencies {classpath group: 'xurui_groupId ', name: 'xurui_artifactId ', version: '1.0.0'}Copy the code

4.5 Application Plug-ins

Once this is done, the plug-in can be applied smoothly:

The project build. Gradle

plugins {
    id 'com.xurui.plugin'
    id  'com.android.application'
}
Copy the code

5. Customize Extension and Task

Editting…


6. Summary

  • Gradle is a build tool that provides the core build process, while Gradle plug-ins are essentially reusable tasks that depend on the Gradle environment.
  • Plug-in types are divided into script plug-in & object plug-in, object plug-in is used more;
  • This article will walk you through the steps of customizing plug-ins and hopefully help you avoid the pitfalls. In a later article, I will work with you to use the custom Gradle plugin to do the actual work. Please pay attention to ~

The resources

  • Three Ways to Customize Gradle plugins. By Wangshu Liu
  • An in-depth exploration of Gradle automation build technology (4). By JsonChao
  • Gradle in Action, Chapter 8. By Benjamin Muschko

Creation is not easy, your “three lian” is chouchou’s biggest motivation, we will see you next time!