Gradle is a very powerful open source tool for building automated projects. Gradle plug-ins are developed based on Groovy language. This article will introduce how to develop Gradle plug-ins.

Creating a Groovy project

Create a normal Java project and modify build.gradle

apply plugin: 'groovy'
apply plugin: 'maven'

sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 dependencies { compile gradleApi()  compilelocalGroovy()
    implementation  'com. Android. Tools. Build: gradle: 2.3.3'
    implementation  'org. Jetbrains. Kotlin: kotlin - gradle - plugin: 1.3.31'
}
repositories {
    jcenter()
    google()
    mavenCentral()
}
group = 'com.grouter'
version = '1.0.0'UploadArchives {repositories {mavenDeployer {repository(url: uri)'.. /repo'))}}}Copy the code

Groovy code router – gradle/SRC/main/Groovy/grouter GRouterPlugin. Groovy

// GRouterPlugin.groovy
package grouter

import org.gradle.api.Plugin
import org.gradle.api.Project
class GRouterPlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        project.tasks.create("GRouterCompile").doLast {
            // do something
        }
    }
}
Copy the code

Write GRouter. The properties file router – gradle/SRC/main/resources/meta-inf/gradle – plugins/GRouter properties

implementation-class=grouter.GRouterPlugin
Copy the code
Publish to local

Run the uploadArchives command to publish the current Module locally

The introduction of the plugin

Introduce local repositories and add plug-ins to Android projects

buildscript {
    repositories {
        jcenter()
        maven {
            url uri('.. /repo')
        }
    }
    dependencies {
        classpath 'com. Grouter: the router - gradle: 1.0.0'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'GRouter'
Copy the code

Looking at the Gradle panel, you can see that a GRouterCompile Task has been generated

Task

Variants

There are at least two modes in Android development, Debug and Release. When we create tasks in Android development, we basically create a Task for each mode, such as GRouterCompileDebug and GRouterCompileRelease. Android modules will also feature application and Library Variants.

package router
import com.android.build.gradle.AppPlugin
import com.android.build.gradle.LibraryPlugin
import org.gradle.api.Plugin
import org.gradle.api.Project

class GRouterPlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        def hasApp = project.plugins.withType(AppPlugin)
        def hasLib = project.plugins.withType(LibraryPlugin)
        if(! hasApp && ! hasLib) { throw new IllegalStateException("'android' or 'android-library' plugin required.")
        }
        final def variants
        if (hasApp) {
            variants = project.android.applicationVariants
        } else{ variants = project.android.libraryVariants } variants.all { variant -> String variantName = variant.name.capitalize() // Debug, Release println("variantName $variantName")}}}Copy the code
Create a Task
Variants. All {variant -> String variantName = vari.name.capitalize () // print def task = project.tasks.create("GRouterCompile" + variantName).doLast {
        println("On a mission"} // setGroup task.setgroup ("GRouter") // generateDebugSources > Task.dependson project.tasks["generate${variantName}Sources"]}Copy the code
dependsOn
Variants. All {variant -> String variantName = vari.name.capitalize () // print def task = project.tasks.create("UploadApk" + variantName).doLast {
        File apkFile = variant.outputs[0].outputFile
        // doAssembleRelease task.dependsOn project.tasks["assemble${variantName}"]}Copy the code
Automated execution

Sometimes we need to create automated tasks instead of manually executing commands, such as automatically uploading APK to a cloud backup every time a formal version is packaged.

variants.all { variant ->
    String variantName = variant.name.capitalize() / / the Debug and Release
    if (variantName.contains("Release")) {
        project.tasks["assemble${variantName}"].doLast {
            File apkFile = variant.outputs[0].outputFile
            // do something}}}Copy the code
Group, a Group

Set groups for easy Task searching.

task.setGroup("grouter")
Copy the code

project.dependencies

We can add dependencies to our target Module through plug-ins

class GRouterPlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        project.dependencies {
            implementation 'com. Alibaba: fastjson: 1.1.45. Android'}}}Copy the code