A tutorial for writing custom Gradle plug-ins for Android Studio _android

Source: Internet
Author: User

Google has suggested that Android development all turn to Android Studio development, and that Android Studio is compiled and packaged using Gradle, so the problem is, Gradle has a bunch of things ... and in order to get a thorough understanding of Gradle, Today to learn how to write your own Gradle plug-ins (of course, the plug-in source code is written in groovy), first look at the following code directory:

As shown in the figure above, the plugin directory is the plug-in source directory, sample is used to test plug-ins.


1, under the Directory plugin/src/main/groovy/com/micky/gradle/new plug-in class 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 '}}} 
 

Look at the source of plugin, in fact, is an interface

Public interface Plugin<t> { 
  /** 
   * Apply This Plugin to the given target object. 
   * 
   @param target the target object/ 
  void apply (T target); 
 


2, in the directory plugin/src/main/resources/meta-inf/gradle-plugins/create a file com.micky.mycustom.properties to specify the plug-in implementation class

Implementation-class=com.micky.gradle.mycustomplugin 

Special attention: The filename "Com.micky.mycustom" That is later we use Plug-ins when the application plugin ' Java ' Java, where I have been tossing a half-day to come to the results, pit AH.


3, under normal circumstances, we also need to specify the plug-in project name, in the plugin directory under the new Settings.gradle

Rootproject.name= ' Gradle-micky ' 

4, everything has, on the difference compiled, the compiler needs to be in the plugin directory under the new 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 (' .... /repo ')}}} 
 

In this script, using the groovy plugin to compile the groovy source code, declaring Gradleapi as Just-in-time compilation dependency, apply plugin: ' maven ' is used to create a plugin jar file and store it in the local MAVEN library, The local MAVEN library is what we created in the script. /repo "Directory
To execute a command:

Gradle uploadarchives


5, the above 4 steps have been compiled Plug-ins and uploaded to the local library, next to see how to use Plug-ins, in the sample directory, the new 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. Execute the Order

MyTask is the task that we created in the Mycustomplugin.groovy code.

7. Custom Task

(1) Copy a name Customplugintask

Create a source file in the Plugin\src\main\groovy\com\micky\gradle directory 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 are my custom task output" 
  } 
} 

(2) Modify 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) Modify the Build.gradle in the plugin directory, modify the version number

Apply plugin: ' Groovy ' 
Apply plugin: ' maven ' 
 
dependencies { 
  compile gradleapi () 
  compile Localgroovy () 
} 
 
repositories { 
  mavencentral () 
} 
 
group= ' Com.micky 
' version= ' 1.0.1 ' 
 
uploadarchives { 
  repositories { 
    Mavendeployer { 
      repository ('. /repo ')}}} 
 

Execute Gradle uploadarchives Compile the plug-in package

(4) Build.gradle under the catalogue of sample

Buildscript { 
  repositories { 
    maven { 
      URL uri ('.. /repo ') 
    } 
  } 
 
  dependencies { 
    classpath group: ' Com.micky ', 
      name: ' Gradle-micky ', 
      version: ' 1.0.1 ' 
  } 
Apply plugin: ' Com.micky.mycustom ' 

The results of the implementation of Gradle CustomTask are as follows:


8. Pass parameters to plugin Task
(1) A copy of the above code, renamed Customplugintaskwithparam, modify 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) Modify 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) Modify 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 ('. /repo ')}}} 
 

Execute Gradle uploadarchives Compile the plug-in package

(4) Modify 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) Implementation of Gradle CustomTask, the results are as follows:

(6) Configuring parameters in Gradle files

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) Implementation of Gradle CustomTask, the results are as follows:

8. Pass nested test complex parameters to plugin Task
(1) A copy of the above code, renamed Customplugintaskwithnestparam, modify 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) Modify 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) Modify 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 ('. /repo ')}}} 
 

(4) Execute Gradle uploadarchives compile the plug-in package
(5) Modify 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) Implementation of the implementation of Gradle CustomTask, the results are as follows:

(7) Configuring parameters in Gradle files

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) Implementation of Gradle CustomTask, the results are as follows:

Source Address: Https://github.com/mickyliu945/GradleCustomPlugin

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.