为Android Studio编写自定义Gradle插件的教程

时间:2021-05-19

Google已经建议Android开发全部转向Android Studio开发,Android Studio 是使用gradle编译、打包的,那么问题来了,gradle可是有一堆东西...,为了彻底了解gradle,今天就来学习下如何写自己的gradle插件(当然插件源码是使用groovy写的),先看如下代码目录:

如上图所示,plugin目录是插件源码目录,sample是用来测试插件的。


1、在目录plugin/src/main/groovy/com/micky/gradle/下新建插件类MyCustomPlugin.groovy

package com.micky.gradle; import org.gradle.api.*; class MyCustomPlugin implements Plugin<Project> { void apply(Project project) { project.task('myTask') << { println "Hi this is micky's plugin" } } }

看看Plugin的源码,其实就是一接口

public interface Plugin<T> { /** * Apply this plugin to the given target object. * * @param target The target object */ void apply(T target); }


2、在目录plugin/src/main/resources/META-INF/gradle-plugins/下创建文件com.micky.mycustom.properties用来指定插件实现类

implementation-class=com.micky.gradle.MyCustomPlugin

特别注意下:文件名“com.micky.mycustom”即是以后我们在使用插件时的apply plugin 'java' 的java,这里我也是折腾了半天才得出的结果,坑啊。


3、一般情况下,我们还需要指定插件项目名称,在plugin目录下新建settings.gradle

rootProject.name='gradle-micky'

4、万事具备,就差编译了,编译需要在plugin目录下新建build.gradle

apply plugin: 'groovy' apply plugin: 'maven' dependencies { compile gradleApi() compile localGroovy() } repositories { mavenCentral() } group='com.micky' version='1.0.0' uploadArchives { repositories { mavenDeployer { repository(url: uri('../repo')) } } }

在这个脚本里使用groovy插件编译groovy源码,声明gradleAPI作为即时编译依赖,apply plugin: 'maven' 是用来创建一个插件jar文件并且存储在本地maven库里,本地maven库即我们在脚本里创建的"../repo"目录
执行命令:

gradle uploadArchives


5、以上4个步骤已经编译插件并上传到了本地库中,接下来就看看怎么使用插件,在sample目录下,新建build.gradle

buildscript { repositories { maven { url uri('../repo') } } dependencies { classpath group: 'com.micky', name: 'gradle-micky', version: '1.0.0' } } apply plugin: 'com.micky.mycustom'

6、执行命令

myTask即我们在MyCustomPlugin.groovy代码中创建的任务。

7、自定义Task

(1)copy一份改名CustomPluginTask

在plugin\src\main\groovy\com\micky\gradle目录创建源文件 MyCustomTask.groovy

package com.micky.gradle; import org.gradle.api.DefaultTask import org.gradle.api.tasks.TaskAction class MyCustomTask extends DefaultTask { @TaskAction void output() { println "Hello this is my custom task output" } }

(2)修改MyCustomPlugin.groovy

package com.micky.gradle; import org.gradle.api.*; class MyCustomPlugin implements Plugin<Project> { void apply(Project project) { project.task('customTask', type: MyCustomTask) } }

(3)修改plugin目录下的build.gradle,修改版本号

apply plugin: 'groovy' apply plugin: 'maven' dependencies { compile gradleApi() compile localGroovy() } repositories { mavenCentral() } group='com.micky' version='1.0.1' uploadArchives { repositories { mavenDeployer { repository(url: uri('../repo')) } } }

执行gradle uploadArchives 编译插件包

(4)sample目录下的build.gradle

buildscript { repositories { maven { url uri('../repo') } } dependencies { classpath group: 'com.micky', name: 'gradle-micky', version: '1.0.1' } } apply plugin: 'com.micky.mycustom'

执行gradle customTask 结果如下:


8、向Plugin Task 传递参数
(1)拷贝一份上面的代码,改名为CustomPluginTaskWithParam,修改plugin\src\main\groovy\com\micky\gradle\MyCustomPlugin.groovy

