Gradle custom plug in

Official text

gradle learning official translation website

1. New project

Configure build.gradle

//Use the official 'groovy' plug-in
apply plugin: 'groovy'

//Add api dependency
dependencies {dependencyHandler ->
    dependencyHandler .compile gradleApi()
   dependencyHandler. compile localGroovy()
}
//Add the currently used version of gradle as 2.1.3
dependencies { DependencyHandler dependencyHandler ->
    dependencyHandler.compile 'com.android.tools.build:gradle:2.1.3'
    //dependencyHandler.compile fileTree()
}




Create resources

Notice that the names in the red box are the same!


image.png

2. Create a pluginLaunch class

 class MyPluginLaunch implements Plugin<Project> {

    /**
     * Because it is added by traversing the List order, you can add them one by one in Plugin
     registerTransform The second parameter of the method is dependsOn, which can be set manually
     * @param project
     */

    @Override
    public void apply(Project project) {

              ZDMLogger.i('Project enable MyPluginLaunch plugin')

        project.extensions.create("ccjReleaseInfo",ReleaseInfoExtension)        //Create extended properties

        project.tasks.create("ccjReleaseTask",ReleaseInfoTask) //Create a custom task

    }

}



3. Create a new custom attribute class Extension

This attribute can be used directly in gradle similar to android {}

class ReleaseInfoExtension extends Extension {

    String versionName
    String versionCode
    String versionInfo
    String fileName


    @Override
    public String toString() {
        return "ReleaseInfoExtension{" +
                "versionName='" + versionName + '\'' +
                ", versionCode='" + versionCode + '\'' +
                ", versionInfo='" + versionInfo + '\'' +
                ", fileName='" + fileName + '\'' +
                '}'
    }
}


4. Create a new custom Task

Used to handle variables in the custom property ReleaseInfoExtension


class ReleaseInfoTask extends DefaultTask{

    ReleaseInfoTask() {
        group='zdmrouter'
        description='the realeaseInfo Task of zdm router'
        mustRunAfter()
    }

    /**
     * Between doFitst and doLast
     */
    @TaskAction
    void doAction (){

        updateInfo()
    }




    private void updateInfo(){

        String versionCodeMsg=project.extensions.getByName("ccjReleaseInfo").versionCode //Where ccjReleaseInfo is the variable defined in launcher

        ZDMLogger.i( "ReleaseInfoTask.updateInfo.versionCodeMsg>>>>"+versionCodeMsg)



    }

}

5. Publish plugin to jcenter or the server warehouse address built by yourself, and then rely on it

//In dependencies in the root project
        classpath 'com.smzdm:zdm_router_register:1.1.0'


//Then, where it is used,; is similar to relying on Android plug-ins. It depends on its own plug-ins
apply plugin: 'com.smzdm.android.router.register'



6. After the dependency is completed, it can be done in the gradle of the application custom plug-in

Configure custom properties

ccjReleaseInfo {

    versionName = "1.0"
    versionCode = 1
    versionInfo = "versionInfo>>ccjReleaseInfo"
}

Run custom Task

gradle ccjReleaseTask

Or call it directly in the gradle panel

image.png

Keywords: Android Gradle Attribute

Added by bigsid on Sat, 14 Dec 2019 17:43:48 +0200