Steps for developing a plug-in

  1. Create a New Android Library module and delete itsrcIn the directoryandroidTest.testDirectory.
  2. insrc/mainCreate a new directorygroovyTable of contents, used to put relevantgroovyfile
  3. insrc/main/groovyCreate a new directory under the directorycom.fred.asmNote that this is a directory namedcom.fred.asmInstead of building onecom/fred/asmDirectory)
  4. insrc/main/groovy/com.fred.asm/Directory, create a new class, this class is our plug-in entry, my initial idea was to write a plug-in to count the execution time of the method, so I wrote a classMethodStatPlugin.groovy
public class MethodStatPlugin implements Plugin<Project> {

    @Override
    void apply(Project target) {
        println "plugin execute"; }}Copy the code
  1. insrc/mainCreate a new one under the directoryresourcesdirectory
  2. insrc/main/resources/Create a new directoryMETA-INFIn theMETA-INFLet’s create another directorygradle-plugins
  3. Once the above steps are complete, there you have itsrc/main/resources/META-INF/gradle-pluginsDirectory, and then under this directory, we’ll create aperformance.methodstat.propertiesfile
  4. inperformance.methodstat.propertiesAdd the following code to the file
implementation-class=com.fred.asm.MethodStatPlugin
Copy the code
  1. inbuild.gradleAdd the following code to the file:
apply plugin: 'groovy' apply plugin: 'maven' dependencies { implementation fileTree(dir: 'libs', include: [' *. Jar ']) implementation gradleApi () implementation localGroovy () implementation 'com. Android. View the build: gradle: 3.4.2' Implementation 'org.ow2.asm:asm:7.1'} group='com.fred.asm. Plugin ' Version ='1.0.0' uploadArchives {repositories {mavenDeployer {// Repository (url: uri('.. /asm_plugin_repo')) } } }Copy the code

This completes our plug-in Module, which looks like this:

Need to pay attention to

  • In the pictureMETA-INF.gradle-pluginsIs the display effect after Android Studio folds, and its corresponding physical path isMETA-INF/gradle-plugins.performance.methodstat.propertiesThe document must be in thesrc/main/resources/META-INF/gradle-pluginsDirectory.

At the same time, we double-click to execute uploadArchivestask on the right side of Android Studio as follows:

A local repository will be generated and the jar packages will be placed in this directory.

The use of plug-in

In the build.gradle file of our application, we need to introduce this plugin:

  1. Setting up a dependency library
Buildscript {repositories {Google () jCenter () // Custom plugin maven {url '.. /asm_plugin_repo'} dependencies {classpath 'com.fred.asm. Plugin :asm-tool:1.0.0'} }Copy the code
  1. Add the plug-inapply plugin: 'performance.methodcost'

Remember when we plug-in development above have a SRC/main/resources/meta-inf/gradle – a performance under the plugins directory. Methodstat. Not the properties file, apply here the plugin: {pluginId}, {pluginId} is and the performance. The above methodstat. Corresponding to the properties file.

3. When we compile an application like assembleDebug, we can see that the console prints plugin execute

Let’s do it again in Java

We know that Groovy is also the language for JVM classes, and it can do all the things that Java can do, so let’s write it in Java once. Same thing:

  1. Create a New Android Library module and delete itsrcIn the directoryandroidTest.testDirectory.
  2. insrc/main/javaCreate a new packagecom.fred.asmNote that this is the Java package structure, corresponding to the physical directory issrc/main/java/com/fred/asm
  3. insrc/main/java/com/fred/asmNext, create a new Java classMethodStatPlugin.javaAnd ingroovyI’ll write it the same way, so that oursrc/main/resources/META-INF/gradle-pluginsAnd the pluginId can remain the same.
  4. MethodStatPlugin.java, write the following code:
public class MethodStatPlugin implements Plugin<Project> {
    @Override
    public void apply(Project target) {
        System.out.println("plugin execute [write by java]"); }}Copy the code
  1. The same withuploadArchivesCompile, package, and upload plug-ins to a local repository
  2. Recompile the module that uses the plug-in, and you can see the console outputplugin execute [write by java]The plug-in is successfully applied

usingbuildSrcSimplify the development

So what is buildSrc? When we need to write some plug-ins, we can create a buildSrc Module in Android Studio. We can only have one module per Android project.

  • When Gradle builds a module, it builds it firstbuildSrcThis project is configured to the classpath
  • ifbuildSrcGradle is a plugin project, Gradle automatically configures the Gradle plugin in the project directlyapplyA plug-in can be developed.

Development steps

  1. Create a New Java type Module with Android Studio and name itbuildSrc(The name must be this)
  2. Modify thebuild.gradleAdd related dependencies as follows:
apply plugin: 'java-library'

repositories {
    maven { url 'https://jitpack.io' }

    maven {
        url 'http://maven.aliyun.com/nexus/content/groups/public/'
    }
    google()
    jcenter()
}

dependencies {
    // Add gradle version
    implementation 'com. Android. Tools. Build: gradle: 3.4.2'
    // Add the gradle API
    implementation gradleApi()
}
Copy the code
  1. Writing a plug-in
public class MyPlugin implements Plugin<Project> {
    @Override
    public void apply(Project target) {
        System.out.println("[App Log]-------------->MyPlugin"); }}Copy the code
  1. Plug-in configuration

  1. The reference added in the place where the plug-in is needed
apply plugin: 'com.fred.plugin'
Copy the code

So far the development of Gradle plug-in has been introduced, the follow-up is to be familiar with the relevant API, according to their own project needs, do relevant business development.

reference

  • Docs.gradle.org/current/use…
  • Blog.csdn.net/Notzuonotdi…