preface

Recently, I have been studying three methods of compiling pegs: AspectJ, ASM, and ASM in Redex. To use ASM, you need to use it as a plug-in in your project. Therefore, the purpose of this article is to understand and define a Gradle plug-in.

There are a lot of such articles on the net now, we can look at them in combination.

Gradle plug-ins package reusable build logic that can be used in many different projects and builds.

You can implement a custom Gradle plugin in your favorite coding language, which will eventually compile city JVM bytecode. Plugins can now be implemented in Groovy, Java, Kotlin, and so on. In general, plug-ins implemented in Java or Kotlin will outperform their groovy counterparts.

There are three ways to implement plug-ins, which I will describe in the following sections.

implementation

Build Script

You can build some simple plug-in code directly in build.gradle, which is automatically compiled and included in the classpath of your build script without doing anything. However, this plug-in can only be used in this project and is not visible externally.

class GreetingExtension { String message = null } class GreetingPlugin implements Plugin<Project> { @Override void Def Extension = target.extensions.create() def Extension = target.extensions.create("greeting", GreetingExtension)

        target.task("customPlugin") {
            doLast() {println(extension.message)}}}} /** * Apply plugin executes the apply method for binding * */ apply plugin: GreetingPlugin greeting {message ="Hi this is a cutstom plugin"
}
Copy the code

We can find the customPlugin in our app using the command line or gradle to the right of AS

./gradlew -p app customPlugin --stacktrace
Copy the code

Take a look at the result:

> Task :app:customPlugin
Hi this is a cutstom plugin

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

Copy the code

buildSrc project

Put rootProjectDir plug-in source/buildScr/SCR/main/groovy, the advantage of automatic compilation project directory buildSrc plugin source code, do not need to manually specify the classpath. Other modules of the same project can also be used. The downside is that it is not available for other projects. The third way solves this shortcoming.

And if you want to know about it, you can see it here

The third is similar to this, except that it is a separate plug-in project that can be used by other projects.

Standalone project

This is the way most of them are used now.

Create the Java Library Module customPlugin in Android Studio. Using IJ to develop plug-ins is much more convenient than using AS.

There are three steps to developing plug-ins in AS:

  1. Remove the Java file.
  2. Add the Groovy folder where all the plug-in code is stored.
  3. Add the Resources folder, which is used to declare the plug-in pointing address.
  4. Modify the build.gradle file
apply plugin: 'groovy'
apply plugin: 'maven'

repositories {
    mavenCentral()
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation gradleApi() // gradler sdk
    implementation localGroovy() // Groovy SDK} uploadArchives {repositories {mavenDeployer {// Set the plugin GAV parameter pop.groupid ='com.lenny.plugin'
            pom.artifactId = 'art'/ / this if not set, after the release should be com. Lenny. Plugin. CustomPlugin pom, version ='1.0.3'// Publish the file to the following directory: repository(url: uri('.. /repo'))}}}Copy the code

The modified directory should look like this:

├ ─ ─ build. Gradle ├ ─ ─ libs ├ ─ ─ customPlugin. On iml └ ─ ─ the SRC └ ─ ─ the main ├ ─ ─ groovy │ └ ─ ─ com │ └ ─ ─ lenny │ └ ─ ─ the plugin │ ├ ─ ─ CustomPlugin. Groovy │ └ ─ ─ resources └ ─ ─ meta-inf └ ─ ─ gradle - plugins └ ─ ─ com. Lenny. Plugin. PropertiesCopy the code

CustomPlugin.groovy

package com.lenny.plugin import org.gradle.api.Plugin import org.gradle.api.Project public class CustomPlugin implements  Plugin<Project> { @Override void apply(Project project) { println("hello from custom plugin")}}Copy the code

Note the suffix of the file.

com.lenny.plugin.properties

implementation-class=com.lenny.plugin.CustomPlugin
Copy the code

The uploadArchives command can be found in Gradle on the right. After executing this command, you will find the uploadArchives command in your project’s sibling directory:

└ ─ ─ repo └ ─ ─ com ├ ─ ─ lenny │ └ ─ ─ the plugin │ └ ─ ─ art │ └ ─ ─ 1.0.3 │ ├ ─ ─ art - 1.0.3. Jar │ ├ ─ ─ art - 1.0.3. Jar. The md5 │ ├ ─ ─ Art - 1.0.3. Jar. Sha1 │ ├ ─ ─ art - 1.0.3. Pom │ ├ ─ ─ art - 1.0.3. Pom. Md5 │ ├ ─ ─ art - 1.0.3. Pom. Sha1Copy the code

Then we integrate in the project:

Project build.gradle

buildscript {
    repositories {
        ...
        maven {
            url uri('repo/')
        }
    }
    dependencies {
        ...
        classpath 'com. Lenny. Plugin: art: 1.0.3'}}Copy the code

The build of the app. Gradle

apply plugin: 'com.lenny.plugin'
Copy the code

If you’re repositoies, you don’t have to do that.

The origin of the classpath XXX: groupId: artifactId: version

Apply Plugin: ‘XXX’ is preceded by properties: com.lenny.plugin

Execution Result:

> Configure project :app
hello from custom plugin

> Task :app:createMockableJar UP-TO-DATE
> Task :app:preBuild UP-TO-DATE
Copy the code

conclusion

Customizing Gradle is a good way to implement gradle. For more advanced gradle applications, see the official documentation.

Thank you for seeing this.

The resources

Develop custom Gradle plug-ins