package com.micky.gradle; import org.gradle.api.*; class MyCustomPluginExtension { def message = "From MyCustomPluginExtention" def sender = "MyCustomPluin" } class MyCustomPlugin implements Plugin<Project> { void apply(Project project) { project.extensions.create('myArgs', MyCustomPluginExtension) project.task('customTask', type: MyCustomTask) } }

(2)修改plugin\src\main\groovy\com\micky\gradle\MyCustomTask.groovy

package com.micky.gradle; import org.gradle.api.DefaultTask import org.gradle.api.tasks.TaskAction class MyCustomTask extends DefaultTask { @TaskAction void output() { println "Sender is ${project.myArgs.sender},\nmessage: ${project.myArgs.message}" } }


(3)修改plugin/build.gradle

apply plugin: 'groovy' apply plugin: 'maven' dependencies { compile gradleApi() compile localGroovy() } repositories { mavenCentral() } group='com.micky' version='1.0.2' uploadArchives { repositories { mavenDeployer { repository(url: uri('../repo')) } } }

执行gradle uploadArchives 编译插件包

(4)修改sample/build.gradle

buildscript { repositories { maven { url uri('../repo') } } dependencies { classpath group: 'com.micky', name: 'gradle-micky', version: '1.0.2' } } apply plugin: 'com.micky.mycustom'

(5)执行gradle customTask,结果如下:

(6)在gradle文件配置参数

buildscript { repositories { maven { url uri('../repo') } } dependencies { classpath group: 'com.micky', name: 'gradle-micky', version: '1.0.2' } } apply plugin: 'com.micky.mycustom' myArgs { sender='Micky Liu' message='Gradle is so simple.' }


(7)执行gradle customTask,结果如下:

8、向Plugin Task 传递嵌套试复杂参数
(1)拷贝一份上面的代码,改名为CustomPluginTaskWithNestParam,修改plugin\src\main\groovy\com\micky\gradle\MyCustomPlugin.groovy

package com.micky.gradle; import org.gradle.api.*; class MyNestPluginExtension { def receiver = "Kate Zhou" def email = "KateZhou@gmail.com" } class MyCustomPluginExtension { def message = "From MyCustomPluginExtention" def sender = "MyCustomPluin" } class MyCustomPlugin implements Plugin<Project> { void apply(Project project) { project.extensions.create('myArgs', MyCustomPluginExtension) project.myArgs.extensions.create('nestArgs', MyNestPluginExtension) project.task('customTask', type: MyCustomTask) } }

(2)修改MyCustomPlugin.groovy

package com.micky.gradle; import org.gradle.api.DefaultTask import org.gradle.api.tasks.TaskAction class MyCustomTask extends DefaultTask { @TaskAction void output() { println "Sender is ${project.myArgs.sender} \nmessage: ${project.myArgs.message}" println "Receiver is ${project.myArgs.nestArgs.receiver} \nemail: ${project.myArgs.nestArgs.email}" } }

(3)修改plugin/build.gradle

apply plugin: 'groovy' apply plugin: 'maven' dependencies { compile gradleApi() compile localGroovy() } repositories { mavenCentral() } group='com.micky' version='1.0.3' uploadArchives { repositories { mavenDeployer { repository(url: uri('../repo')) } } }

(4)执行gradle uploadArchives 编译插件包
(5)修改sample/build.gradle

buildscript { repositories { maven { url uri('../repo') } } dependencies { classpath group: 'com.micky', name: 'gradle-micky', version: '1.0.3' } } apply plugin: 'com.micky.mycustom' myArgs { sender='Micky Liu' message='Gradle is so simple.' }


(6)执行执行gradle customTask,结果如下:

(7)在gradle文件配置参数

buildscript { repositories { maven { url uri('../repo') } } dependencies { classpath group: 'com.micky', name: 'gradle-micky', version: '1.0.3' } } apply plugin: 'com.micky.mycustom' myArgs { sender='Micky Liu' message='Gradle is so simple.' nestArgs { receiver='David Chen' email='David@126.com' } }

(8)执行gradle customTask,结果如下:

源码地址:https://github.com/mickyliu945/GradleCustomPlugin

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章