为Android Studio编写自定义Gradle插件的教程
Google已经建议Android开发全部转向Android Studio开发,Android Studio 是使用gradle编译、打包的,那么问题来了,gradle可是有一堆东西...,为了彻底了解gradle,今天就来学习下如何写自己的gradle插件(当然插件源码是使用groovy写的),先看如下代码目录:
如上图所示,plugin目录是插件源码目录,sample是用来测试插件的。
1、在目录plugin/class="lazy" data-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> {
void apply(T target);
}
2、在目录plugin/class="lazy" data-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\class="lazy" data-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\class="lazy" data-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\class="lazy" data-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\class="lazy" data-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
您可能感兴趣的文章:Android gradle插件打印时间戳的方法详解详解Android Gradle插件3.0挖坑日记详解如何使用Android Studio开发Gradle插件Android Studio中的Gradle依赖深入讲解android studio3.0.1无法启动Gradle守护进程的解决方法Android studio gradle环境变量配置教程Mac中配置gradle环境及使用android studio打包jar包与arr包的方法unity5.6 导出gradle工程 Android Studio 导入问题及处理方法详解AndroidStudio JNI +Gradle3.0以上JNI爬坑之旅Android Studio Gradle插件版本与Gradle版本之间的对应关系
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341