Creating Custom Gradle Plugins Using buildSrc and Independent Modules

This guide explains how to develop custom Gradle plugins by using the three supported approaches—directly in build.gradle, within the buildSrc directory, or in an independent module—covering plugin implementation in Groovy, extension creation, resource setup, and publishing to a local Maven repository for reuse in Android projects.

Hujiang Technology
Hujiang Technology
Hujiang Technology
Creating Custom Gradle Plugins Using buildSrc and Independent Modules

Gradle supports three ways to create custom plugins: directly in a build.gradle script, inside the buildSrc directory, or in an independent module.

When developing a plugin, you can work in either IntelliJ IDEA or Android Studio; the only difference is that IDEA offers a Gradle plugin that simplifies file and directory creation, while Android Studio requires manual setup.

Using buildSrc in a project

The buildSrc folder is Gradle's default location for custom plugin code. Create the following build.gradle inside buildSrc:

apply plugin: 'groovy'

dependencies {
    compile gradleApi()
    compile localGroovy()
}

Next, add a Groovy class that implements the Plugin interface. The class defines a task (e.g., testPlugin) that prints a log line when executed.

Creating a Groovy Extension

An extension allows the main project to pass configuration values to the plugin. Define a simple Groovy class such as:

package com.xys;

class MyExtension {
    String message
}

After creating the extension, modify the plugin class to load it via project.extensions.create.

Adding plugin resources

Under src/main/resources create the directory structure META-INF/gradle-plugins and place a properties file (e.g., pluginsrc.properties) containing: implementation-class=com.xys.MainPluginForBuildSrc This file tells Gradle which class implements the plugin.

Applying the plugin in the main project

In the main project's build.gradle, load the plugin with: apply plugin: 'pluginsrc' Configure the extension:

pluginsrc{
    message = 'hello gradle plugin'
}

Running gradle testPlugin produces:

:app:testPlugin
hello gradle plugin

Using a local Maven repository

For reusable plugins, create an Android Library module, replicate the buildSrc structure, and add Maven publishing configuration to its build.gradle. Execute gradle uploadArchives to publish the plugin to a local repo.

In the consuming project, reference the plugin with:

apply plugin: 'com.xys.plugin'

buildscript {
    repositories {
        maven { url uri('../repo') }
    }
    dependencies {
        classpath 'com.xys.plugin:plugin:2.0.0'
    }
}

Alternatively, copy the generated JAR to a libs folder and add:

classpath fileTree(dir: 'libs', include: '*.jar') // 使用jar

After configuration, the custom plugin can be invoked in the main project just like any standard Gradle plugin.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GroovyPlugin Developmentbuildsrc
Hujiang Technology
Written by

Hujiang Technology

We focus on the real-world challenges developers face, delivering authentic, practical content and a direct platform for technical networking among developers.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.