个人博客

http://www.milovetingting.cn

自定义Gradle插件

自定义Gradle插件可以分三种

  1. 定义在具体的Module对应的gradle文件里。这种方式定义的插件只能在定义的Module中使用

  2. 定义名为BuildSrc的Module,在BuildSrc里定义。这种方式定义的插件只能在同一个工程中使用。

  3. 定义其它名字的Module,在新定义的Module里定义插件。这种方式定义的插件可以给第三方使用。

下面针对这三种方式,以开发一个可以让用户自定义配置的插件为例,分别来展开具体的定义插件过程

定义在Gradle文件里

定义

在app模块下的gradle文件中,增加定义

1
2
3
4
5
6
7
8
9
10
class CustomInnerConfig {
String key
}

//定义extension
extensions.create("customInnerConfig", CustomInnerConfig.class)

task("CustomInnerTask", group: 'CustomTask').doLast {
println("CustomInnerTask,key=${customInnerConfig.key}")
}

同步项目后,可以在Android Studio右侧的gradle面板中看到新增加的插件

定义在gradle文件

使用

自定义配置

1
2
3
customInnerConfig {
key = 'inner key'
}

在gralde面板中,找到CustomInnerTask,双击运行

输出结果如下,可以看到自定义的配置已经生效:

1
2
3
4
5
6
7
Parallel execution with configuration on demand is an incubating feature.
:app:CustomInnerTask
CustomInnerTask,key=inner key

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
10:56:44: Task execution finished 'CustomInnerTask'.

在BuildSrc模块中定义

定义

新建名为BuildSrc的模块,删除BuildSrc模块下其它文件,只保留build.gradle。配置build.gradle文件如下:

1
2
3
4
5
6
apply plugin: 'groovy'

dependencies {
implementation gradleApi()
implementation localGroovy()
}

同步后,新建src/main目录

在main目录下新建groovy文件夹

然后在groovy文件夹下新建目录:com/wangyz/local

在local目录下新建CustomLocalConfig.java文件,内容如下:

1
2
3
4
5
6
package com.wangyz.local;

public class CustomLocalConfig {
String key;
}

在local目录下新建LocalPlugin.groovy文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.wangyz.local

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

class LocalPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
project.extensions.create("setting", Setting)

project.task('CustomLocalTask', group: 'Custom').doLast {
println "自定义LocalTask,versionName:${project.setting.versionName}"
}
}
}

使用

在app模块下的gradle文件中引入刚才定义的插件

1
apply plugin: com.wangyz.local.LocalPlugin

同步后,gradle面板如下

定义在BuildSrc

自定义配置

1
2
3
customLocalConfig{
key = 'local key'
}

双击CustomLocalTask,执行结果如下

1
2
3
4
5
CustomLocalTask,key=local key

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
11:20:47: Task execution finished 'CustomLocalTask'.

定义可供第三方使用的插件

定义

新建名为Plugins的模块,删除Plugins模块下src/main其它文件。配置build.gradle文件如下:

1
2
3
4
5
6
apply plugin: 'groovy'

dependencies {
implementation gradleApi()
implementation localGroovy()
}

在main目录下新建groovy文件夹

然后在groovy文件夹下新建目录:com/wangyz/plugins

CustomConfig.java文件,内容如下:

1
2
3
4
5
6
7
package com.wangyz.plugins;

public class CustomConfig {

String key;

}

在plugins目录下新建CustomPlugin.groovy文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.wangyz.plugins

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

class CustomPlugin implements Plugin<Project> {
@Override
void apply(Project project){
project.extensions.create("customConfig", CustomConfig)

project.task('CustomTask', group: 'CustomTask').doLast {
println "CustomTask,key=${project.customConfig.key}"
}
}
}

在Plugins模块的src/main目录下新建resources/META-INF/gradle-plugins目录,在目录下新建名为com.wangyz.plugins.CustomPlugin.properties的文件,文件名即为第三方引用的插件名称。文件内容为:

1
implementation-class=com.wangyz.plugins.CustomPlugin

在这个文件里配置了之前定义的CustomPlugin的路径

发布插件

在Plugin模块的build.gradle文件中增加以下配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
apply plugin: 'maven-publish'

publishing{
publications{
mavenJava(MavenPublication){
groupId 'com.wangyzs.plugin'
artifactId 'CustomPlugin'
version '1.0.0'

from components.java
}
}
}

publishing{
repositories {
maven{
url uri('D:\\Repository')
}
}
}

同步后,在Terminal面板执行gradle publish命令,然后在D:\Repository下可以找到生成的插件

引入插件

在工程的build.gradle文件中增加

1
2
3
4
5
6
7
8
9
10
repositories {
//...
maven{
url uri('D:\\Repository')
}
}
dependencies {
//...
classpath 'com.wangyz.plugins:CustomPlugin:1.0.0'
}

在需要引入的模块的build.gradle中增加

1
2
3
4
5
apply plugin: 'com.wangyz.plugins.CustomPlugin'

customConfig{
key = 'custom key'
}

同步后,在gradle面板双击CustomTask运行

第三方使用的插件

输出结果:

1
2
3
4
5
CustomTask,key=custom key

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
13:02:48: Task execution finished 'CustomTask'.

源码地址:https://github.com/milovetingting/Samples/tree/master/GradlePlugin