Build script script and buildSrc method in Gradle plug-in development

Gradle plug-in development (2)

Link to this article: https://blog.csdn.net/feather_wch/article/details/131746388

Build scriptScript

The plugin is written in the build.gradle file, the current file is visible, simple logic

apply plugin:MyPlugin

class MyPlugin implements Plugin<Project>{
    
    
    @Override
    void apply(Project target) {
    
    
        println 111
    }
}

buildSrc directory

The plug-in source code is placed in the root directory /buildSrc/src/main/groovy and is only visible to this project

Same level directory as app, used by build.gradle of all submodules

create groovy file

class APlugin{
    
    
    public static void test(){
    
    
        println "I'm APlugin"
    }
}

Use directly in app/build.gradle:

APlugin.test()

Create build.gradle of buildSrc to help identify

buildSrc/build.gradle

apply plugin:'groovy'

You can directly develop your own plug-ins and create MyPlugin.groovy implements Plugin

import org.gradle.api.Plugin;
import org.gradle.api.Project;

public class MyPlugin implements Plugin<Project> {
    
    
    @Override
    public void apply(Project target) {
    
    
        println "I'm MyPlugin"
    }
}

custom plugin name

Create folder: buildSrc\src\main\resources\META-INF\gradle-plugins

Create file: com.temp.newplugin.properties

Add plugin path:

implementation-class=MyPlugin

Use the plugin:

app的build.gradle

plugins {
    //xxx
    id 'com.temp.newplugin'
}

After synchronizing the project, you can see the output information:

> Configure project :app
I'm MyPlugin

independent project

Can be published to the repository, other projects can use

Guess you like

Origin blog.csdn.net/feather_wch/article/details/131746388