個人博客
自定義Gradle插件
自定義Gradle插件可以分三種
-
定義在具體的Module對應的gradle文件里。這種方式定義的插件只能在定義的Module中使用
-
定義名為BuildSrc的Module,在BuildSrc里定義。這種方式定義的插件只能在同一個工程中使用。
-
定義其它名字的Module,在新定義的Module里定義插件。這種方式定義的插件可以給第三方使用。
下面針對這三種方式,以開發一個可以讓用戶自定義配置的插件為例,分別來展開具體的定義插件過程
定義在Gradle文件里
定義
在app模塊下的gradle文件中,增加定義
class CustomInnerConfig {
String key
}
//定義extension
extensions.create("customInnerConfig", CustomInnerConfig.class)
task("CustomInnerTask", group: 'CustomTask').doLast {
println("CustomInnerTask,key=${customInnerConfig.key}")
}
同步項目后,可以在Android Studio右側的gradle面板中看到新增加的插件
使用
自定義配置
customInnerConfig {
key = 'inner key'
}
在gralde面板中,找到CustomInnerTask,雙擊運行
輸出結果如下,可以看到自定義的配置已經生效:
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文件如下:
apply plugin: 'groovy'
dependencies {
implementation gradleApi()
implementation localGroovy()
}
同步后,新建src/main目錄
在main目錄下新建groovy文件夾
然后在groovy文件夾下新建目錄:com/wangyz/local
在local目錄下新建CustomLocalConfig.java文件,內容如下:
package com.wangyz.local;
public class CustomLocalConfig {
String key;
}
在local目錄下新建LocalPlugin.groovy文件,內容如下:
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文件中引入剛才定義的插件
apply plugin: com.wangyz.local.LocalPlugin
同步后,gradle面板如下
自定義配置
customLocalConfig{
key = 'local key'
}
雙擊CustomLocalTask,執行結果如下
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文件如下:
apply plugin: 'groovy'
dependencies {
implementation gradleApi()
implementation localGroovy()
}
在main目錄下新建groovy文件夾
然后在groovy文件夾下新建目錄:com/wangyz/plugins
CustomConfig.java文件,內容如下:
package com.wangyz.plugins;
public class CustomConfig {
String key;
}
在plugins目錄下新建CustomPlugin.groovy文件,內容如下:
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的文件,文件名即為第三方引用的插件名稱。文件內容為:
implementation-class=com.wangyz.plugins.CustomPlugin
在這個文件里配置了之前定義的CustomPlugin的路徑
發布插件
在Plugin模塊的build.gradle文件中增加以下配置
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文件中增加
repositories {
//...
maven{
url uri('D:\\Repository')
}
}
dependencies {
//...
classpath 'com.wangyz.plugins:CustomPlugin:1.0.0'
}
在需要引入的模塊的build.gradle中增加
apply plugin: 'com.wangyz.plugins.CustomPlugin'
customConfig{
key = 'custom key'
}
同步后,在gradle面板雙擊CustomTask運行
輸出結果:
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