欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-02-25 20:43:57
google已经建议android开发全部转向android studio开发,android studio 是使用gradle编译、打包的,那么问题来了,gradle可是...

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

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

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


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

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

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

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

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


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

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

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

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

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

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

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

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

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