current position:Home>Android gradle custom plug-in implementation

Android gradle custom plug-in implementation

2023-01-25 11:29:40Someone from Yungu Li

GradleBasic overview of plugin implementation

Gradle插件的三种方式,如下表所示:

插件实现方式解释说明
Build script 脚本把插件写在build.gradle文件中,一般用于简单的逻辑,只在该build.grade文件可见
buildSrc目录将插件源代码放在buildSrc/src/main中,Only visible to this item
独立项目一个独立的Java项目/模块,可以将文件包发布到仓库(Jecenter,Maven),使其他项目方便引入

The three methods are described below:

1、build script 脚本

Plugins implemented this way onlybuild.gradle文件可见,例如在app的build.gradle中,插件名PluginDemo实现方式:

apply plugin: PluginDemo //引入插件

//定义插件
class PluginDemo implements Plugin<Project> {
    

    @Override
    void apply(Project project) {
    
        println 'The first way is to implement plugins'
    }
}

在这里插入图片描述
运行gradlewwill print the above content
在这里插入图片描述

2、buildSrc目录

这种方式创建的Module是一个独特的,AndroidStudio会自动识别buildSrc插件模块.
打开AS——>File——>New Module——>选择Java or Kotlin Library
在这里插入图片描述

填写Library Name一定要是 buildSrc.Otherwise, the system cannot recognize the plug-in,After the creation is complete, the project is as follows:
在这里插入图片描述
另外注意 在setting.gradleTo remove the pairbuildSrc的配置,无需配置,如下图所示:
在这里插入图片描述

下面在src/main/The package file is below,创建Java插件类Plugin2Dmeo,实现如下:

package com.xiaomi.app;

import org.gradle.api.Plugin;
import org.gradle.api.Project;

public class Plugin2Demo implements Plugin<Project> {
    
    @Override
    public void apply(Project project) {
    
        System.out.println("The second way to implement plugins");
    }

}

The next step is to configure the manifest file before it can be used,创建步骤如下:
在main下面创建resources/META-INF/gradle-plugins/com.xiaomi.plugin.properties文件,结构如下:
在这里插入图片描述

说明一下com.xiaomi.plugin.properties文件的含义
其中com.xiaomi.pluginIt is the name of the plugin to be introduced in our project,The name of the plugin can be chosen arbitrarily.
上面的.propertiesWhat should be filled in the content of the file
In fact, it is the plug-in class found from the entrance;内容如下:

implementation-class = com.xiaomi.app.Plugin2Demo

里面内容implementation-class,The configuration inside is the class name of the plugin,截图如下:
在这里插入图片描述
完成上面的步骤,We are just one step away from using the plugin!
在app.gradleImport the above plugins into the module,就可以:

apply plugin:'com.xiaomi.plugin'//这里引用的就是properties文件的名称

在这里插入图片描述
The name in the light blue box here isproperties的名称;
运行gradlew,It is found that the plug-in of the second method has been implemented;
在这里插入图片描述
怎么样学会了吗?The third method is implemented below.

3、独立项目(推荐,有用,This is how plugins are written)

Plug-ins implemented in this way can be published locally,jcenter仓库或者Maven仓库,Or your own company's code warehouse;
Its implementation process is similar to the second way,Only some steps are different,It requires you to publish;
The general process is divided into custom plug-ins and using plug-ins,The process of customizing the plugin is first:

自定义插件

一、创建一个java lib库 Feel free to go with a name yourself,不多说了,Similar to the above process
在这里插入图片描述
二,在这个插件的build.gradleAdd the following dependencies as wellclass类中实现Plugin接口
build.gradle,This file needs to manually add the dependencies of the plugin,This is different from the second one above

apply plugin: 'groovy'
dependencies {
    
    implementation gradleApi()
}

javaThe class implementation is similar to the above

package com.huawei.plugin;

import org.gradle.api.Plugin;
import org.gradle.api.Project;

public class MyPlugin implements Plugin<Project> {
    
    @Override
    public void apply(Project project) {
    
        System.out.println("The third way is to implement pluginsmy plugin");
    }
}

三、配置清单文件
The implementation of the manifest file is also similar to the second step above;
在main文件夹下面新建resources/META-INF/gradle-plugins,After building these three folders,We're going to build a suffix of .properties的文件,You can choose any name you want;此处是com.huawei.plugin.properties
在这里插入图片描述
The configuration in this file is:

implementation-class = com.huawei.plugin.MyPlugin

四,发布到仓库(Get your local repository here,Do not get the remote warehouse)
在插件的build.gradle添加如下脚本,A post is required here,后面才可以使用.

apply plugin: 'maven-publish'

publishing {
    
    publications {
    
        MyPlugin(MavenPublication) {
    
            from components.java
            groupId 'com.huawei'
            artifactId 'plugin'
            version '1.0.0'
        }
    }
}

单击 rebuild,进行编译,Then click on the right menu bar to openGadle
在这里插入图片描述
双击3的位置,Publish locally,After this is done, the plugin can be used;

使用插件

使用方式如下:
1.在主工程的build.gradle文件中添加如下:

buildscript {
    
    repositories {
    
        google()
        jcenter()
        mavenLocal() //默认路径在C:/Users/用户名/.m2/repository下面,(本机电脑)
    }
    dependencies {
    
        classpath "com.android.tools.build:gradle:4.1.1"
        classpath 'com.huawei:plugin:1.0.0'//引入插件
    }
}

2.在app项目的build.gradle中添加

apply plugin: 'com.huawei.plugin'

This is the third way to configure plug-insOK了!
我运行gradlew之后,The plugin works normally;
在这里插入图片描述
The above are the three ways to use the simple version of the plug-in summarized by me,It was good to see it in the end,Excuse me, bosses,动动小手,给个赞吧!

动动小手,点个赞再走

Additional extension classes for plugins,I will not expand here,afraid of making too much,Everyone sees it in a cloud of fog,需要的话,可以自己去扩展,此处就不展开了!

copyright notice
author[Someone from Yungu Li],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2023/025/202301251120395800.html

Random recommended