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

2019/7/7 19:56:28

本文主要是介绍为Android Studio编写自定义Gradle插件的教程,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

2016224145421168.png (326×201)

如上图所示,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


2016224145611491.png (559×192)

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、执行命令

2016224145636115.png (507×113)

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 结果如下:

2016224145747922.png (563×125)


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,结果如下:

2016224145851154.png (558×129)

(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,结果如下:

2016224145926554.png (561×131)

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,结果如下:

2016224150016512.png (561×156)

(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,结果如下:

2016224150049182.png (563×154)

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



这篇关于为Android Studio编写自定义Gradle插件的教程的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程